博客
关于我
华为笔试-简单错误记录
阅读量:113 次
发布时间:2019-02-26

本文共 2258 字,大约阅读时间需要 7 分钟。

开发一个简单的错误记录功能模块

问题描述

我们需要开发一个简单的错误记录功能模块,能够记录出错的代码所在的文件名称和行号。具体要求如下:

  • 记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加。
  • 文件名超过16个字符时,只记录最后16个有效字符。
  • 文件路径可能存在,记录时需去除路径,只保留文件名。
  • 输入描述

    输入是一行或多行字符串,每行包括带路径文件名称、行号,两者用空格隔开。文件路径为Windows格式,例如:E:\V1R2\product\fpgadrive.c 1325

    输出描述

    将所有记录统计并输出,格式为:文件名代码行数数目,一个空格隔开。结果按数目从多到少排序,数目相同则按输入顺序排序。若超过8条记录,只输出前8条。文件名若超过16字符,输出最后16字符。

    解决思路

    1. 数据存储

    使用哈希表(字典)存储错误记录,键为文件名,值为记录的行号和计数。每次遇到相同文件名和行号的记录,计数增加,若超过8条则处理。

    2. 处理文件名

    • 如果文件名长度超过16字符,仅保留最后16个字符。
    • 若文件名不同但仅末尾16字符相同,不合并。

    3. 去除路径

    提取文件名,去除路径信息。

    4. 排序

    使用自定义比较器,按错误计数排序,计数相同按出现顺序排序。

    5. 优化

    通过哈希表和自定义排序结构实现,时间复杂度为O(n)存储,O(n log n)排序。

    代码实现

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std; class Info { public: int rank; // 排序权重 int count; // 错误记录数 string file; // 文件名 Info(int _r, int _c, const string &_s) : rank(_r), count(_c), file(_s) {} bool operator<(const Info &other) const { if (count == other.count) { return rank < other.rank; } return count > other.count; } }; int fun(const char *x) { int len = 0; while (*x++) len++; return len - 1; } int main() { char x[] = {'1','2','3','4'}; int m = fun(x); set
    errors; ifstream geoFile; geoFile.open("text.txt", ios::in); if (geoFile.is_open()) { string instr; while (getline(geoFile, instr)) { if (instr.empty()) break; size_t lastSlash = instr.rfind('\\'); string file = instr.substr(lastSlash + 1); int count = 1; auto it = errors.begin(); while (it != errors.end() && it->file == file) { count++; it++; } if (it == errors.end()) { errors.insert(Info(1, count, file)); } else { Info &existing = *it; existing.count += count; } } } geoFile.close(); vector
    result; for (const auto &info : errors) { string trimmed = info.file; size_t pos = trimmed.find(' '); if (pos > 16) { trimmed = trimmed.substr(pos - 16); } result.push_back({info.rank, info.count, trimmed}); } sort(result.begin(), result.end()); int outputCount = min(8, (int)result.size()); for (int i = 0; i < outputCount; ++i) { const Info &info = result[i]; cout << info.file << ' ' << info.count << ' '; } }

    代码解释

  • 类Info:用于存储错误记录的文件名和计数,并定义了自定义比较器,用于排序。
  • fun函数:计算字符串长度。
  • 主程序
    • 读取文件内容,提取文件名和行号。
    • 使用哈希表存储错误记录,处理重复和文件名长度。
    • 排序后输出前8条记录。
  • 通过这种方法,能够高效地处理错误记录,满足要求的存储和排序功能。

    转载地址:http://tcok.baihongyu.com/

    你可能感兴趣的文章
    NoSQL介绍
    查看>>
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>