简单错误记录/华为机试(C/C++)

题目描述

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

 

处理: 

 

1、 记录最多8条错误记录,循环记录,对相同的错误记录(净文件名称和行号完全匹配)只记录一条,错误计数增加;

 

2、 超过16个字符的文件名称,只记录文件的最后有效16个字符;

 

3、 输入的文件可能带路径,记录文件名称不能带路径。

 

输入描述:

一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。

输出描述:

将所有的记录统计并将结果输出,格式:文件名 代码行数 数目,一个空格隔开,如:

示例1

输入

E:\V1R2\product\fpgadrive.c   1325

输出

fpgadrive.c 1325 1

代码1:

//第十九题 错误记录
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct item
{
	string filename;
	int num_line;
	int num_fre;

	bool operator== (const item& item)
	{
		if (item.filename == filename && item.num_line == num_line)
			return true;
		return false;
	}
};
int main()
{
	int inum = 0;
	vector<item>v_record;
	string in_str;
	int i_num_line;
	while (cin>> in_str>> i_num_line)
	{
		size_t last_position = in_str.find_last_of('\\');
		if (last_position != string::npos)
		{
			string filename = in_str.substr(last_position + 1, in_str.length() - last_position - 1);
			if (filename.length() > 16)
			{
				filename = filename.substr(filename.length() - 16);
			}
			item temp{ filename ,i_num_line ,1};
			vector<item>::iterator it;
			if ((it=find(v_record.begin(), v_record.end(), temp)) != v_record.end())
			{
				it->num_fre += 1;
			}
			else
				v_record.push_back(temp);
		}
		else
		{
			break;
		}
	}
	int i_max = v_record.size();
	int i_min = i_max - 8;
	i_min = i_min > 0 ? i_min : 0;	
	for (int i = i_min; i < i_max; i++)
	{
		cout << v_record[i].filename.c_str() << " " << v_record[i].num_line << " " << v_record[i].num_fre << endl;
	}
	system("pause");
	return 0;
}

3ms

代码2:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct item
{
	string filename;
	int line;
	int cnt;

	bool operator== (const item& item)
	{
		if (item.filename == filename && item.line == line)
			return true;
		return false;
	}
};

// 获取净文件名的最后16个字符
string get_filename(string filename)
{
	string ret;
	int index = filename.find_last_of('\\', filename.size() - 1);
	if (index == -1)
		ret = filename;
	else
		ret = filename.substr(index + 1);

	if (ret.size() > 16)
		ret = ret.substr(ret.size() - 16);

	return ret;
}

int main(void)
{
	string filename;
	int line;
	vector<item> log;
	while (cin >> filename >> line)
	{
		filename = get_filename(filename);
		vector<item>::iterator it;
		item tmp = { filename, line, 1 };
		if ((it = std::find(log.begin(), log.end(), tmp)) != log.end())
			(*it).cnt++;
		else
			log.push_back(tmp);
	}

	int start_index = log.size() - 8;
	if (start_index < 0) start_index = 0;
	for (int i = start_index; i < log.size(); ++i)
		cout << log[i].filename << " " << log[i].line << " " << log[i].cnt << endl;
	system("pause");
	return 0;
}

代码2不是本人所写,感觉写代码2的人对C++std学的挺好的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值