开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。
处理:
1、 记录最多8条错误记录,循环记录,对相同的错误记录(净文件名称和行号完全匹配)只记录一条,错误计数增加;
2、 超过16个字符的文件名称,只记录文件的最后有效16个字符;
3、 输入的文件可能带路径,记录文件名称不能带路径。
输入描述:一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
输出描述:将所有的记录统计并将结果输出,格式:文件名 代码行数 数目,一个空格隔开,如:
示例1
输入 E:\V1R2\product\fpgadrive.c 1325
输出 fpgadrive.c 1325 1
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct error_codes{
string filename;
int codeline;
int counter=1;
bool operator==(const error_codes &a){
if(a.filename==filename&&a.codeline==codeline){
return true;
}else{
return false;
}
}
};
string getfilename(string &path);
int main(){
vector<error_codes> arr;
string path;
int codeline;
while(cin>>path>>codeline){
error_codes temp;
temp.filename=getfilename(path);
temp.codeline=codeline;
vector<error_codes>::iterator res;
res=find(arr.begin(),arr.end(),temp);
if(res==arr.end()){
arr.push_back(temp);
}else{
res->counter++;
}
}
int j=0;
if(arr.size()>8){
j=arr.size()-8;
}
for(;j<arr.size();j++){
cout<<arr[j].filename<<" "<<arr[j].codeline<<" "<<arr[j].counter<<endl;
}
return 0;
}
string getfilename(string &path){
int pos;
string result;
pos=path.rfind('\\');
result=path.substr(pos+1,path.size()-pos);
if(result.length()>16){
result=result.substr(result.length()-16,16);
}
return result;
}