開發一個簡單錯誤記錄功能小模塊,能夠記錄出錯的代碼所在的文件名稱和行號。


// ConsoleApplication10.cpp : 定義控制台應用程序的入口點。
//

#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct ErrordRecord //錯誤記錄
{
public:
	string pathWay;;
	string fileName;
	int line;
	int num;//記錄的數目
};

int main()
{
	vector<ErrordRecord *> errordRecords;

	//	while (errordRecords.size()<8)//當記錄多余8條是結束程序
	string pathWay;
	string fileName;
	int line;
	while (cin >> pathWay >> line)
	{

		fileName = pathWay.substr(pathWay.find_last_of("\\") + 1);
		bool exist = false;//如果記錄是否已經存在


		for (int i = 0;i < errordRecords.size();++i)
		{
			if (fileName == (errordRecords[i]->fileName) && line == (errordRecords[i]->line))
			{
				errordRecords[i]->num = (errordRecords[i]->num) + 1;
				exist = true;
				break;
			}
		}
		if (exist == false)//記錄不存在
		{
			ErrordRecord *errordRecord = new ErrordRecord();
			errordRecord->fileName = fileName;
			errordRecord->line = line;
			errordRecord->num = 1;
			errordRecords.push_back(errordRecord);
		}

		
	}


	//根據數目多少輸出,冒泡排序
	for (int i = 0;i < errordRecords.size();++i)
	{
		for (int j = i + 1;j < errordRecords.size();++j)
		{
			if (errordRecords[i]->num < errordRecords[j]->num)
			{
				string fileName = errordRecords[i]->fileName;
				int line = errordRecords[i]->line;
				int num = errordRecords[i]->num;

				errordRecords[i]->fileName = errordRecords[j]->fileName;
				errordRecords[i]->line = errordRecords[j]->line;
				errordRecords[i]->num = errordRecords[j]->num;

				errordRecords[j]->fileName = fileName;
				errordRecords[j]->line = line;
				errordRecords[j]->num = num;

			}
		}
	}

	//for (int i = 0;i < errordRecords.size();++i)
	for (int i = 0;i < errordRecords.size() && i < 8;++i)//輸出前八條記錄
	{
		if (errordRecords[i]->fileName.length() > 16)
		{
			cout << errordRecords[i]->fileName.substr(fileName.length()-16)
				<< " " << errordRecords[i]->line << " " << errordRecords[i]->num << " ";
		}
		else {
			cout << errordRecords[i]->fileName << " " << errordRecords[i]->line << " " << errordRecords[i]->num << " ";
		}
		

	}


	return 0;
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM