1186: Delete Record (Structure Topic)
Item description
There is a student grade sheet, including student number, name, and grades of 3 courses. Please implement the following delete function: enter a student's student number to delete all the information of the student.
Input
First, enter an integer n (1<=n<=100), indicating the number of students;
then input n lines, each line contains the information of a student: student number (12 digits), name (without spaces and no more than 20 digits) ), and 3 integers, indicating the grades of 3 courses, and the data are separated by spaces.
Enter a student number num in the last line.
Output
If the student ID to be deleted does not exist, output "error!"; otherwise, output all records after the student is deleted.
Sample Enter Copy
3
541207010188 ZHANGLING 78 95 55
541207010189 WANGLI 87 99 88
541207010190 Fangfang 68 76 75
541207010188
Sample Output Copy
541207010189 WANGLI 87 99 88
541207010190 Fangfang 68 76 75
Source / Classification
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 102
typedef struct student{
char id[13];
char name[21];
int a,b,c;
}student;
int main(){
int n,i,f=0;
student stu[N];
char id[13];
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s %s %d %d %d",stu[i].id,stu[i].name,&stu[i].a,&stu[i].b,&stu[i].c);
}
scanf("%s",id);
for(i=0;i<n;i++){
//学号相同,后面的前移1位
if(strcmp(stu[i].id,id)==0) {
i++;
f=1;//标记是否找到学号
for(i;i<n;i++){
stu[i-1]=stu[i];
}
}
}
//没有找到
if(f==0) {
printf("error!\n");
return 0;
}
for(i=0;i<n-1;i++){
printf("%s %s %d %d %d\n",stu[i].id,stu[i].name,stu[i].a,stu[i].b,stu[i].c);
}
return 0;
}