OR43. 简单错误记录
描述
开发一个简单错误记录功能小模块,能够记录出错的代码所在的文件名称和行号。输入描述
一行或多行字符串。每行包括带路径文件名称,行号,以空格隔开。输出描述
将所有的记录统计并将结果输出,格式:文件名代码行数数目,一个空格隔开,如: fpgadrive.c 1325 1示例1
输入:
E:\V1R2\product\fpgadrive.c 1325
输出:
fpgadrive.c 1325 1
C 解法, 执行用时: 1ms, 内存消耗: 368KB, 提交时间: 2021-05-15
#include <stdio.h> struct Node { char filename[200]; int rownum; int sum; }; struct Node records[100]; int length = 0; int RecordsAt(char filename[], int rownum); void InsertRecords(char filename[], int rownum); int main() { char str[500]; char filename[200]; int rownum; int index; while (scanf("%s %d", str, &rownum) != EOF) { int i; for (i = 0; str[i] != '\0'; i++); while (str[i] != '\\') i--; i++; index = 0; while (str[i] != '\0') filename[index++] = str[i++]; filename[index] = '\0'; int at = RecordsAt(filename, rownum); if (at == -1) InsertRecords(filename, rownum); else records[at].sum++; } int flag = 1; for (int i = 0; i < length - 1 && flag; i++) { flag = 0; for (int j = 0; j < length - i -1; j++) { if (records[j].sum < records[j + 1].sum) { struct Node temp; temp = records[j]; records[j] = records[j + 1]; records[j + 1] = temp; flag = 1; } } } for (int i = 0; i < length && i < 8; i++) { int j; for (j = 0; records[i].filename[j] != '\0'; j++); if (j >= 16) { j = j - 16; while (records[i].filename[j] != '\0') printf("%c", records[i].filename[j++]); printf(" %d %d\n", records[i].rownum, records[i].sum); } else { printf("%s %d %d\n", records[i].filename, records[i].rownum, records[i].sum); } } return 0; } int RecordsAt(char filename[], int rownum) { int i; for (i = 0; i < length; i++) { int j; for (j = 0; filename[j] != '\0' && records[i].filename[j] != '\0'; j++) { if (filename[j] != records[i].filename[j]) break; } if (filename[j] == '\0' && records[i].filename[j] == '\0') { if (rownum == records[i].rownum) return i; } } return -1; } void InsertRecords(char filename[], int rownum) { records[length].rownum = rownum; records[length].sum = 1; int i; for (i = 0; filename[i] != '\0'; i++) { records[length].filename[i] = filename[i]; } records[length].filename[i] = '\0'; length++; }
C 解法, 执行用时: 1ms, 内存消耗: 376KB, 提交时间: 2020-07-14
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct reco { char name[1000]; char errnum[20]; int inipos; int displaypos; int count; }Record; Record a[1000]; void QSort(Record a[],int low,int high) { if(low<high) { Record Pivot=a[low],temp; int start=low,last=high; int pos=low+1; while(start<last) { if(Pivot.count>a[pos].count) { temp=a[last]; a[last]=a[pos]; a[pos]=temp; last--; } else if(Pivot.count<a[pos].count) a[start++]=a[pos++]; else { if(Pivot.inipos>a[pos].inipos) a[start++]=a[pos++]; else { temp=a[last]; a[last]=a[pos]; a[pos]=temp; last--; } } } a[start]=Pivot; QSort(a, low, start-1); QSort(a, start+1, high); } } int IndexName(char file[]) { int i; for(i=strlen(file)-2;i>=0;i--) { if(file[i]=='\\') return i; } return -1; } int main() { char *name; char file[1000]; char ernum[20]; int hasflag; int recent=0; int len; while(scanf("%s%s",file,ernum)!=EOF) { name=file+IndexName(file)+1; hasflag=0; for(int i=0;i<recent;i++) { if(0==strcmp(a[i].name,name)&&0==strcmp(a[i].errnum,ernum)) { a[i].count++; hasflag=1; break; } } if(hasflag==1) continue; strcpy(a[recent].name,name); strcpy(a[recent].errnum,ernum); a[recent].count=1; a[recent].inipos=recent; len=strlen(name); if(len>16) a[recent].displaypos=len-16; else a[recent].displaypos=0; recent++; } QSort(a,0,recent-1); recent=recent>8?8:recent; for(int i=0;i<recent;i++) printf("%s %s %d\n",&a[i].name[a[i].displaypos],a[i].errnum,a[i].count); return 0; }