学员成绩管理系统

学员成绩管理
问题描述
用C语言编写一个程序实现学员成绩管理,每个学员包括3门课的成绩,从键盘输入学员信息,包括学号、姓名、三门课成绩,计算出学员的平均成绩,按照学员平均成绩由大到小排序
插入功能:在排序后的学员成绩表中插入一个学员的信息,要求插入后仍然保持成绩表有序
删除功能:要求输入指定的学号,从学员信息表中删除该学员,删除后的成绩表保持有序
问题分析
使用结构保存每个学员的信息,包括学号、姓名、三门课的成绩、平均成绩;
使用结构数组保存所有学员的信息;
需要实现以下函数:
单个学员信息的录入;
显示学员信息;
排序;(按照平均成绩由大到小)
插入;(插入后保持有序)
删除;(删除后保持有序)
在主函数中调用以上函数,分别完成录入、排序、插入和删除功能,并显示排序前后的学员信息,以及插入删除后的学员信息
难点分析-1
学员信息用结构表示,包括学号、姓名、三门课成绩,平均成绩
 struct student
 {
    int no;        //学号
    char name[15]; //姓名
    int score[3];  //三门课程的成绩
    double avr;    //平均成绩
 };
信息保存在一个结构数组中,由于学员人数<50,所以结构数组的大小定义为50 struct student stu[50];
难点分析-2
单个学员信息的录入函数
函数的原型:struct student input();
录入的同时,计算平均成绩,并保存在平均成绩字段中;
在主函数中调用“单个学员信息录入”函数
使用循环完成信息录入功能,由于不确定学员的数量(<50),所以建议使用while循环;
要求根据用户的输入决定是否继续。比如:
输出:“是否继续? (y or n)”,如果输入y或者Y则继续录入,否则结束录入;
录入的同时,记录录入学员信息的数量;
难点分析-3
编写显示学员信息的函数
由于需要多次显示学员信息,所以将显示学员信息的功能编写为函数;
考虑函数的参数及返回值;
通过循环依次输出学员信息;
注意控制输出的格式,使得输出的信息整齐;
阶段划分
第一阶段:实现学员信息录入
第二阶段:实现排序功能
第三阶段:实现插入和删除功能
第四阶段:相互讨论总结
第一阶段
第一阶段:实现学员信息录入
定义结构和结构数组;
编写“单个学员信息录入”函数;
编写“显示学员信息”函数,完成学员信息显示功能;
在main函数中通过调用“单个学员信息录入”函数,完成所有学员信息的录入,调用“显示学员信息”函数,显示录入后的学员信息;
要求学员自己动手编码,在编码的过程中解答学员提出的问题
第二阶段
第二阶段:实现排序功能
考虑函数的参数及返回值,使用冒泡排序算法;
在主函数中调用排序函数,并输出排序后的学员信息
要求学员自己动手编码,在编码的过程中解答学员提出的问题
第三阶段
第三阶段:实现插入和删除功能
编写插入函数
考虑函数参数及返回值,调用“单个学员信息录入”函数录入要插入的学员信息;
按照平均成绩的大小插入到学员信息数组中(找到合适的位置,先移动、再插入);
编写删除函数
考虑函数参数及返回值,要求用户输入要删除的学员的学号;
在学员信息数组中找到该学员,然后将后面元素前移达到删除该学员信息的目的;
在主函数中调用插入和删除函数
第四阶段
相互讨论总结
总结
本次项目案例完成了一个学员成绩管理的功能:包括录入、显示、排序、插入和删除
巩固的知识点:
结构;
结构数组;
不带参函数和带参函数,以及有返回值和没有返回值的情况;

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 int stu_count;///统计学生信息的数量
  4 struct student
  5 {
  6     int no;//学号
  7     char name[15];//姓名
  8     int score[3];//三科成绩
  9     double avr;//平均成绩
 10 } stu[100];
 11 void load()
 12 {
 13 
 14     printf("****************************************
");
 15     printf("********欢迎使用学员成绩管理系统********
");
 16     printf("********本程序由计科171王恺锋原创*******
");
 17     printf("****************************************
");
 18 }
 19 
 20 struct student input()///单个学员信息录入
 21 {
 22     int i;
 23     float sum;
 24     printf("请输入学号:
");
 25     scanf("%d",&stu[stu_count].no);
 26     getchar();
 27     printf("请输入姓名:
");
 28     gets(stu[stu_count].name);
 29     printf("请输入三门成绩:
");
 30     sum=0;
 31     for(i=0; i<3; i++)
 32     {
 33         printf("成绩%d:
",i+1);
 34         scanf("%d",&stu[stu_count].score[i]);
 35         sum=sum+stu[stu_count].score[i];
 36     }
 37     stu[stu_count].avr=sum/3.0;
 38     return stu[stu_count];
 39 };
 40 void display(struct student stu[])//显示学员信息
 41 {
 42 
 43     int i;
 44     for(i=0; i<stu_count; i++)
 45     {
 46         printf("学号: %d
",stu[i].no);
 47         printf("姓名: %s
",stu[i].name);
 48         printf("成绩1: %d 成绩2: %d 成绩3: %d 
",stu[i].score[0],stu[i].score[1],stu[i].score[2]);
 49         printf("平均成绩: %.2f
",stu[i].avr);
 50     }
 51 }
 52 void my_sort(struct student stu[])///冒泡排序
 53 {
 54 
 55     int i,j;
 56     struct student t;
 57     for(i=0; i<stu_count; i++)
 58     {
 59         for(j=0; j<stu_count-1-i; j++)
 60         {
 61             if(stu[j].avr<stu[j+1].avr)
 62             {
 63                 t=stu[j];
 64                 stu[j]=stu[j+1];
 65                 stu[j+1]=t;
 66             }
 67         }
 68     }
 69 }
 70 void my_insert(struct student stu[])//插入学员信息
 71 {
 72     int i,j;
 73     struct student t;
 74     printf("请输入要插入的学员信息:
");
 75     t=input();
 76     for(i=0; i<stu_count; i++)
 77     {
 78         if(stu[i].avr>t.avr)
 79         {
 80             break;
 81         }
 82     }
 83     for(j=stu_count; j>i; j--)
 84     {
 85         stu[j]=stu[j-1];
 86     }
 87     stu[i]=t;
 88     stu_count++;
 89 }
 90 void my_delete(struct student stu[])//删除学员信息
 91 {
 92     int i,j;
 93     int x;
 94     printf("请输入要删除的学号:
");
 95     scanf("%d",&x);
 96     for(i=0; i<stu_count; i++)
 97     {
 98         if(stu[i].no==x)
 99             break;
100     }
101     for(j=i; j<stu_count; j++) ///移动后面的数据
102     {
103         stu[j]=stu[j+1];
104     }
105     stu_count--;
106 }
107 int main()
108 {
109     char c;
110     load();
111     stu_count=0;//初始化为0
112     printf("请输入学员信息:
");
113     stu[stu_count]=input();
114     stu_count++;
115     while(1)
116     {
117         printf("是否继续?(y or n)
");
118         scanf(" %c",&c);
119         if(c=='y'||c=='Y')
120         {
121             stu[stu_count]=input();
122             stu_count++;
123         }
124         else if(c=='n'||c=='N')
125         {
126             break;
127         }
128     }
129     printf("按学员平均成绩降序排列:
");
130     my_sort(stu);
131     display(stu);
132     printf("是否插入新成员?(y or n)
");
133     scanf(" %c",&c);
134     if(c=='y'||c=='Y')
135     {
136         my_insert(stu);
137         printf("插入新学员后的信息如下:
");
138         my_sort(stu);
139         display(stu);
140     }
141     printf("是否删除某一成员?(y or n)
");
142     scanf(" %c",&c);
143     if(c=='y'||c=='Y')
144     {
145         my_delete(stu);
146         printf("删除某一学员后的信息如下:
");
147         my_sort(stu);
148         display(stu);
149     }
150     return 0;
151 }

使用链表改写程序

  1 #include <stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 using namespace std;
  5 struct student///学生信息存储结构
  6 {
  7     int no;                              //学号
  8     char name[15];                            //姓名
  9     int score[3];                             //成绩
 10     double avr;                            //平均分
 11     struct student *next;
 12 };
 13 void load()
 14 {
 15     printf("****************************************
");
 16     printf("********欢迎使用学员成绩管理系统********
");
 17     printf("********本程序由计科171王恺锋原创*******
");
 18     printf("****************************************
");
 19 }
 20 struct student *Create(student *head)///创建链表
 21 {
 22     student *p,*q;;
 23     head=(student *)malloc(sizeof(student));
 24     head->next=NULL;
 25     q=head;
 26     p=(student*)malloc(sizeof(student));
 27     printf("请输入学号:");
 28     scanf("%d",&p->no);
 29     getchar();
 30     printf("请输入姓名:");
 31     scanf("%s",p->name);
 32     printf("请输入成绩1:");
 33     scanf("%d",&p->score[0]);
 34     printf("请输入成绩2:");
 35     scanf("%d",&p->score[1]);
 36     printf("请输入成绩3:");
 37     scanf("%d",&p->score[2]);
 38     p->avr=(p->score[0]+p->score[1]+p->score[2])/3.0;
 39     q->next=p;
 40     q=p;
 41     q->next=NULL;
 42     return head;
 43 }
 44 struct student *Append(student *head)///在链表中追加记录
 45 {
 46     student *p,*q=head,*t;
 47     p=(student*)malloc(sizeof(student));
 48     printf("请输入学号:");
 49     scanf("%d",&p->no);
 50     getchar();
 51     printf("请输入姓名:");
 52     scanf("%s",p->name);
 53     printf("请输入成绩1:");
 54     scanf("%d",&p->score[0]);
 55     printf("请输入成绩2:");
 56     scanf("%d",&p->score[1]);
 57     printf("请输入成绩3:");
 58     scanf("%d",&p->score[2]);
 59     p->avr=(p->score[0]+p->score[1]+p->score[2])/3.0;
 60     /*p->order=0;*/
 61     while(q)
 62     {
 63         t=q;
 64         q=q->next;
 65     }
 66     t->next=p;
 67     t=p;
 68     t->next=NULL;
 69     return head;
 70 }
 71 void Print(student *head)///打印记录中的信息
 72 {
 73     student *p=head;
 74     while(p->next)
 75     {
 76         p=p->next;
 77         printf("学号: %d
",p->no);
 78         printf("姓名: %s
",p->name);
 79         printf("成绩1: %d 成绩2: %d 成绩3: %d 
",p->score[0],p->score[1],p->score[2]);
 80         printf("平均成绩: %.2f
",p->avr);
 81     }
 82 }
 83 
 84 struct student *Del(student *head) ///删除记录
 85 {
 86     int  number;
 87     student *p=head,*q;
 88     printf("输入要删除的学生学号:");
 89     scanf("%d",&number);
 90     while(p->next&&number!=p->no)
 91     {
 92         q=p;
 93         p=p->next;
 94     }
 95     q->next=p->next;
 96     return head;
 97 }
 98 
 99 
100 struct student *Sort(student *head)///根据平均分对记录进行排序
101 {
102     int i=1;
103     student *q, *s, *pre,*p,*a;
104     p=head->next;
105     q=p->next;
106     p->next=NULL;
107     while(q)
108     {
109         s=q;
110         q=q->next;
111         pre=head;
112         p=head->next;
113         while(p!=NULL && p->avr > s->avr)
114         {
115             pre=p;
116             p=p->next;
117         }
118         s->next=p;
119         pre->next=s;
120     }
121     a=head->next;
122     while(a)
123     {
124         /*a->order=i++;*/
125         a=a->next;
126     }
127     return head;
128 }
129 int main()
130 {
131     char c;
132     student *head=NULL;
133     load();
134     printf("请输入学员信息:
");
135     head=Create(head);///输入第一条信息并创建链表
136     while(1)
137     {
138         printf("是否继续?(y or n)
");
139         scanf(" %c",&c);
140         if(c=='y'||c=='Y')
141         {
142             head=Append(head);///追加信息
143         }
144         else if(c=='n'||c=='N')
145         {
146             break;
147         }
148     }
149     printf("按学员平均成绩降序排列:
");
150     head=Sort(head);
151     Print(head);
152     printf("是否插入新成员?(y or n)
");
153     scanf(" %c",&c);
154     if(c=='y'||c=='Y')
155     {
156         printf("请输入要增加的学生信息:
");
157         head=Append(head);///追加信息
158         printf("插入新学员后的信息如下:
");
159         head=Sort(head);
160         Print(head);
161     }
162     printf("是否删除某一成员?(y or n)
");
163     scanf(" %c",&c);
164     if(c=='y'||c=='Y')
165     {
166         head=Del(head);
167         printf("删除某一学员后的信息如下:
");
168         head=Sort(head);
169         Print(head);
170     }
171     return 0;
172 }
原文地址:https://www.cnblogs.com/wkfvawl/p/9273043.html