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

本文共 2251 字,大约阅读时间需要 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/

    你可能感兴趣的文章
    NUMPY矢量化np.prod不能构造具有超过32个操作数的ufunc
    查看>>
    Numpy矩阵与通用函数
    查看>>
    numpy绘制热力图
    查看>>
    numpy转PIL 报错TypeError: Cannot handle this data type
    查看>>
    Numpy闯关100题,我闯了95关,你呢?
    查看>>
    nump模块
    查看>>
    Nutch + solr 这个配合不错哦
    查看>>
    NuttX 构建系统
    查看>>
    NutUI:京东风格的轻量级 Vue 组件库
    查看>>
    NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
    查看>>
    NutzWk 5.1.5 发布,Java 微服务分布式开发框架
    查看>>
    NUUO网络视频录像机 css_parser.php 任意文件读取漏洞复现
    查看>>
    NUUO网络视频录像机 upload.php 任意文件上传漏洞复现
    查看>>
    Nuxt Time 使用指南
    查看>>
    NuxtJS 接口转发详解:Nitro 的用法与注意事项
    查看>>
    NVDIMM原理与应用之四:基于pstore 和 ramoops保存Kernel panic日志
    查看>>
    NVelocity标签使用详解
    查看>>
    NVelocity标签设置缓存的解决方案
    查看>>
    Nvidia Cudatoolkit 与 Conda Cudatoolkit
    查看>>
    NVIDIA GPU 的状态信息输出,由 `nvidia-smi` 命令生成
    查看>>