本程序可实现记录的输入,输出,查找,修改,文件存储,文件打开的功能。

  1
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stack>
  4 #include<string.h>
  5 #include<queue>
  6 #include<algorithm>
  7 #include<stdlib.h>
  8 #include<map>
  9 #include<vector>
 10 #define PI acos(-1.0)
 11 using namespace std;
 12 typedef long long ll;
 13 map<ll,ll>::iterator it;
 14 void file_save();
 15 void student_insert();
 16 FILE *fp;
 17 struct student
 18 {
 19     int num;
 20     char name[15];
 21     int score[3];
 22     double avr;
 23     struct student *next;
 24     struct student *before;
 25 };
 26 struct student *p;
 27 int num_stu=0;
 28 void student_input()//学生信息输入
 29 {
 30     int i,j;
 31     char ch;
 32     puts("请逐个输入学生的基本信息");
 33     struct student *head,*tail,*q;
 34     head=(struct student*)malloc(sizeof(struct student));
 35     head->next=NULL;
 36     head->before=NULL;
 37     tail=head;
 38     while(1)
 39     {
 40         p=(struct student*)malloc(sizeof(struct student));
 41         scanf("%d %s %d %d %d",&p->num,&p->name,&p->score[0],&p->score[1],&p->score[2]);
 42         p->avr=(p->score[0]+p->score[1]+p->score[2])/3.0;
 43         p->next=NULL;
 44         p->before=NULL;
 45         tail->next=p;
 46         p->before=tail;
 47         tail=p;
 48         puts("若继续请输入\'y\',否则输入‘n\'");
 49         num_stu++;
 50         scanf(" %c",&ch);
 51         if(ch==\'n\'||ch==\'N\')
 52             break;
 53     }
 54 
 55     p=head;
 56     file_save();
 57 }
 58 void student_output()//学生信息输出
 59 {
 60     int i,j;
 61     struct student *q;
 62     q=p->next;
 63     while(q)
 64     {
 65         printf("%d %s %d %d %d %lf\n",q->num,q->name,q->score[0],q->score[1],q->score[2],q->avr);
 66         q=q->next;
 67     }
 68 }
 69 void student_order()//按成绩排序
 70 {
 71     int i,j;
 72     struct student *q,*t,*tail,*head,*ppq;
 73     head=p;
 74     ppq=p;
 75     q=head;
 76     for(i=0; i<num_stu-1; i++)
 77     {
 78         q=head->next;
 79         while(q->next!=NULL)
 80         {
 81             t=q->next;
 82             printf("  %d\n",t->num);
 83             if(q->avr<t->avr)
 84             {
 85 
 86                 tail=q->before;
 87                 tail->next=t;
 88                 q->next=t->next;
 89                 t->next=q;
 90             }
 91             else
 92                 q=q->next;
 93         }
 94         head=head->next;
 95     }
 96 
 97     while(ppq->next!=NULL)
 98     {
 99         t=ppq->next;
100         t->before=ppq;
101         ppq=ppq->next;
102 
103     }
104     puts("");
105     student_output();
106 }
107 
108 void file_save()//存储信息
109 {
110     char str[123];
111     struct student *head,*q;
112     fp=fopen("c:\\111\\富哥.txt","w");
113     head=p->next;
114     while(head)
115     {
116         fprintf(fp,"%d %s %d %d %d %lf\n",head->num,head->name,head->score[0],head->score[1],head->score[2],head->avr);
117         head=head->next;
118     }
119     fclose(fp);
120 }
121 void file_open(int ppq)//打开文件,读取学生信息
122 {
123     struct student *head,*q,*tail,*qq;
124     char str[123];
125     p=(struct student*)malloc(sizeof(struct student));
126     p->next=NULL;
127     tail=p;
128     fp=fopen("c:\\111\\富哥.txt","r");
129     while(1)
130     {
131         qq=(struct student*)malloc(sizeof(struct student));
132         fscanf(fp,"%d %s%d %d %d %lf",&qq->num,&qq->name,&qq->score[0],&qq->score[1],&qq->score[2],&qq->avr);
133         if(feof(fp))
134             break;
135         qq->next=NULL;
136         tail->next=qq;
137         tail=qq;
138     }
139     if(ppq)
140         return;
141     q=p->next;
142     while(q)
143     {
144         printf("%d %s %d %d %d %lf\n",q->num,q->name,q->score[0],q->score[1],q->score[2],q->avr);
145         q=q->next;
146     }
147     fclose(fp);
148 }
149 void stuednt_insert()//学生信息插入
150 {
151     int i,j,m;
152     char ch;
153     file_open(1);
154     puts("如果你要添加的学生的基本信息请按1,否则按任意键结束");
155     scanf("%d",&m);
156     if(m!=1)
157         return ;
158     while(1)
159     {
160         struct student *t,*s,*q,*head;
161         t=(struct student*)malloc(sizeof(struct student));
162         scanf("%d %s %d %d %d",&t->num,&t->name,&t->score[0],&t->score[1],&t->score[2]);
163         t->avr=(t->score[0]+t->score[1]+t->score[2])/3.0;
164         t->next=NULL;
165         int flag=0,num=0;
166         q=p->next;
167         while(q)
168         {
169             num++;
170             if(t->avr>=q->avr)
171             {
172                 head=q->next;
173                 s=q->before;
174                 s->next=t;
175                 t->next=q;
176                 q->before=t;
177                 t->before=s;
178                 q->next=head;
179                 break;
180             }
181             if(num==num_stu)
182             {
183                 q->next=t;
184                 t->before=q;
185                 break;
186 
187             }
188             q=q->next;
189         }
190         num_stu++;
191         puts("若继续请输入\'y\',否则输入‘n\'");
192         scanf(" %c",&ch);
193         q=p->next;
194         if(ch==\'n\'||ch==\'N\')
195             break;
196     }
197     puts("");
198     student_output();
199     file_save();
200 }
201 void student_Delete()//记录删除
202 {
203     int i,j,m;
204     file_open(1);
205     char ch;
206     puts("请输入你要删除的学生的学号");
207     int num,flag=0;
208     struct student *t,*s,*q,*tail;
209     while(1)
210     {
211         int l=0;
212         scanf("%d",&num);
213         q=p->next;
214         while(q->next!=NULL)
215         {
216             tail=q->next;
217             if(q->num==num)
218             {
219                 flag=1;
220                 t=q->before;
221                 t->next=tail;
222                 tail->before=t;
223                 break;
224             }
225             struct student *t,*s,*q,*tail;
226             q=p->next;
227             q=q->next;
228         }
229         num_stu--;
230         if(!flag)
231         {
232             t=q->before;
233             t->next=NULL;
234         }
235         puts("若继续请输入\'y\',否则输入‘n\'");
236         scanf(" %c",&ch);
237         if(ch==\'n\'||ch==\'N\')
238             break;
239     }
240     student_output();
241     file_save();
242 }
243 void student_search()//记录搜索
244 {
245     struct student *q;
246     file_open(1);
247     q=p;
248     while(1)
249     {
250         int stu_num,flag=0,m,n;
251         char stu_name[123];
252         puts("如果你想用学号查询请按1,用姓名查询请按任意数字键");
253         scanf(" %d",&m);
254         puts("请输入");
255         if(m==1)
256         {
257             scanf("%d",&stu_num);
258             while(q)
259             {
260                 if(q->num==stu_num)
261                 {
262                     printf("%d %s %d %d %d %lf\n",q->num,q->name,q->score[0],q->score[1],q->score[2],q->avr);
263                     flag=1;
264                     break;
265                 }
266                 q=q->next;
267             }
268         }
269         else
270         {
271             scanf("%s",&stu_name);
272             while(q)
273             {
274                 if(strcmp(q->name,stu_name)==0)
275                 {
276                     printf("%d %s %d %d %d %lf\n",q->num,q->name,q->score[0],q->score[1],q->score[2],q->avr);
277                     flag=1;
278                     break;
279                 }
280                 q=q->next;
281             }
282         }
283 
284         if(!flag)
285             puts("对不起,您查早的人不存在");
286         puts("如果您想继续查询请按1,否则按任意键退出");
287         scanf("%d",&n);
288         if(n!=1)
289             return;
290     }
291 }
292 void student_revise()//记录修订
293 {
294     char stu_name[123];
295     struct student *q,*t,*pp;
296     file_open(1);
297     puts("请输入您要修改的学生姓名");
298     scanf(" %s",&stu_name);
299     while(1)
300     {
301         int flag=0,n;
302         q=p->next;
303         puts("请重新输入该学生的所有信息");
304         t=(struct student*)malloc(sizeof(struct student));
305         scanf("%d %s %d %d %d",&t->num,&t->name,&t->score[0],&t->score[1],&t->score[2]);
306         while(q)
307         {
308             if(strcmp(q->name,stu_name)==0)
309             {
310 
311                 q->avr=t->avr;
312                 strcpy(q->name,t->name),q->num=t->num,q->score[0]=t->score[0],q->score[1]=t->score[1],q->score[2]=t->score[2];
313                 student_order();
314                 flag=1;
315                 break;
316             }
317             q=q->next;
318         }
319         if(!flag)
320             puts("对不起,您要修改的人的信息不存在");
321         puts("如果您想继续查询请按1,否则按任意键退出");
322         scanf("%d",&n);
323         if(n!=1)
324             break;
325     }
326     file_save();
327 }
328 void func_choice()//功能介绍
329 {
330     printf("\t\t╭═════════■□■□═══╮\n");
331     printf("\t\t│ 学生信息管理系统 │\n");
332     printf("\t\t╰═══■□■□══════════╯\n");
333     printf("\t\t ┌─────────────-┐\n");
334     printf("\t\t │ 1. 输入记录 2. 显示记录 │\n");
335     printf("\t\t │  │\n");
336     printf("\t\t │ 3. 信息查询 4. 插入记录 │\n");
337     printf("\t\t │  │\n");
338     printf("\t\t │ 5. 修改记录 6. 删除记录 │\n");
339     printf("\t\t │  │\n");
340     printf("\t\t │ 0. 退出程序 │\n");
341     printf("\t\t └─────────────-┘\n");
342     printf("\t\t请您选择(0-6):");
343     while(1)
344     {
345         int m;
346         scanf("%d",&m);
347         switch(m)
348         {
349         case 0:
350             exit(0);
351             break;
352         case 1:
353             student_input();
354             break;
355         case 2:
356             file_open(0);
357             break;
358         case 3:
359             student_search();
360             break;
361         case 4:
362             //student_insert();
363             break;
364         case 5:
365             student_revise();
366             break;
367         case 6:
368             student_Delete();
369             break;
370         }
371         puts("本次查询结束");
372     }
373 }
374 int main()
375 {
376     func_choice();
377 }

 

版权声明:本文为moomcake原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/moomcake/p/9270554.html