本文为转载,原博客地址:http://blog.csdn.net/moli152_/article/details/48008523
一:简单错误记录
开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。
处理:
1.记录最多8条错误记录,对相同的错误记录(即文件名称和行号完全匹配)只记录一条,错误计数增加;(文件所在的目录不同,文件名和行号相同也要合并)
2.超过16个字符的文件名称,只记录文件的最后有效16个字符;(如果文件名不同,而只是文件名的后16个字符和行号相同,也不要合并)
3.输入的文件可能带路径,记录文件名称不能带路径
输入描述:
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。
文件路径为windows格式
如:E:\V1R2\product\fpgadrive.c 1325
输出描述:
将所有的记录统计并将结果输出,格式:文件名代码行数数目,一个空格隔开,如: fpgadrive.c 1325 1
结果根据数目从多到少排序,数目相同的情况下,按照输入第一次出现顺序排序。
如果超过8条记录,则只输出前8条记录.
如果文件名的长度超过16个字符,则只输出后16个字符
输入例子:
E:\V1R2\product\fpgadrive.c 1325
输出例子:
fpgadrive.c 1325 1
思路:
建一个结构体ErrorLog,包含文件名,行号,错误数量
结果数据集vector<ErrorLog> res,
对于输入的每一个<filePath, line>对,得到<fileName,line> 然后和res里面的每条记录比较,看是否存在一条记录其<文件名,行号>和输入相等,若是,则记录count++;否则,将输入添加到res中;这样处理完所有输入之后,
对vector按照ErrorLog的count字段进行排序,
sort(res.begin(), res.end(),Compare)
其中 Compare函数为:
static bool Compare(const ErrorLog& a, const ErrlrLog& b)
{
return a.count>b.count;
}
- #include<iostream>
- #include<string>
- #include<vector>
- #include<map>
- #include<algorithm>
- using namespace std;
- struct ErrorLog
- {
- string name;
- int line; //最好写成int型
- int count;
- };
- ErrorLog CreateErrorLog(string name, int line)
- {
- ErrorLog log;
- int nameSize = name.length();
- int index = -1;
- for (int i = nameSize - 1; i >= 0; --i)
- {
- if (name[i] == '\\') //注意!
- {
- index = i;
- break;
- }
- }
- name = name.substr(index + 1);
- log.name = name;
- log.line = line;
- log.count = 1;
- return log;
- }
- void RecordErrorLog(int number, ErrorLog log, vector<ErrorLog>& res)
- {
- bool isrepeat = false;
- for (int i = 0; i < res.size(); ++i)
- {
- if (res[i].name == log.name && res[i].line == log.line)
- {
- res[i].count++;
- isrepeat = true;
- break;
- }
- }
- if (isrepeat == false)
- {
- //if (res.size() < 8)
- //{
- res.push_back(log);
- //}
- }
- }
- static bool Compare(const ErrorLog& a, const ErrorLog& b)
- {
- return a.count>b.count;
- }
- int main()
- {
- string name;
- int line;
- vector<ErrorLog> result;
- int number = 8;
- while (cin>>name>>line)
- {
- ErrorLog log = CreateErrorLog(name, line);
- RecordErrorLog(number, log, result);
- }
- sort(result.begin(), result.end(),Compare);//升序排序
- for (int i = 0; i < 8; ++i)
- {
- int len = result[i].name.length();
- if (len>16)
- result[i].name = result[i].name.substr(len - 16);
- cout << result[i].name << " " << result[i].line << " " << result[i].count << endl;
- }
- }
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
struct errorLog{
string fileName;
int line;
int count;
};
errorLog GreatErrorLog(string filePath, int line)
{
errorLog Log;
int index=-1; //问题要考虑全面,两种情况都要能用
int len = filePath.length();
for (int i = len - 1; i >= 0; --i){
if (filePath[i] == '\\'){ //在C语言中单斜杠用双斜杠表示
index = i;
break;
}
}
filePath = filePath.substr(index + 1);//考虑清楚,智障
Log.fileName = filePath;
Log.line = line;
Log.count = 1;
return Log;
}
static bool compare(const errorLog& a, const errorLog& b){
return a.count > b.count;
}
void Rcord(errorLog curErrorLog, vector<errorLog> &res){
bool isrepeat = false;
for (int i = 0; i < res.size(); ++i){ //我特么的,多写了一个等号,导致看了半天都没看出来,错误:数组或栈溢出,递归过多之类,一定要小心
if (curErrorLog.fileName == res[i].fileName && curErrorLog.line == res[i].line) //情况考虑清楚。for和if都不能少
{
res[i].count++;
isrepeat = true;
break;
}
}
if (isrepeat==false){ //if 后面的条件一定要看清楚
res.push_back(curErrorLog);
}
}
int main()
{
string filePath;
int line;
errorLog curErrorLog;
vector<errorLog> res;
while (cin >> filePath >> line)
{
curErrorLog = GreatErrorLog(filePath, line);
Rcord(curErrorLog, res);
}
sort(res.begin(), res.end(), compare);//降序,关于定义compare,了解一下
for (int i = 0; i < 8; ++i){
int length = res[i].fileName.length();
if (length>16){
res[i].fileName = res[i].fileName.substr(length - 16);//substr了解一下
}
cout << res[i].fileName <<" "<< res[i].line <<" "<< res[i].count << endl;//一定要看清楚要求,要求用空格隔开
}
}
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef struct errorLog{
string fileName;
int line;
int count;
}errorLog;
errorLog GreatErrorLog(string filePath, int line)
{
errorLog Log;
int index = -1;
int len = filePath.length();
for (int i = len - 1; i >= 0; i--){
if (filePath[i] == '\\'){ //条件括号里面的等号是两个,智障
index = i;
break;
}
}
string Name = filePath.substr(index + 1);
Log.fileName = Name;
Log.line = line;
Log.count = 1;
return Log;
}
bool compare(errorLog& a, errorLog& b){
return a.count > b.count;
}
int main()
{
string filePath;
int line;
errorLog curErrorLog;
vector<errorLog> res;
while (cin >> filePath >> line)
{
curErrorLog = GreatErrorLog(filePath, line);
bool isrepeat = false;
for (int i = 0; i < res.size(); i++){ // //涉及到size(),length()的 i不能有等号
if (curErrorLog.fileName == res[i].fileName && curErrorLog.line == res[i].line)
{
res[i].count ++;
isrepeat = true;
break;
}
}
if (isrepeat==false){
res.push_back(curErrorLog);
}
}
sort(res.begin(), res.end(), compare);
for (int i = 0; i < 8&&i<res.size(); i++){
int length = res[i].fileName.size();
if (length>16){
string name = res[i].fileName;
name = name.substr(length - 16);
res[i].fileName = name;
}
cout << res[i].fileName <<" "<< res[i].line <<" "<< res[i].count << endl;
}
return 0;
}