华为机试题2016(一) 简单错误记录

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 64M,其他语言128M

开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。
处理:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并)
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并)
3.输入的文件可能带路径,记录文件名称不能带路径

输入描述:
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
文件路径为windows格式
如:E:V1R2productfpgadrive.c 1325

输出描述:
将所有的记录统计并将结果输出,格式:文件名代码行数数目,一个空格隔开,如: fpgadrive.c 1325 1 
结果根据数目从多到少排序,数目相同的情况下,按照输入第一次出现顺序排序。
如果超过8条记录,则只输出前8条记录.
如果文件名的长度超过16个字符,则只输出后16个字符

输入例子1:
E:V1R2productfpgadrive.c 1325

输出例子1:
fpgadrive.c 1325 1

思路参考:https://blog.csdn.net/moli152_/article/details/48008523
注意是输出所有错误记录的前8个,而不是每个错误记录的最高累计数为8.

substr()函数使用参考:https://blog.csdn.net/wang13342322203/article/details/91457260

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node
 4 {
 5     string name;
 6     int row, times;//row为错误行,times为累计数
 7 };
 8 typedef struct node pnode;
 9 
10 void output(vector<node>& in)//输出函数
11 {
12     for (int i=0; i<in.size(); i++)
13     {
14         if (i == 8)//只输出所有记录的前8条
15         {
16             break;
17         }
18 
19         int len = in[i].name.length();
20         if (len >16)//文件名过长的进行截取
21         {
22             int ind = len - 16;
23             in[i].name = in[i].name.substr(ind);
24         }
25 
26         cout<<in[i].name<<" "<<in[i].row<<" "<<in[i].times<<endl;
27     }
28 }
29 
30 void add(vector<node>& result, string name, int row)//添加函数
31 {
32     for (int i=name.length()-1; i>=0; i--)//截取文件名
33     {
34         if (name[i] == '\')
35         {
36             name = name.substr(i+1);
37             break;
38         }
39     }
40 
41     int isrepeat=0;
42     for (int i=0; i<result.size(); i++)//看是否重复
43     {
44         if (result[i].name == name && result[i].row == row)
45         {
46             result[i].times++;
47             isrepeat = 1;
48             break;
49         }
50     }
51 
52     if (!isrepeat)//增加新的错误条目
53     {
54         pnode temp;
55         temp.name=name;
56         temp.row=row;
57         temp.times=1;
58         result.push_back(temp);
59     }
60 }
61 
62 int cmp(const pnode a, const pnode b)//排序时的比较函数
63 {
64     return a.times > b.times;//按累计条数降序
65 }
66 
67 int main()
68 {
69     string name;
70     int row;
71     vector<node> result;//错误条目集合
72 
73     while (cin>>name>>row)
74     {
75         add(result, name, row);
76     }
77     sort(result.begin(), result.end(), cmp);
78     output(result);
79 
80     return 0;
81 }


 
原文地址:https://www.cnblogs.com/hemeiwolong/p/12295662.html