学生成绩管理系统(链表处理+链表排序)

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 struct information{
  5     char number[11];
  6     char name[7];
  7     int English;
  8     int math;
  9     int physics;
 10     int C;
 11     int sum;
 12     double ave;
 13 };//data
 14 
 15 typedef struct student{
 16     struct information data;
 17     struct student *next;
 18 }STU;
 19 
 20 STU * sort(STU *head);
 21 // void exchange(STU *a,STU *b);
 22 STU * Creatlist(STU *head){
 23     STU *p=NULL;
 24     int num;
 25     scanf("%d",&num);//输入的是学生的个数 
 26     if(head==NULL){
 27         STU *tail=NULL;
 28         while(num-->0){
 29             p=(STU *)malloc(sizeof(STU ));
 30 //        //输入学生信息  
 31             scanf("%s",p->data.number);
 32             scanf("%s",p->data.name);
 33             scanf("%d %d %d %d",&p->data.English,&p->data.math,&p->data.physics,&p->data.C);
 34 //            scanf("%d",&p->data.math);
 35 //            scanf("%d",&p->data.physics);
 36 //            scanf("%d",&p->data.C);
 37         //    
 38 //            printf("here");
 39             if(head==NULL){
 40 //                printf("GOT");
 41                 head=p;//初次输入1时第一轮 
 42             }
 43             else{
 44                 tail->next=p;
 45             }
 46             tail=p;
 47         }tail->next=NULL;
 48         //sb题目创建完列表都要排序的吗
 49         p=head;
 50         while(p!=NULL){
 51         double average=0.00;
 52         int sum_=0;
 53         sum_=p->data.English+p->data.math+p->data.physics+p->data.C;
 54         average=(sum_)/4.00*1.00;
 55         p->data.ave=average;
 56         p->data.sum=sum_;
 57         p=p->next;
 58         }
 59         //
 60         
 61     }else {
 62         p=head;
 63         while(p->next!=NULL){
 64             p=p->next;
 65         }
 66         STU *tail=p;
 67         while(num-->0){
 68             p=(STU *)malloc(sizeof(STU ));
 69         //第二次输入学生信息 
 70             scanf("%s",p->data.number);
 71             scanf("%s",p->data.name);
 72             scanf("%d %d %d %d",&p->data.English,&p->data.math,&p->data.physics,&p->data.C);
 73         //    
 74             tail->next=p;
 75             tail=p;
 76         }tail->next=NULL;
 77     }
 78     return head;
 79 }
 80 STU * Change(STU *head){
 81     char symbol[11];
 82     scanf("%s",symbol);//读入待查找人
 83     STU *p=head;
 84     while(p!=NULL){
 85         if(strcmp(p->data.number,symbol)==0){
 86             int judge=0,score=0;
 87             scanf("%d",&judge);//读入待修改成绩 
 88             scanf("%d",&score);
 89             switch(judge){
 90                 case 1:
 91                     p->data.English=score; 
 92                     break;
 93                 case 2:
 94                     p->data.math=score;
 95                     break;
 96                 case 3:
 97                     p->data.physics=score;
 98                     break;
 99                 case 4:
100                     p->data.C=score;
101                     break;
102                 default: break; 
103             }
104             break;//改变之后跳出查找 
105         }
106         p=p->next;
107     }
108     return head;
109 }
110 
111 STU * Print_all(STU *head){
112     head=sort(head);
113     STU *p=head;
114     while(p!=NULL){
115         printf("%s ",p->data.number);
116         printf("%s ",p->data.name);
117         printf("%d ",p->data.English);
118         printf("%d ",p->data.math);
119         printf("%d ",p->data.physics);
120         printf("%d",p->data.C);
121         printf("\n");
122         p=p->next;
123     }
124     return head;
125 }
126 
127 
128 
129  STU * Average(STU *head){
130      STU *p=head;
131      while(p!=NULL){
132          double average=0.00;
133          int sum_=0;
134          sum_=p->data.English+p->data.math+p->data.physics+p->data.C;
135          average=(sum_)/4.00*1.00;
136          p->data.ave=average;
137          p->data.sum=sum_;
138          p=p->next;
139  //        printf("##%s %d %f##\n",p->data.name,p->data.sum,p->data.ave);
140      }
141      head=sort(head);
142      p=head;
143      while(p!=NULL){
144          printf("%s ",p->data.number);
145          printf("%s ",p->data.name);
146          printf("%.2f",p->data.ave);
147          printf("\n");
148          p=p->next;
149      }
150      return head;    
151  }
152 
153 // STU * sort(STU *head){
154     
155 //     return head;
156 // }
157 
158 STU * sort(STU *head){
159     STU *loc_head,*tail=NULL,*last=NULL,*p;
160     p=head;
161     STU *mid=NULL;
162     loc_head=(STU *)malloc(sizeof(STU));
163     loc_head->next=head;
164     for(tail=NULL;loc_head->next!=tail;tail=p){
165         p=loc_head->next;//如果用 p=head;为什么会传址失败呢?
166         for(last=loc_head;p->next!=tail;last=last->next,p=p->next){
167             mid=p->next; 
168             if((p->data.ave)>(mid->data.ave)){
169                 last->next=mid;
170                 p->next=mid->next;
171                 mid->next=p;
172                 p=last->next;
173             }
174         }
175     }
176     head=loc_head->next;
177     free(loc_head);
178     return head;
179 }
180 
181 
182 STU * subPrint(STU *head){
183     STU *p=head;
184     while(p!=NULL){
185         double average=0.00;
186         int sum_=p->data.English+p->data.math+p->data.physics+p->data.C;
187         average=(sum_)/4.00*1.00;
188         p->data.ave=average;
189         p->data.sum=sum_;
190         p=p->next;
191 //        printf("##%s %d %f##\n",p->data.name,p->data.sum,p->data.ave);
192     }
193     head=sort(head);
194     p=head;
195     while(p!=NULL){
196         printf("%s ",p->data.number);
197         printf("%s ",p->data.name);
198         printf("%d ",p->data.sum);
199         printf("%.2f",p->data.ave);
200         printf("\n");
201         p=p->next;
202     }
203     return head;
204 }
205 
206 STU * (*menu[5])(STU *)={Creatlist,Print_all,Change,Average,subPrint};
207 int main(){
208     STU *head=NULL;
209     int flag;
210     //input flag
211     scanf("%d",&flag);
212     while(flag!=0){
213         head=(*menu[flag-1])(head);
214         scanf("%d",&flag);
215     }
216     return 0;
217 }

 

posted @ 2022-03-19 20:59  leoyin59123  阅读(304)  评论(0)    收藏  举报