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

本文为转载,原博客地址: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;

}

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<string>  
  3. #include<vector>  
  4. #include<map>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. struct ErrorLog  
  8. {  
  9.     string name;  
  10.     int line; //最好写成int型  
  11.     int count;  
  12. };  
  13. ErrorLog CreateErrorLog(string name, int line)  
  14. {  
  15.     ErrorLog log;  
  16.     int nameSize = name.length();  
  17.     int index = -1;  
  18.     for (int i = nameSize - 1; i >= 0; --i)  
  19.     {  
  20.         if (name[i] == '\\'//注意!  
  21.         {  
  22.             index = i;  
  23.             break;  
  24.         }  
  25.     }  
  26.     name = name.substr(index + 1);  
  27.     log.name = name;  
  28.     log.line = line;  
  29.     log.count = 1;  
  30.     return log;  
  31. }  
  32. void RecordErrorLog(int number, ErrorLog log, vector<ErrorLog>& res)  
  33. {  
  34.     bool isrepeat = false;  
  35.     for (int i = 0; i < res.size(); ++i)  
  36.     {  
  37.         if (res[i].name == log.name && res[i].line == log.line)  
  38.         {  
  39.             res[i].count++;  
  40.             isrepeat = true;  
  41.             break;  
  42.         }  
  43.     }  
  44.     if (isrepeat == false)  
  45.     {  
  46.         //if (res.size() < 8)  
  47.         //{  
  48.             res.push_back(log);  
  49.         //}  
  50.           
  51.     }  
  52. }  
  53. static bool Compare(const ErrorLog& a, const ErrorLog& b)  
  54. {  
  55.     return a.count>b.count;  
  56. }  
  57. int main()  
  58. {  
  59.     string name;  
  60.     int line;  
  61.     vector<ErrorLog> result;  
  62.       
  63.     int number = 8;  
  64.     while (cin>>name>>line)  
  65.     {  
  66.         ErrorLog log = CreateErrorLog(name, line);  
  67.         RecordErrorLog(number, log, result);  
  68.     }  
  69.     sort(result.begin(), result.end(),Compare);//升序排序  
  70.     for (int i = 0; i < 8; ++i)  
  71.     {  
  72.         int len = result[i].name.length();  
  73.         if (len>16)  
  74.             result[i].name = result[i].name.substr(len - 16);  
  75.         cout << result[i].name << " " << result[i].line << " " << result[i].count << endl;  
  76.     }     
  77. }  

#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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值