题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> typedef struct{ char filename[17]; int line; uint8_t count; }record_t; //传入指向文件名的最后一个字符的指针 char* _get_filename_start(char * str) { char *p = NULL; int i = 0; for (i = 1; i < 16; i++) { if (*(str-i) == '\\') { break; } } return str-i+1; } //10:42-11.13 半小时 int main() { int num = 0; char file_path[101] = {0}; char line_buf[101] = {0}; record_t rec_list[100]; memset(rec_list, 0, sizeof(rec_list)); while (fgets(line_buf, sizeof(line_buf), stdin) != NULL) { line_buf[strcspn(line_buf, "\r")] = 0; line_buf[strcspn(line_buf, "\n")] = 0; //printf("line_buf:%s\n", line_buf); //获取输出 char *p = strchr(line_buf, ' '); int line = atoi(p+1); *p = 0; char *filename = _get_filename_start(p-1); //printf("filename:%s\n", filename); //printf("line:%d\n", line); //保存输入,需要判断有没有出现过 int i = 0; char skip = 0; for (i = 0; i < num; i++) { if (strcmp(rec_list[i].filename, filename) == 0 && rec_list[i].line == line) { rec_list[i].count++; skip = 1; break; } } if (skip) { continue; } //保存未出现的记录 strncpy(rec_list[num].filename, filename, strlen(filename)); rec_list[num].line = line; rec_list[num].count++; num++; } //输出后面8条 int i = num >= 8 ? 8:num; for (; i > 0; i--) { printf("%s %d %d\n", rec_list[num-i].filename, rec_list[num-i].line, rec_list[num-i].count); } return 0; }
樯橹灰飞烟灭