C语言学生成绩管理系统(简易版)

时间:2022-10-20 13:12:55
  1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 int readstudents(struct students stu[]); //读取学生信息
6 int readsexcode(struct sexcode sex[]); //读取性别代码
7 int readcollegecode(struct collegecode colle[]); //读取学院代码
8 void transform(struct students stu[],struct sexcode sexy[],struct collegecode colle[],int a,int b,int c); //录入
9 void namesort(struct students stu[],int a); //按照姓名排序
10 void averagesort(struct students stu[],int a); //平均分排序
11 void searchcollege(struct students stu[],int a); //按学院搜索学生
12 void namesearch(struct students stu[],int a); //按名字搜索
13 void printstudent(struct students stu[],struct sexcode sex[],struct collegecode colle[],int a,int b,int c); //输出
14 void change(struct students stu[],int a);
15 int passwordfun();
16 void passwordchange();
17 void averagefun(struct students stu[],int a);
18
19 struct students{
20 int num;
21 char name[20];
22 int sexnum;
23 char sex[4];
24 int collegenum;
25 char college[20];
26 int score[10];
27 float average;
28 };
29 struct sexcode{
30 int sexnum;
31 char sex[4];
32 };
33 struct collegecode{
34 int collegenum;
35 char college[20];
36 };
37
38
39 int main()
40 {
41 int a,b,c;
42 int choice;
43 struct students stu[100];
44 struct sexcode sexy[2];
45 struct collegecode colle[10];
46 int t = passwordfun();
47 if(t == 0){
48 printf("请使用正确密码重新登陆程序!\n");
49 return 0;
50 }
51 while(1){
52 system("cls");
53 printf("***********学生成绩系统菜单************ \n* 1. 原始文件读取 *\n* 2. 按姓名排序,输出 *\n* 3. 按平均成绩排序,输出 *\n* 4. 输出给定学院学生 *\n* 5. 修改给定学生成绩信息 *\n* 6. 按姓名查询学生,输出 *\n* 7. 修改系统密码 *\n* 0. 返回上一级菜单 *\n***************************************\n");
54 scanf("%d",&choice);
55 switch(choice){
56 case 1: system("cls");
57 a = readstudents(stu);
58 b = readsexcode(sexy);
59 c = readcollegecode(colle);
60 transform(stu,sexy,colle,a,b,c);
61 averagefun(stu,a);
62 printf("原始文件读取完毕!\n0. 返回上一级菜单 \n");
63 scanf("%d",&choice);
64 if(choice == 0) break;
65 case 2: system("cls");
66 namesort(stu,a);
67 printstudent(stu,sexy,colle,a,b,c);
68 printf("\n0. 返回上一级菜单 \n");
69 scanf("%d",&choice);
70 if(choice == 0) break;
71 case 3: system("cls");
72 averagesort(stu,a);
73 printstudent(stu,sexy,colle,a,b,c);
74 printf("\n0. 返回上一级菜单 \n");
75 scanf("%d",&choice);
76 if(choice == 0) break;
77 case 4: system("cls");
78 searchcollege(stu,a);
79 printf("\n0. 返回上一级菜单 \n");
80 scanf("%d",&choice);
81 if(choice == 0) break;
82 case 5: system("cls");
83 change(stu,a);
84 printf("修改成功!\n");
85 printf("\n0. 返回上一级菜单 \n");
86 scanf("%d",&choice);
87 if(choice == 0) break;
88 case 6: system("cls");
89 namesearch(stu,a);
90 printf("\n0. 返回上一级菜单 \n");
91 scanf("%d",&choice);
92 if(choice == 0) break;
93 case 7: system("cls");
94 passwordchange();
95 printf("\n0. 返回上一级菜单 \n");
96 scanf("%d",&choice);
97 if(choice == 0) break;
98 case 0: system("cls");
99 printf("欢迎使用,下次再见!\n");
100 system("pause");
101 exit(0);
102 }
103 }
104 return 0;
105 }
106 int readstudents(struct students stu[]) //读取学生信息
107 {
108 int i = 0;
109 FILE *fp;
110 fp = fopen("D:\\abc\\Student_Info.txt","r+");
111 if(fp == 0){
112 printf("can not open this file\n");
113 exit (0);
114 }
115 fscanf(fp,"%d",&stu[i].num);
116 while(!feof(fp)){
117 fscanf(fp,"%s%d%d",stu[i].name,&stu[i].sexnum,&stu[i].collegenum);
118 for(int j = 0 ;j < 10; j++ ){
119 fscanf(fp,"%d",&stu[i].score[j]);
120 }
121 i++;
122 fscanf(fp,"%d",&stu[i].num);
123 }
124 fclose(fp);
125 return i;
126 }
127 int readsexcode(struct sexcode sexy[]) //读取学生性别信息
128 {
129 int i = 0;
130 FILE *fp;
131 fp = fopen("D:\\abc\\S_Info.txt","r");
132 if(fp == 0){
133 printf("can not open this file\n");
134 exit (0);
135 }
136 fscanf(fp,"%d",&sexy[i].sexnum);
137 while(!feof(fp)){
138 fscanf(fp,"%s",sexy[i].sex);
139 i++;
140 fscanf(fp,"%d",&sexy[i].sexnum);
141 }
142 fclose(fp);
143 return i;
144 }
145 int readcollegecode(struct collegecode colle[]) //读取学生学院信息
146 {
147 int i = 0;
148 FILE *fp;
149 fp = fopen("D:\\abc\\C_Info.txt","r");
150 if(fp == 0){
151 printf("can not open this file\n");
152 exit (0);
153 }
154 fscanf(fp,"%d",&colle[i].collegenum);
155 while(!feof(fp)){
156 fscanf(fp,"%s",colle[i].college);
157 i++;
158 fscanf(fp,"%d",&colle[i].collegenum);
159 }
160 fclose(fp);
161 return i;
162 }
163 void averagefun(struct students stu[],int a) //平均分
164 {
165 for(int i = 0; i < a; i++){
166 int sum = 0;
167 for(int j = 0; j < 10; j++){
168 sum = sum + stu[i].score[j];
169 }
170 stu[i].average = 1.0 * sum / 10;
171 }
172 return ;
173 }
174 void namesort(struct students stu[],int a) //按姓名排序
175 {
176 for(int i = 0; i < a-1; i++){
177 for(int j = i+1; j < a ; j++){
178 if(strcmp(stu[i].name,stu[j].name) < 0){
179 struct students temp = stu[i];
180 stu[i] = stu[j];
181 stu[j] = temp;
182 }
183 }
184 }
185 return ;
186 }
187 void averagesort(struct students stu[],int a) // 按平均分排序
188 {
189 for(int i = 0; i < a-1; i++){
190 for(int j = i+1; j < a ; j++){
191 if(stu[i].average < stu[j].average){
192 struct students temp = stu[i];
193 stu[i] = stu[j];
194 stu[j] = temp;
195 }
196 }
197 }
198 return ;
199 }
200 void printstudent(struct students stu[],struct sexcode sexy[],struct collegecode colle[],int a,int b,int c) //输出信息
201 {
202 for(int i = 0;i < a; i++){
203 printf("%d%8s%4s%12s",stu[i].num,stu[i].name,stu[i].sex,stu[i].college);
204 for(int j = 0; j < c; j++){
205 printf("%4d",stu[i].score[j]);
206 }
207 printf("%7.2f\n",stu[i].average);
208 }
209 return ;
210 }
211 void transform(struct students stu[],struct sexcode sexy[],struct collegecode colle[],int a,int b,int c) //修改信息
212 {
213 for(int i = 0; i < a; i++){
214 for(int j = 0; j < b; j++){
215 if(stu[i].sexnum == sexy[j].sexnum){
216 strcpy(stu[i].sex,sexy[j].sex);
217 break;
218 }
219 }
220 for(int j = 0; j < c; j++){
221 if(stu[i].collegenum == (colle[j].collegenum - 1)){
222 strcpy(stu[i].college,colle[j].college);
223 break;
224 }
225 }
226 }
227 }
228
229 void searchcollege(struct students stu[],int a) //查找学院并输出
230 {
231 char collegename[20];
232 printf("请输入需要查找的学院的名称:\n") ;
233 scanf("%s",collegename);
234 for(int i = 0;i < a; i++){
235 if(strcmp(stu[i].college,collegename) == 0){
236 printf("%d%8s%4s%12s",stu[i].num,stu[i].name,stu[i].sex,stu[i].college);
237 for(int j = 0; j < 10; j++){
238 printf("%4d",stu[i].score[j]);
239 }
240 printf("%7.2f\n",stu[i].average);
241 }
242 }
243 return ;
244 }
245 void namesearch(struct students stu[],int a) //按姓名查找
246 {
247 printf("请输入需要查找的学生的姓名:\n");
248 char name[20];
249 scanf("%s",name);
250 for(int i = 0;i < a; i++){
251 if(strcmp(stu[i].name,name) == 0){
252 printf("%d%8s%4s%12s",stu[i].num,stu[i].name,stu[i].sex,stu[i].college);
253 for(int j = 0; j < 10; j++){
254 printf("%4d",stu[i].score[j]);
255 }
256 printf("%7.2f\n",stu[i].average);
257 }
258 }
259 return ;
260 }
261 void change(struct students stu[],int a) //按名字搜索修改分数
262 {
263 char name[20];
264 printf("请输入需要修改成绩的学生的姓名:\n");
265 scanf("%s",name);
266 printf("请输入改学生各科成绩,不需要修改的按原样输入:\n");
267 for(int i = 0;i < a; i++){
268 if(strcmp(stu[i].name,name) == 0){
269 for(int j = 1; j <= 10; j++){
270 printf("第%d门成绩: ",j);
271 scanf("%d",&stu[i].score[j-1]);
272 printf("\n");
273 }
274 }
275 }
276 int i = 0;
277 FILE *fp;
278 fp = fopen("D:\\abc\\Student_Info.txt","w");
279 if(fp == 0){
280 printf("can not open this file\n");
281 exit (0);
282 }
283 while(i<100){
284 fprintf(fp,"%d %s %d %d",stu[i].num,stu[i].name,stu[i].sexnum,stu[i].collegenum);
285 for(int j = 0 ;j < 10; j++ ){
286 fprintf(fp," %d",stu[i].score[j]);
287 if(j == 9){
288 fprintf(fp,"\n");
289 }
290 }
291 i++;
292 }
293 fclose(fp);
294 return ;
295 }
296 int passwordfun() //密码
297 {
298 char password_1[20],password_2[20],password_3[20];
299 int i = 0;
300 FILE *fp;
301 fp = fopen("D:\\abc\\password.txt","r");
302 if(fp == 0){
303 printf("can not open this file\n");
304 exit (0);
305 }
306 fscanf(fp,"%s",password_2);
307 fclose(fp);
308 if(password_2[i] == '\0'){ //当检测到密码文本为空时
309 printf("欢迎使用学生成绩管理系统!\n首次使用系统,您可以直接进入主菜单,进入主菜单后请及时修改密码!\n");
310 system("pause");
311 return 1;
312 }
313 else
314 {
315 printf("请输入系统密码:\n");
316
317 //保存输入的密码
318 char s[20];
319 int t,n_,x;
320 for(x=0;x<20;x++){
321 s[x]=getch();
322 if(s[x]=='\r') break;
323 printf("*");
324 }
325 s[x]='\0';
326 for(t=0;t<20;t++){ //将输入的密码存放在password_1中
327 password_1[t]=s[t];
328 }
329 for(i = 0; password_2[i] != '\0'; i++){
330 if(password_2[i] <= 127 && password_2[i] >= 4){ //password_2为真实密码
331 password_3[i] = password_2[i] - 3; //密码密文加密(+3)
332 }else{
333 password_3[i] = 127 - password_3[i];
334 } //password_3为按算法处理后的真实密码
335 }
336 password_3[i] = '\0';
337 if(strcmp(password_1,password_3) == 0){
338 return 1;
339 }else{
340 printf("密码错误!\n程序结束!请使用正确密码重新登入程序!\n");
341 system("pause");
342 return 0;
343 }
344 }
345 }
346 void passwordchange() //密码修改
347 {
348 FILE *fp;
349 char newpassword_1[20];
350 char newpassword_2[20];
351 int i = 0;
352 printf("请输入新的密码:\n");
353 scanf("%s",newpassword_1);
354 for(i = 0; newpassword_1[i] != '\0'; i++){
355 if(newpassword_1[i] + 3 <= 127){
356 newpassword_2[i] = newpassword_1[i] + 3;
357 }else{
358 newpassword_2[i] = newpassword_1[i] + 3 -127;
359 }
360 }
361 newpassword_2[i] = '\0';
362 fp = fopen("D:\\abc\\password.txt","w");
363 if(fp == 0){
364 printf("can not open this file\n");
365 exit (0);
366 }
367 fprintf(fp,"%s",newpassword_2);
368 fclose(fp);
369 printf("密码修改成功!\n");
370 return ;
371 }