华为机试题——简单错误记录

19 篇文章 1 订阅

题目描述

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

 

处理: 

 

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

 

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

 

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



输入描述:

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



输出描述:

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


输入例子:
E:\V1R2\product\fpgadrive.c   1325

输出例子:

fpgadrive.c 1325 1

代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
 
bool compare(pair<string, int> a, pair<string, int> b){
    return a.second > b.second;
}
int main(void){
    string input, file;
    vector<pair<string, int>> errors;
    while (getline(cin, input)){
        if (input.size() == 0)
            break;
        unsigned int f = input.rfind('\\');
        file = input.substr(f + 1);
        errors.push_back(make_pair(file, 1));
        for (int i = 0; i<(errors.size() - 1); i++){
            if (errors[i].first == file){
                errors[i].second++;
                errors.pop_back(); break;
            }
        }
    }
    stable_sort(errors.begin(), errors.end(), compare);
    int idx = 0;
    while (idx<8 && idx<errors.size()){
        string check = errors[idx].first;
        int t = check.find(' ');
        if (t>16)
            errors[idx].first.erase(0, t - 16);
        cout << errors[idx].first << ' ' << errors[idx].second << endl;
        idx++;
    }
}
//通过所有测试用例:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
 
using namespace std;
 
string getFileName(string path){
    int pos = path.rfind('\\');
    return path.substr(pos + 1);
}
 
string modifyName(string name){
    if (name.size() > 16){
        name = name.substr(name.size() - 16);
    }
 
    return name;
}
 
struct ErrRecord{
    string file;
    int lineNo;
    int count;
 
    ErrRecord(string file, int lineNo){
        this->file = file;
        this->lineNo = lineNo;
        count = 1;
    }
 
    bool operator==(const ErrRecord & a){
        return (file == a.file) && (lineNo == a.lineNo);
    }
};
 
int main(){
 
    string file;
    int lineNo;
    vector<ErrRecord> myvec;
    while (cin >> file >> lineNo){
        ErrRecord record(getFileName(file), lineNo);
        auto res = find(myvec.begin(), myvec.end(), record);
        if (res == myvec.end()){
            myvec.push_back(record);
        }
        else{
            res->count++;
        }
    }
 
    int count = 0;
    for (auto item : myvec){
        if (count + 8 >= myvec.size()){
            cout << modifyName(item.file) << " " << item.lineNo << " "
                << item.count << endl;
        }
        count++;       
    }
 
     
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值