华为OJ——简单错误记录

简单错误记录

题目描述

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

处理: 

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

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

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

输入描述:

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

输出描述:

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

输入例子:

E:\V1R2\product\fpgadrive.c   1325

输出例子:

fpgadrive.c 1325 1

解答代码:

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

typedef struct node
{
    string fileName;
    int    fileLine;
    int count;
    node(string fileName_,int fileLine_)
    {
        this->fileName=fileName_;
        this->fileLine=fileLine_;
        this->count=1;
    }
    bool operator== (const node & errnode) const
    {
        return (errnode.fileName == fileName) && (errnode.fileLine==fileLine);
    }
} ERRNODE;

string getFileName(string tempName)
{
    int index=tempName.find_last_of('\\');
    //属于文件路径
    if(index>=0)
    {
        tempName=tempName.substr(index+1);
    }
    int length=tempName.length();
    if(length>16)
    {
        tempName=tempName.substr(length-16);
    }
    return tempName;
}

int main()
{
    //存储错误文件的记录
    vector<ERRNODE> v;
    vector<ERRNODE> ::iterator pos;
    v.clear();
    //存储临时输入的错误记录信息
    string tempName;
    int    tempLine;
    //存储分离的文件名
    string fileName;
    while(cin>>tempName>>tempLine)
    {
        fileName=getFileName(tempName);
        ERRNODE errnode(fileName,tempLine);
        //V集合为空直接插入即可
        if(v.size()==0)
        {
            v.push_back(errnode);
        }
        else
        {
            pos=find(v.begin(),v.end(),errnode);
            if(pos==v.end())
            {
                v.push_back(errnode);
            }
            else
            {
                pos->count++;
            }
        }
    }
    int index=v.size()-8;
    if(index>=0)//数据>>=8条,只输出最后8条
    {
        for(; index<v.size(); index++)
            cout<<v[index].fileName<<" "<<v[index].fileLine<<" "<<v[index].count<<endl;
    }
    else//记录小于8条全部输出
    {
        for(index=0; index<v.size(); index++)
            cout<<v[index].fileName<<" "<<v[index].fileLine<<" "<<v[index].count<<endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值