#include <stdio.h>
#include <stdlib.h>
#include<string.h>
struct stud
{
char id[13];
char name[21];
int score1,score2,score3;
int sum;
struct stud *next;
};
typedef struct stud snode, *stud;
void CreateStuList(struct stud *head);
void PrintStuList(struct stud *head);
void StuListInsert(struct stud *head);
void StuListDelete(struct stud *head);
void lianbiaoxieru(struct stud *head);
void duqulianbiao(struct stud *head);
void StaticScoreList(struct stud *head);
int main()
{
struct stud *head;
int a;
head=(stud)malloc(sizeof(snode));
head->next=NULL;
while(1)
{
printf("===========================\n");
printf("*欢迎进入学生信息管理系统*\n");
printf("1:创建链表\n");
printf("2:插入元素\n");
printf("3:删除元素\n");
printf("4:显示元素\n");
printf("5:统计\n");
printf("0:退出系统\n");
printf("===========================\n");
printf("请选择菜单项:\n");
scanf("%d",&a);
switch(a)
{
case 1:
CreateStuList(head);
break;
case 2:
StuListInsert(head);
break;
case 3:
StuListDelete(head);
break;
case 4:
PrintStuList(head);
break;
case 5:
StaticScoreList(head);
break;
case 0:
exit(0);
}
}
return 0;
}
void CreateStuList(struct stud *head)
{
stud p,r=head;
char n[13];
printf("输入若干整数,输入#表示输入结束\n");
while(scanf("%s",n),strcmp(n,"#")!=0)
{
p=(stud)malloc(sizeof(snode));
strcpy(p->id,n);
scanf("%s%d%d%d",p->name,&p->score1,&p->score2,&p->score3);
p->sum=p->score1+p->score2+p->score3;
r->next=p;
r=p;
}
r->next=NULL;
printf("\n\n新创建的链表中学生信息:\n");
PrintStuList(head);
system("pause");
system("cls");
}
void StuListInsert(struct stud *head)
{
printf("当前链表中的学生信息为:\n");
char id1[13],name1[21];
int s1,s2,s3,a=0;
PrintStuList(head);
printf("请输入待添加学生的信息:\n");
scanf("%s%s%d%d%d",id1,name1,&s1,&s2,&s3);
stud p=head->next;
while(p)
{
if(strcmp(p->id,id1)==0)
{
a=1;
break;
}
p=p->next;
}
if(a==1)
{
printf("\n\n学号已有,无法添加!\n\n");
}
else
{
stud p=head,r;
r=(stud)malloc(sizeof(snode));strcpy(r->id,id1),strcpy(r->name,name1),r->score1=s1,r->score2=s2,r->score3=s3,r->sum=s1+s2+s3;
while(p)
{
if(p->next==NULL)
{
r->next=p->next;p->next=r;
break;
}
else if(strcmp(p->next->id,id1)>0)
{
r->next=p->next;p->next=r;
break;
}
p=p->next;
}
}
printf("链表修改后的学生信息为:\n");
PrintStuList(head);
system("pause");
system("cls");
}
void StuListDelete(struct stud *head)
{
printf("当前链表中的学生信息为:\n");
PrintStuList(head);
printf("\n请输入待删除学生的学号信息:\n");
char id1[13];
scanf("%s",id1);
stud p=head,r=NULL;
while(p&&strcmp(p->id,id1)!=0)
{
r=p;
p=p->next;
}
if(p==NULL)
{
printf("\n\n查无此号,无法删除!\n\n");
}
else if(r==NULL)
{printf("待删除学生的信息为:\n");
printf("%-15s%-10s%-10d%-10d%-10d%-10d\n",p->id,p->name,p->score1,p->score2,p->score3,p->sum);
head=head->next;
}
else
{ printf("待删除学生的信息为:\n");
printf("%-15s%-10s%-10d%-10d%-10d%-10d\n",p->id,p->name,p->score1,p->score2,p->score3,p->sum);
r->next=p->next;
free(p);
}
printf("链表修改后的学生信息为:\n");
PrintStuList(head);
system("pause");
system("cls");
}
void PrintStuList(struct stud *head)
{
stud p=head->next;
while(p)
{
printf("%-15s%-10s%-10d%-10d%-10d%-10d\n",p->id,p->name,p->score1,p->score2,p->score3,p->sum);
p=p->next;
}
}
void lianbiaoxieru(struct stud *head)
{
FILE *fp;
stud p;
p=head->next;
fp=fopen("学生信息.txt","w");
if(fp==NULL)
{
printf("FILE OPEN FILE\n");
exit(0);
}
while(p)
{
if(p->next==NULL)
{
fprintf(fp,"%-15s%-8s%-5d%-5d%-5d\n",p->id,p->name,p->score1,p->score2,p->score3);
break;
}
fprintf(fp,"%-15s%-8s%-5d%-5d%-5d\n",p->id,p->name,p->score1,p->score2,p->score3);
p=p->next;
}
fclose(fp);
}
void duqulianbiao(struct stud *head)
{
FILE *fp;
stud p,re;
re=head;
fp=fopen("学生信息.txt","r");
if(fp==NULL)
{
printf("can not\n");
exit(0);
}
while(feof(fp)==0)
{
p=(stud)malloc(sizeof(snode));
fscanf(fp,"%s%s%d%d%d\n",p->id,p->name,&p->score1,&p->score2,&p->score3);
re->next=p;
re=p;
}
re->next=NULL;
fclose(fp);
}
void paixu(struct stud *head)
{
duqulianbiao(head);
stud p,b;
char t[20],a;
for(p=head->next;p!=NULL;p=p->next)
{
for(b=p->next;b!=NULL;b=b->next)
if(strcmp(p->id,b->id)>0)
{
strcpy(t,p->id);
strcpy(p->id,b->id);
strcpy(b->id,t);
strcpy(t,p->name);
strcpy(p->name,b->name);
strcpy(b->name,t);
a=p->score1;
p->score1=b->score1;
b->score1=a;
a=p->score2;
p->score2=b->score2;
b->score2=a;
a=p->score3;
p->score3=b->score3;
b->score3=a;
}
}
}
void StaticScoreList(struct stud *head)
{
int a=0;
printf("当前链表中的学生信息为:\n");
PrintStuList(head);
printf("有不及格成绩的学生信息统计结果为:\n");
stud p=head->next;
while(p)
{if(p->score1<60||p->score2<60||p->score3<60)
{
printf("%-15s%-10s%-10d%-10d%-10d%-10d\n",p->id,p->name,p->score1,p->score2,p->score3,p->sum);
a=1;
}
p=p->next;
}
if(a==0)
{
printf("没有不及格成绩的学生!\n");
}
system("pause");
system("cls");
}