diff --git a/5.c b/5.c new file mode 100644 index 0000000..23fbb0f --- /dev/null +++ b/5.c @@ -0,0 +1,85 @@ +#include +#include + +struct s +{ + char id[20]; + int class; // 学号 + char name[20]; + float m, p, e; // 总成绩 +}; + +void deleteAndShowStudent() +{ + struct s stu[3] = { + {"10001", 11, "Zhang", 99.5, 88.5, 89.5}, + {"10002", 12, "Yang", 77.9, 56.5, 87.5}, + {"10003", 11, "Liang", 92.5, 99.0, 60.5} + }; + struct s stu2[3] = { + {"10001", 11, "Zhang", 99.5, 88.5, 89.5}, + {"10002", 12, "Yang", 77.9, 56.5, 87.5}, + {"10003", 11, "Liang", 92.5, 99.0, 60.5} + }; + int numStudents = 3; + + char input[20]; + scanf("%s", input); + int found = 0; + int i,j; + for ( i = 0; i < numStudents; i++) + { + if (strcmp(stu[i].id, input) == 0 || strcmp(stu[i].name, input) == 0) + { + found = 1; + for ( j = i; j < numStudents - 1; j++) + { + stu[j] = stu[j + 1]; + } + numStudents--; + break; + } + } + + if (found) + { + + for ( i = 0; i < numStudents; i++) + { + printf("%s %d %s %.1f %.1f %.1f\n", stu[i].id, stu[i].class, stu[i].name, stu[i].m, stu[i].p, stu[i].e); + } + char confirm[5]; + printf("Are you sure to delete? (yes/no): "); + fflush(stdin); // 清空输入缓冲区 + fgets(confirm, sizeof(confirm), stdin); // 使用fgets来获取用户输入 + confirm[strcspn(confirm, "\n")] = '\0'; // 去掉输入中的换行符 + if (strcmp(confirm,"y") == 0) + { + return; + } + else if (strcmp(confirm,"n") == 0) + { + for ( i = 0; i < numStudents+1; i++) + { + printf("%s %d %s %.1f %.1f %.1f\n", stu2[i].id, stu2[i].class, stu2[i].name, stu2[i].m, stu2[i].p, stu2[i].e); + } + return; + } + } + else + { + for (i = 0; i < numStudents; i++) + { + printf("%s %d %s %.1f %.1f %.1f\n", stu[i].id, stu[i].class, stu[i].name, stu[i].m, stu[i].p, stu[i].e); + } + return; + } +} + +int main() +{ + deleteAndShowStudent(); + // getchar(); + return 0; +} +