首页 > 试题广场 >

简单错误记录

[编程题]简单错误记录
  • 热度指数:339061 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

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


处理:


1、 记录最多8条错误记录,循环记录,最后只用输出最后出现的八条错误记录。对相同的错误记录只记录一条,但是错误计数增加。最后一个斜杠后面的带后缀名的部分(保留最后16位)和行号完全匹配的记录才做算是相同的错误记录。
2、 超过16个字符的文件名称,只记录文件的最后有效16个字符;
3、 输入的文件可能带路径,记录文件名称不能带路径。也就是说,哪怕不同路径下的文件,如果它们的名字的后16个字符相同,也被视为相同的错误记录
4、循环记录时,只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准

数据范围:错误记录数量满足 ,每条记录长度满足

输入描述:

每组只包含一个测试用例。一个测试用例包含一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。



输出描述:

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

示例1

输入

D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645
E:\je\rzuwnjvnuz 633
C:\km\tgjwpb\gy\atl 637
F:\weioj\hadd\connsh\rwyfvzsopsuiqjnr 647
E:\ns\mfwj\wqkoki\eez 648
D:\cfmwafhhgeyawnool 649
E:\czt\opwip\osnll\c 637
G:\nt\f 633
F:\fop\ywzqaop 631
F:\yay\jc\ywzqaop 631
D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645

输出

rzuwnjvnuz 633 1
atl 637 1
rwyfvzsopsuiqjnr 647 1
eez 648 1
fmwafhhgeyawnool 649 1
c 637 1
f 633 1
ywzqaop 631 2

说明

由于D:\cfmwafhhgeyawnool 649的文件名长度超过了16个字符,达到了17,所以第一个字符'c'应该被忽略。
记录F:\fop\ywzqaop 631和F:\yay\jc\ywzqaop 631由于文件名和行号相同,因此被视为同一个错误记录,哪怕它们的路径是不同的。
由于循环记录时,只以第一次出现的顺序为准,后面重复的不会更新它的出现时间,仍以第一次为准,所以D:\zwtymj\xccb\ljj\cqzlyaszjvlsjmkwoqijggmybr 645不会被记录。  
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct recode 
{
	char str[17];//最后16个字符的文件名
	int num;//行号
	int count;//记错次数
}recode;

void getLastFileName(char path[],char tmp[]) 
{
	char* lastSlash = strrchr(path, '\\'); // 找到最后一个斜杠字符
	char fileName[100] = ""; // 存储文件名,初始化为空字符串

	if (lastSlash != NULL) { // 如果找到了斜杠
		strcpy(fileName, lastSlash+1); // 复制斜杠后面的字符串到fileName
	}
	else {
		strcpy(fileName, path); // 如果没有斜杠,直接复制整个路径到fileName
	}
	int len = strlen(fileName);
	if (len <= 16)
		strcpy(tmp,fileName);
	else
		strcpy(tmp,fileName + len - 16);
}

int main()
{
	recode* list[100] = { 0 };
	char path[100] = { 0 };//路径
	char filename[100] = { 0 };//文件名
	int i = 0;//记录次数
	int j = 0;
	int flag = 0;//重复标志
	int n = 0;//行数
	//初始化字符串
	for (i = 0; i < 100; i++)
	{
		list[i] = (recode*)malloc(sizeof(recode));
		list[i]->num = 0;
		list[i]->count = 0;
		memset(list[i]->str, 0, sizeof(list[i]->str));
	}
	i = 0;
	while (scanf("%s %d", path, &n)==2)//输入:路径和行数分开
	{
		flag = 0;//初始化重复标志
		getLastFileName(path, filename);//记录文件名
		for (j = 0; j < i; j++)
		{
			//重复:文件名相同且错误行数相同
			if (strcmp(filename, list[j]->str) == 0 && list[j]->num ==n)
			{
				list[j]->count++;
				flag = 1;
				break;
			}
		}
		if (flag == 0)//不重复
		{
			strcpy(list[i]->str, filename);
			list[i]->num = n;
			list[i]->count=1;
			i++;//记录次数增加
		}
	}

	//错误记录少于八条则正常输出,否则输出最后八条记录
	if (i < 8)
	{
		for (j = 0; j < i; j++)
		{
			printf("%s %d %d\n", list[j]->str, list[j]->num, list[j]->count);
		}
	}
	else
	{
		for (j = i - 8; j < i; j++)
		{
			printf("%s %d %d\n", list[j]->str, list[j]->num, list[j]->count);
		}
	}

	return 0;
}

编辑于 2023-12-27 11:11:07 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define       N    100
typedef struct record
{
    char str[17];
    char row[4];
    int num;
}record;
int main()
{
    record *note[N],temp;
    char *str[N];
    int i=-1,j,flag1,flag2,m,n,cnt=0,p,q;
    do
    {
        i++;
        str[i]=(char*)malloc(sizeof(char)*100);
        gets(str[i]);
    }while(*str[i]!='\0');
    for(j=0;j<i;j++)
    {
        flag1=0;flag2=0;m=0;p=0;
        while(*(str[j]+m)!='\0')
        {
            if(*(str[j]+m)=='\\')
            {
                flag1=m;
            }
            else if(*(str[j]+m)==' ')
            {
                flag2=m;
                break;
            }
            m++;
        }
        if((flag2-flag1-1)>16)
        {
            n=flag2-16;
            if(j==0)
            {
                note[cnt]=(record*)malloc(sizeof(record));
                while(*(str[j]+n)!=' ')
                {
                    note[cnt]->str[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    note[cnt]->row[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->row[p]='\0';
                note[cnt]->num=1;
                cnt++;
            }
            else if(j>0)
            {
                while(*(str[j]+n)!=' ')
                {
                    temp.str[p++]=*(str[j]+n);
                    n++;
                }
                temp.str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    temp.row[p++]=*(str[j]+n);
                    n++;
                }
                temp.row[p]='\0';
                for(q=0;q<cnt;q++)
                {
                    if(strcmp(note[q]->str,temp.str)==0&&
                       strcmp(note[q]->row,temp.row)==0)
                    {
                        note[q]->num++;
                        break;
                    }
                }
                if(q==cnt)
                {
                    note[cnt]=(record*)malloc(sizeof(record));
                    strcpy(note[cnt]->str,temp.str);
                    strcpy(note[cnt]->row,temp.row);
                    note[cnt]->num=1;
                    cnt++;
                }
            }
        }
        else if((flag2-flag1-1)<=16)
        {
            n=flag1+1;
            if(j==0)
            {
                note[cnt]=(record*)malloc(sizeof(record));
                while(*(str[j]+n)!=' ')
                {
                    note[cnt]->str[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    note[cnt]->row[p++]=*(str[j]+n);
                    n++;
                }
                note[cnt]->row[p]='\0';
                note[cnt]->num=1;
                cnt++;
            }
            else if(j>0)
            {
                while(*(str[j]+n)!=' ')
                {
                    temp.str[p++]=*(str[j]+n);
                    n++;
                }
                temp.str[p]='\0';
                n++;
                p=0;
                while(*(str[j]+n)!='\0')
                {
                    temp.row[p++]=*(str[j]+n);
                    n++;
                }
                temp.row[p]='\0';
                for(q=0;q<cnt;q++)
                {
                    if(strcmp(note[q]->str,temp.str)==0&&
                       strcmp(note[q]->row,temp.row)==0)
                    {
                        note[q]->num++;
                        break;
                    }
                }
                if(q==cnt)
                {
                    note[cnt]=(record*)malloc(sizeof(record));
                    strcpy(note[cnt]->str,temp.str);
                    strcpy(note[cnt]->row,temp.row);
                    note[cnt]->num=1;
                    cnt++;
                }
            }           
        }
    }
    if(cnt<=8)
    {
        for(n=0;n<cnt;n++)
        {
            printf("%s %s %d\n",note[n]->str,note[n]->row,note[n]->num);
        }
    }
    else
    {
        for(n=cnt-8;n<cnt;n++)
        {
            printf("%s %s %d\n",note[n]->str,note[n]->row,note[n]->num);
        }
    }
    return 0;
}

//耗时巨大,虽然代码很low,但一次就通过了

发表于 2022-04-15 19:34:05 回复(0)
可以帮我看看下面这段代码错误在哪吗,与正确代码的区别只在于字符串赋值,我的是 char *name;name= str+l-16;指针赋值。换成    char name[];; strcpy(name, str+l-16);这个就正确了,为什么呢,直接指针赋值的错误在哪呢?


#include<stdio.h>
#include<string.h>
int main()
{
    struct rec
    {
        int row;
        char *s;
        int num;

    };
    struct rec r[100];
    int  q = -1;
    int j=0;
    int line = 0;
    char str[100] ;
    
    while (scanf("%s%d",str,&line) != EOF)
    {
        char *name;
        int k = 0;
        int l = strlen(str);
        int i = 0;
      for(i=l-1;i>=0;i--)
        {
            if(str[i] == '\\')
               break;
        }
        //从i+1 下一位开始复制,还有考虑长度不能超过16
        if(i+1 < l -16)
        {
            name= (str+l-16);
        }else {
         name= (str+i+1);
        }
        
            int flag = 0;
            for (int c = 0; c <= q; c++)
            {
                if (r[c].s==name && r[c].row == line)
                {
                    r[c].num++;
                    flag =1;
                    break;
                }
             
            }
        
   
    if (flag==0)
    {
       q++;
      r[q].s= name;
        r[q].row =line;
        r[q].num=1;
    }
}

    if(q<8)
    {
        for(int i=0;i<=q;i++)
        {
            printf("%s %d %d \n",r[i].s,r[i].row,r[i].num);
        }
    }else {
        for(int i=q-7;i<=q;i++)
        {
            printf("%s %d %d \n",r[i].s,r[i].row,r[i].num);
        }
    }
     
    return 0;
     
    
}
发表于 2022-03-09 19:26:41 回复(0)
按要求来即可。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
    char str[17];
    int row;
    
    int cnt;
}T_rec;

typedef struct{
    T_rec rec[100];
    int pWrite;
}T;

void rec_one(T *p_rec,char *str,int row)
{
    int i,len,flag = 1;
    char *p_e,*p_s;
    char file_name[17];
    
    len = strlen(str);
    
    p_e = str + len - 1;
    
    
    p_s = p_e;
    
    while(*p_s != '\\') p_s--;
    len = strlen(p_s + 1);
    if(len > 16)
    {
        strcpy(file_name, p_s + 1 + len - 16);
    }
    else
    {
        strcpy(file_name, p_s + 1);
    }
    
    for(i = 0;i < p_rec->pWrite; i++)
    {
        if(strcmp(file_name,p_rec->rec[i].str) == 0 && row == p_rec->rec[i].row)
        {
            p_rec->rec[i].cnt++;
            flag = 0;
            break;
        }
    }
    
    if(flag)
    {
        strcpy(p_rec->rec[p_rec->pWrite].str, file_name);
        p_rec->rec[p_rec->pWrite].cnt = 1;
        p_rec->rec[p_rec->pWrite].row = row;
        p_rec->pWrite++;
    }
    
}

int main()
{
    T rec;
    char input[101];
    int row;
    int i,start;
    
    memset((void *)&rec,0,sizeof(rec));
    
    while(scanf("%s %d",input,&row) != EOF)
    {
        rec_one(&rec,input,row);
    }
    
    if(rec.pWrite >= 8)
    {
        start = rec.pWrite - 8;
    }
    else
    {
        start = 0;
    }
    
    for(i = start;i < rec.pWrite; i++)
    {
        printf("%s %d %d\n",rec.rec[i].str,rec.rec[i].row,rec.rec[i].cnt);
    }
    
    return 0;
}


发表于 2021-12-25 11:58:52 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct{
	char error_name[17];
	int  inumber;
	int error_count;
}ErrorInfo;

ErrorInfo err_info[100] = {0};
int i_errinfo_num = 0;

int main()
{
	char str[100] = {0};
	int iHangNum = 0, i = 0, j = 0;
	while(scanf("%s %d", str, &iHangNum) != EOF)
	{
		int is_find = 0;
		char *ptrTag = strrchr(str, '\\');
		
		if(ptrTag == NULL || ++ptrTag == NULL )
		{
			continue;
		}
		
		int istrLen = strlen(ptrTag);
		// printf("ilen:%d\n", istrLen);
		
		if(istrLen > 16)
		{
			ptrTag += (istrLen - 16);
		}
		
		// printf("ptrTag:%s\n", ptrTag);
		
		for(i = 0; i < i_errinfo_num; i++)
		{
			if(strcmp(ptrTag, err_info[i].error_name) == 0 && iHangNum == err_info[i].inumber)
			{
				err_info[i].error_count++;
				is_find = 1;
				break;
			}
		}
		
		if(is_find == 0)
		{
			strcpy(err_info[i_errinfo_num].error_name, ptrTag);
			err_info[i_errinfo_num].inumber = iHangNum;
			err_info[i_errinfo_num].error_count = 1;
			
			i_errinfo_num++;
		}
	}
	
	// printf("i_errinfo_num:%d\n", i_errinfo_num);
	
	if(i_errinfo_num > 8)
	{
		j = i_errinfo_num - 8;
	}
	
	for(i = j; i < i_errinfo_num; i++)
	{
		printf("%s %d %d\n", err_info[i].error_name, err_info[i].inumber, err_info[i].error_count);
	}	
	
	return 0;
}

发表于 2021-12-06 12:31:38 回复(2)
这题大坑,必须全记录,然后输出最后8条,而不能把前面的删掉。假设删掉了a记录,后面又出现a记录,你会追加,但实际应该算重复的。
发表于 2021-11-17 20:57:57 回复(0)
#include <stdio.h>
#include <string.h>
#define MAX 50
#define OUTMAX 8
#define FILENAMELEN 16

int main(){
    char record_name[MAX][FILENAME_MAX] = {0};
    int record_line[MAX] = {0};
    int record_freq[MAX] = {0};

    int next = 0;
    
    char file[1000];
    int line;

    int total = 0;

    while (scanf("%s %d", file, &line) == 2){
        int file_len = strlen(file);
        int count = 1;
        char recorded_filename[FILENAMELEN + 1];
        recorded_filename[FILENAMELEN] = '\000';
        while (file[file_len-count] != '\\' && count <= FILENAMELEN){
            recorded_filename[FILENAMELEN - count] = file[file_len - count];
            count++;
        }
        for (int i = 0; i < count; i++){
            recorded_filename[i] = recorded_filename[FILENAMELEN + 1 - count + i];
        }


        int exist = 0;
        for (int i = 0; i < MAX; i++){
            if (strcmp(record_name[i], recorded_filename) == 0 && record_line[i] == line){
                record_freq[i]++;
                exist = 1;
                break;
            }
        }
        if (exist == 0){
            next = next % MAX;
            record_freq[next] = 1;
            record_line[next] = line;
            strcpy(record_name[next], recorded_filename);
            next++;
            total++;
        }
        
    }


    total = total > MAX? MAX : total;
    for (int i = total - OUTMAX < 0? 0:total-OUTMAX; i < total; i++){
        int idx = (next + i) % total; 
        printf("%s %d %d\n", record_name[idx], record_line[idx], record_freq[idx]);
    }
}


发表于 2021-08-25 14:30:01 回复(0)

问题信息

难度:
7条回答 66135浏览

热门推荐

通过挑战的用户

查看代码