写的作业 用结构体统计学生信息


结构体作

1.定义一个acmer结构体,包括以下信息:姓名,学号,手机号,做题数,出生日期,其中出生日期date也是一个结构体,包括年、月、日
2.
建立结构体数组,实现对多个同学的信息输入,输出
3.
实现简单的统计功能,比如统计做题数大于150的同学并输出其完整信息
4.
实现查找功能,包括按姓名、学号查找
5.
实现信息修改功能
6.
按做题数目进行排序(选作)
7.
其他功能可以自由扩展,多多益善 ^_^
8.
程序一个函数实现一个功能
9.
代码测试成功后贴在论坛上,大家互相学习借鉴


  1 #include<stdio.h>
2 #include<string.h>
3 struct data
4 {
5 int year ;
6 int month ;
7 int day ;
8 } ;
9 struct acmer
10 {
11 int number ;
12 char name[20] ;
13 char sex ;
14 char stunum[20] ;
15 char phonum[20] ;
16 struct data birth ;
17 };
18 void menu()
19 {
20 printf("1输出\n") ;
21 printf("2统计\n");
22 printf("3查找\n");
23 printf("4信息修改\n") ;
24 printf("5排名\n") ;
25 printf("6添加\n");
26 printf("7删除\n");
27 printf("输入数字选择想要实现的功能,按0结束程序: ");
28 }
29 void input(int i, struct acmer stu[] )
30 {
31 printf("姓名: ") ;
32 scanf("%s", stu[i].name) ;
33 printf("学号: ") ;
34 scanf("%s", stu[i].stunum) ;
35 printf("手机号: ") ;
36 scanf("%s", stu[i].phonum) ;
37 printf("做题数: ") ;
38 scanf("%d", &stu[i].number) ;
39 printf("出生日期 年月日 : ") ;
40 scanf("%d%d%d", &stu[i].birth.year,&stu[i].birth.month,&stu[i].birth.day) ;
41 }
42 void output(int i,struct acmer stu[] )
43 {
44 printf("姓名:%s ", stu[i].name) ;
45 printf("学号:%s ", stu[i].stunum) ;
46 printf("手机号:%s ", stu[i].phonum) ;
47 printf("做题数:%d ", stu[i].number) ;
48 printf("出生日期:%d %d %d\n", stu[i].birth.year,stu[i].birth.month,stu[i].birth.day) ;
49 }
50 int add(int n,struct acmer stu[] )
51 {
52 int k,i;
53 printf("请输入要添加学生的数目:");
54 scanf("%d", &k);
55 for(i = n+1 ; i <= n+k ; i++)
56 {
57 input(i,stu);
58 }
59 return n+k ;
60 }
61 int cancel(int n,struct acmer stu[] )
62 {
63 char x[20];
64 int i,j,flag,t;
65 t = n ;
66 printf("请输入要删除学生的学号(按000结束删除):");
67 while(scanf("%s",x)!=EOF)
68 {
69 flag = 0;
70 if(strcmp(x,"000") == 0)
71 break;
72 for(i = 1 ; i <= n ; i++)
73 if(strcmp(x,stu[i].stunum) == 0)
74 {
75 for(j = i+1 ;j<=n ; j++)
76 stu[i] = stu[i+1];
77 flag = 1;
78 t--;
79 break;
80 }
81 if(flag == 0)
82 printf("没有此人的信息\n");
83 }
84 return t ;
85 }
86 void sort(int n,struct acmer stu[])
87 {
88 struct acmer t ;
89 int i, j;
90 for(i = 1;i <= n-1 ;i++)
91 for(j = 1 ; j <= n-i;j++)
92 if(stu[j].number<stu[j+1].number)
93 {
94 t = stu[j];
95 stu[j] = stu[j+1] ;
96 stu[j+1] = t ;
97 }
98 }
99 void stat(int n,struct acmer stu[])
100 {
101 int k,i,x ;
102 printf("1:做题数超过k的学生\n") ;
103 printf("2:排名前k的学生\n");
104 printf("请选择要实现的统计功能,按4结束统计:\n");
105 while(scanf("%d", &x),x!=4)
106 {
107 printf("请输入k: ");
108 scanf("%d", &k);
109 switch(x)
110 {
111 case 1:for(i = 1 ; i <= n ; i++)
112 if(stu[i].number>k)
113 output(i,stu) ;break;
114 case 2:sort(n,stu);
115 for(i = 1 ; i <= k ; i++)
116 output(i,stu);break;
117 default:printf("error\n") ;
118 }
119 }
120 }
121 void amend(int n,struct acmer stu[] )
122 {
123 int i,k,flag = 0 ;
124 char x[20];
125 printf("请输入要修改的学生的学号: ");
126 scanf("%s",x);
127 for(i = 1;i <= n;i++)
128 {
129 if(strcmp(x,stu[i].stunum)==0)
130 {
131 flag =1;
132 printf("请输入你要修改的信息:1姓名 2学号 3手机 4做题数 5出生日期\n");
133 scanf("%d", &k);
134 switch(k)
135 {
136 case 1:printf("姓名: ") ;scanf("%s", stu[i].name) ;break;
137 case 2:printf("学号: ") ;scanf("%s", stu[i].stunum) ;break;
138 case 3:printf("手机号: ") ;scanf("%s", stu[i].phonum) ;break;
139 case 4:printf("做题数: ") ;scanf("%d", &stu[i].number) ;break;
140 case 5:printf("出生日期 年月日 : ") ;
141 scanf("%d%d%d", &stu[i].birth.year,&stu[i].birth.month,&stu[i].birth.day) ;break;
142 default:printf("error\n");
143 }
144 }
145 break;
146 }
147 if(flag == 0)
148 printf("没有此人的信息\n");
149 else
150 printf("修改成功\n");
151 }
152 void search(int n,struct acmer stu[])
153 {
154 int k,i,flag=1;
155 char x[20];
156 printf("1按姓名查找");
157 printf("2按学号查找");
158 printf("请选择查找方式:");
159 scanf("%d", &k);
160 if(k == 1)
161 {
162 printf("请输入要查找学生的姓名:");
163 scanf("%s", x);
164 for(i = 1 ; i <= n;i++)
165 if(strcmp(x, stu[i].name) == 0)
166 {
167 flag = 0;
168 output(i,stu);
169 break;
170 }
171 if(flag == 1)
172 printf("没有此人的信息\n");
173 }
174 if(k == 2)
175 {
176 printf("请输入要查找学生的学号:");
177 scanf("%s", x);
178 for(i = 1 ; i <= n;i++)
179 if(strcmp(x, stu[i].stunum) == 0)
180 {
181 flag = 0;
182 output(i,stu);
183 break;
184 }
185 if(flag == 1)
186 printf("没有此人的信息\n");
187 }
188 }
189 int main()
190 {
191 struct acmer stu[100] ;
192 int n, i, k ;
193 printf("请输入要统计的学生数目: ") ;
194 scanf("%d", &n) ;
195 printf("请输入学生信息:\n") ;
196 for(i = 1 ; i <= n ; i++)
197 input(i, stu) ;
198 menu();
199 while(scanf("%d", &k),k!=0)
200 {
201 switch(k)
202 {
203 case 1:for(i = 1;i <= n ; i++)
204 {output(i, stu);}menu();break;
205 case 2:stat(n,stu);menu();break;
206 case 3:search(n,stu);menu();break;
207 case 4:amend(n,stu);menu();break;
208 case 5:sort(n,stu);
209 for(i = 1;i <= n ; i++)
210 {
211 printf("[%d]",i);
212 output(i, stu);
213 } menu();break;
214 case 6:n = add(n,stu); menu();break;
215 case 7:n=cancel(n,stu);menu();break;
216 default:printf("error\n") ;menu();
217 }
218 }
219 return 0 ;
220 }
221


 

原文地址:https://www.cnblogs.com/shangyu/p/2344085.html