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

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 16:41   35   0

题目描述

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

处理:

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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP