华为OJ:开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。

用到了类string的length(), size(),find_first_of(),find_last_of(),substr(),push_back()函数 

#include 
#include 
#include 
#include 
using namespace std;

struct errorLog
{
	string name;
	string line;
	int count;
};
errorLog createErrorLog(string name, string line)
{
	errorLog log;
	int nameSize = name.length();
	int index = -1;
	index = name.find_last_of('\\');
	name = name.substr(index + 1);
	log.name = name;
	log.line = line;
	log.count = 1;
	return log;
}

void recordErrorLog(int number, errorLog log, vector & resu)
{
	bool isRepeat = false;
	for (int i = 0; i < resu.size(); i++)
	{
		if (resu[i].name == log.name && resu[i].line == log.line)
		{
			resu[i].count++;
			isRepeat = true;
			break;
		}
	}
	if (isRepeat == false)
		resu.push_back(log);
}

int main()
{
	string full;
	string name;
	string line;
	int number = 8;
	vector result;
    // while (cin >> name >> line)
	while (getline(cin, full))
	{
		int ind = full.find_first_of(' ');
		name = full.substr(0, ind);
		line = full.substr(ind + 1);
		errorLog log = createErrorLog(name, line);
		recordErrorLog(number, log, result);
	}
	if (result.size() >= 8)
	{
		for (int i = result.size() - 8; i < result.size(); 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;
		}

	}
	else
	{
		for (int i = 0; i < result.size(); 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;
		}
	}

	return 0;
}

 

你可能感兴趣的:(数据结构与算法,C/C++,华为)