简单错误记录
题目描述
开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。
处理:
1、 记录最多8条错误记录,循环记录,对相同的错误记录(净文件名称和行号完全匹配)只记录一条,错误计数增加;
2、 超过16个字符的文件名称,只记录文件的最后有效16个字符;
3、 输入的文件可能带路径,记录文件名称不能带路径。
输入描述:
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
输出描述:
将所有的记录统计并将结果输出,格式:文件名 代码行数 数目,一个空格隔开,如:
输入例子:
E:\V1R2\product\fpgadrive.c 1325
输出例子:
fpgadrive.c 1325 1
解答代码:
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
typedef struct node
{
string fileName;
int fileLine;
int count;
node(string fileName_,int fileLine_)
{
this->fileName=fileName_;
this->fileLine=fileLine_;
this->count=1;
}
bool operator== (const node & errnode) const
{
return (errnode.fileName == fileName) && (errnode.fileLine==fileLine);
}
} ERRNODE;
string getFileName(string tempName)
{
int index=tempName.find_last_of('\\');
//属于文件路径
if(index>=0)
{
tempName=tempName.substr(index+1);
}
int length=tempName.length();
if(length>16)
{
tempName=tempName.substr(length-16);
}
return tempName;
}
int main()
{
//存储错误文件的记录
vector<ERRNODE> v;
vector<ERRNODE> ::iterator pos;
v.clear();
//存储临时输入的错误记录信息
string tempName;
int tempLine;
//存储分离的文件名
string fileName;
while(cin>>tempName>>tempLine)
{
fileName=getFileName(tempName);
ERRNODE errnode(fileName,tempLine);
//V集合为空直接插入即可
if(v.size()==0)
{
v.push_back(errnode);
}
else
{
pos=find(v.begin(),v.end(),errnode);
if(pos==v.end())
{
v.push_back(errnode);
}
else
{
pos->count++;
}
}
}
int index=v.size()-8;
if(index>=0)//数据>>=8条,只输出最后8条
{
for(; index<v.size(); index++)
cout<<v[index].fileName<<" "<<v[index].fileLine<<" "<<v[index].count<<endl;
}
else//记录小于8条全部输出
{
for(index=0; index<v.size(); index++)
cout<<v[index].fileName<<" "<<v[index].fileLine<<" "<<v[index].count<<endl;
}
return 0;
}