测试数据
000000001 张三 100 99 98
000000002 马七 0 1 0
000000003 王五 50 60 60
000000004 李四 90 90 90
000000005 赵六 30 50 80
000000006 孙八 64 87 78
000000008 王明明 63 66 69
000000009 钱九 86 58 79
000000007 李琳琳 68 80 69
思路
1、输入密码 这里初始密码是 12345
2、录入成绩之后即可执行菜单的操作啦~~
代码段
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define N 10 //数据
#define LEN 10 //学号
#define NUM 100 //录入学生人数
typedef struct student
{
char num[LEN];
char name[N];
float chinese;
float math;
float english;
float ave;
student *next;
}student;
void check(student *p)
{
char num[LEN],name[N];
float chinese,math,english;
while(1)
{
scanf("%s%s%f%f%f",&num,&name,&chinese,&math,&english);
if(strlen(num)==9&&(chinese>=0&&chinese<=100)
&&(math>=0&&math<=100)&&(english>=0&&english<=100))
{
strcpy(p->num,num);strcpy(p->name,name);
p->chinese=chinese,p->english=english,p->math=math;
p->ave=(chinese+english+math)/3;
return ;
}
else
{
printf("数据错误,请检查一下重新输入\n---> ");
}
}
system("pause");
}
student *addstu()
{
system("cls");
student *p,*q,*h=(student *)malloc(sizeof(student));
h->next=NULL;
q=h;
printf("请输入你想要录入的学生信息人数-> ");
int n,i;
scanf("%d",&n);
printf("请按照如下格式填写\n---> 000000000 张三 100 99 98\n");
printf("--------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
p=(student *)malloc(sizeof(student));
printf("---> ");
check(p); //保证输入的数据在合理范围
//scanf("%s%s%f%f%f",&p->num,&p->name,&p->chinese,&p->math,&p->english);
//查看是否重复
student *c=h->next;
int flag=1;
while(c)
{
if(strcmp(p->num,c->num)==0){flag=0;break;}
c=c->next;
}
if(flag)
{
p->next=NULL;
q=q->next=p;
}
else
{
printf("您输入了重复的数据哦,请重新输入~\n");
n++;
}
}
printf("录入完毕\n");
system("pause");
return h;
}
void all(student *h)
{
system("cls");
student *p=h->next;
printf("学号\t\t姓名\t语文\t数学\t英语\t平均分\t\n");
while(p)
{
printf("%s\t%s\t%.1f\t%.1f\t%.1f\t%.2f\t\n",
p->num,p->name,p->chinese,p->math,p->english,p->ave);
p=p->next;
}
printf("--------------------------------------------------------------\n");
printf(" 输出完毕 \n");
system("pause");
}
void bad(student *h)
{
system("cls");
student *p=h->next;
printf("学号\t\t姓名\t语文\t数学\t英语\t平均分\t\n");
while(p)
{
if(p->chinese<60||p->english<60||p->math<60)
{
printf("%s\t%s\t%.1f\t%.1f\t%.1f\t%.2f\t\n",
p->num,p->name,p->chinese,p->math,p->english,p->ave);
}
p=p->next;
}
printf("--------------------------------------------------------------\n");
printf(" 输出完毕 \n");
system("pause");
}
void swap(student *s,student *p)
{
student *t=(student *)malloc(sizeof(student));
strcpy(t->num,s->num);strcpy(s->num,p->num);strcpy(p->num,t->num);
strcpy(t->name,s->name);strcpy(s->name,p->name);strcpy(p->name,t->name);
t->chinese=s->chinese;s->chinese=p->chinese;p->chinese=t->chinese;
t->english=s->english;s->english=p->english;p->english=t->english;
t->math=s->math;s->math=p->math;p->math=t->math;
t->ave=s->ave;s->ave=p->ave;p->ave=t->ave;
}
void insert_num(student *h)
{
system("cls");
student *p,*q,*s;
int t;
for(p=h->next;p->next;p=p->next)
{
s=p;
for(q=p->next;q;q=q->next)
{
if(strcmp(s->num,q->num)>0)
s=q;
}
if(s!=p) //交换
swap(s,p);
}
printf("交换完毕\n");
printf("--------------------------------------------------------------\n");
system("pause");
}
void insert_ave(student *h)
{
system("cls");
student *p,*q,*s;
int t;
for(p=h->next;p->next;p=p->next)
{
s=p;
for(q=p->next;q;q=q->next)
{
if(s->ave > q->ave)
s=q;
}
if(s!=p) //交换
swap(s,p);
}
printf("交换完毕\n");
printf("--------------------------------------------------------------\n");
system("pause");
}
void ave(student *h)
{
system("cls");
student *p=h;
int i=0;
float a[3]={0};
while(p)
{
i++;
a[0]+=p->chinese ;
a[1]+=p->math ;
a[2]+=p->english ;
p=p->next;
}
a[0]/=i;a[1]/=i;a[2]/=i;
printf("\n\n语文\t数学\t英语\t\n");
printf("%.2f\t%.2f\t%.2f\n\n",a[0],a[1],a[2]);
printf("--------------------------------------------------------------\n");
printf("计算完毕\n");
system("pause");
}
void destroy(student *h)
{
student *p;
while(h!=NULL)
{
p = h; //p指向要销毁的结点
h = h ->next; //head指向再下一个要销毁的结点
free(p); //销毁p指向的结点
}
}
int login()
{
char key[N];
printf("请输入密码->__");
while(1)
{
scanf("%s",&key);
if((strcmp("12345",key)==0))
{
printf("yeah!\n");
system("pause");
return 1;
}
else
printf("one more time-> ");
}//重复输入密码直到正确
}
void menu()
{
system("cls");
printf("\n");
printf("--------------------___学生信息管理系统___--------------------\n");
printf("----- ------\n");
printf("----- 菜单 ------\n");
printf("----- ------\n");
printf("----- [1]录入 -----\n");
printf("----- [2]计算各科平均分 -----\n");
printf("----- [3]按平均分排序 -----\n");
printf("----- [4]按学号排序 -----\n");
printf("----- [5]查看挂科学生信息 -----\n");
printf("----- [6]查看全部学生信息 -----\n");
printf("----- [0]退出 -----\n");
printf("\n");
printf("--------------------------------------------------------------\n");
}
int main()
{
student *h=NULL;
if(login()==0)return 0;
while(1)
{
menu();
int n;
printf("PS: 不要忘记先录入成绩哦__\n\n");
printf("选择菜单编号: ");
scanf("%d",&n);
switch(n)
{
case 1: h=addstu();break; //录入
case 2: ave(h);break; //计算各科平均分
case 3: insert_ave(h);all(h);break; //按平均分排序
case 4: insert_num(h);all(h);break; //按学号排序
case 5: bad(h);break; //查看挂科学生信息
case 6: all(h);break; //查看全部学生信息
case 0: printf("end......\n");destroy(h);exit(0); //退出
default : printf("请在 0~6 之间进行选择!\n");break;
}
}
return 0;
}