结构体(添加和删除学生信息)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| #include <stdio.h>
#include <string.h>
#define N 5
typedef struct student {
char name[10];
char sex;
int score;
} Student;
int del_p(Student *q,char *a);
main()
{
int i=0,irest=N;
Student s[N]= {0};
char delname[10]= {'\0'};
printf("Input data:\n");
for(i=0; i<N; i++)
scanf("%s %c%d",s[i].name,&s[i].sex,&s[i].score);
printf("All students are:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
getchar();
printf("Input the name to be deleted:\n");
gets(delname);
irest=del_p(s,delname); /* 返回所剩记录的个数 */
printf("The rest after deleting:\n"); /* 输出删除后的记录 */
for(i=0; i<irest; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
getchar();
}
int del_p(Student *q,char *a)
{
int i=0,j=0;
for(i=0; i<N; i++)
if(strcmp(q[i].name,a)==0) break; /* 找到要删除的记录就退出 */
if(i<N) {
for(j=i; j<N-1; j++) q[j]=q[j+1]; /* 将后面的记录向前移 */
return N-1;
} else return N;
} |
#include <stdio.h>
#include <string.h>
#define N 5
typedef struct student {
char name[10];
char sex;
int score;
} Student;
int del_p(Student *q,char *a);
main()
{
int i=0,irest=N;
Student s[N]= {0};
char delname[10]= {'\0'};
printf("Input data:\n");
for(i=0; i<N; i++)
scanf("%s %c%d",s[i].name,&s[i].sex,&s[i].score);
printf("All students are:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
getchar();
printf("Input the name to be deleted:\n");
gets(delname);
irest=del_p(s,delname); /* 返回所剩记录的个数 */
printf("The rest after deleting:\n"); /* 输出删除后的记录 */
for(i=0; i<irest; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
getchar();
}
int del_p(Student *q,char *a)
{
int i=0,j=0;
for(i=0; i<N; i++)
if(strcmp(q[i].name,a)==0) break; /* 找到要删除的记录就退出 */
if(i<N) {
for(j=i; j<N-1; j++) q[j]=q[j+1]; /* 将后面的记录向前移 */
return N-1;
} else return N;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| #include <stdio.h>
#include <string.h>
#define N 5
typedef struct student
{ char name[10];
char sex;
int score;
}Student;
void add_p(Student *q,char *a,Student w);
main()
{ int i=0;
Student s[N+1]={0},new={0};
char appointed[10]={'\0'};
printf("Input data:\n");
for(i=0; i<N; i++)
scanf("%s %c%d",s[i].name,&s[i].sex,&s[i].score);
printf("All students are:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
getchar();
printf("Input the appointed name:\n");
gets(appointed);
printf("Input the data to be added:\n");
scanf("%s %c%d",new.name,&new.sex,&new.score);
add_p(s,appointed,new);
printf("The records after adding:\n");
for(i=0; i<N+1; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
}
void add_p(Student *q,char *a,Student w)
{ int i=0,j=0;
for(i=0; i<N; i++)
if(strcmp(q[i].name,a)==0) break;
for(j=N-1; j>=i; j--)
q[j+1]=q[j];
q[i]=w;
} |
#include <stdio.h>
#include <string.h>
#define N 5
typedef struct student
{ char name[10];
char sex;
int score;
}Student;
void add_p(Student *q,char *a,Student w);
main()
{ int i=0;
Student s[N+1]={0},new={0};
char appointed[10]={'\0'};
printf("Input data:\n");
for(i=0; i<N; i++)
scanf("%s %c%d",s[i].name,&s[i].sex,&s[i].score);
printf("All students are:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
getchar();
printf("Input the appointed name:\n");
gets(appointed);
printf("Input the data to be added:\n");
scanf("%s %c%d",new.name,&new.sex,&new.score);
add_p(s,appointed,new);
printf("The records after adding:\n");
for(i=0; i<N+1; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
}
void add_p(Student *q,char *a,Student w)
{ int i=0,j=0;
for(i=0; i<N; i++)
if(strcmp(q[i].name,a)==0) break;
for(j=N-1; j>=i; j--)
q[j+1]=q[j];
q[i]=w;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| #include <stdio.h>
#define N 5
typedef struct students {
char name[10];
char sex;
int score;
} Student;
void sort_p(Student *q);
main()
{
int i=0;
Student s[N]= {0};
printf("Input data:\n");
for(i=0; i<N; i++)
scanf("%s %c%d",s[i].name,&s[i].sex,&s[i].score);
printf("All students are:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
sort_p(s);
printf("The recornds after sorting:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
}
void sort_p(Student *q)
{
Student t= {0};
int i=0, j=0, k=0;
for(i=0; i<N-1; i++) {
k=i;
for(j=i+1; j<N; j++)
if(q[k].score<q[j].score) k=j;
t=q[i]; q[i]=q[k]; q[k]=t;
}
} |
#include <stdio.h>
#define N 5
typedef struct students {
char name[10];
char sex;
int score;
} Student;
void sort_p(Student *q);
main()
{
int i=0;
Student s[N]= {0};
printf("Input data:\n");
for(i=0; i<N; i++)
scanf("%s %c%d",s[i].name,&s[i].sex,&s[i].score);
printf("All students are:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
sort_p(s);
printf("The recornds after sorting:\n");
for(i=0; i<N; i++)
printf("%13s%3c%4d\n",s[i].name,s[i].sex,s[i].score);
}
void sort_p(Student *q)
{
Student t= {0};
int i=0, j=0, k=0;
for(i=0; i<N-1; i++) {
k=i;
for(j=i+1; j<N; j++)
if(q[k].score<q[j].score) k=j;
t=q[i]; q[i]=q[k]; q[k]=t;
}
}