题目:
学生信息管理系统设计
功能:1.学生(10个学生),每个学生是一个记录。包括的信息有:班级,学号,姓名,C语言成绩,高数成绩,英语成绩,平均成绩。可以对学生的基本信息进行录入,修改某个学生的记录,删除某个学生的记录,显示所有学生的信息.
2.可计算每个学生的平均分。
3.能按学号查询单个学生的信息
4.按平均成绩排序。
自编的代码:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct node
{char name[20];
char num[10];
int class;
int CG,MG,EG,AG;
struct node *next;
};
struct node *h;
/*创建链表*/
struct node *Create()
{char name[20];
struct node *p,*q;
q=h=NULL;
printf(“name:\n”);
fgets(name,20,stdin);
while(strlen(name)!=0)
{p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{printf(“Allocation is failing.\n”);
exit(0);
}
else strcpy(p->name,name);
printf(“num:\n”);
gets(p->num);
printf(“class:\n”);
scanf(“%d”,&p->class);
printf(“CG:\n”);
scanf(“%d”,&p->CG);
printf(“MG:\n”);
scanf(“%d”,&p->MG);
printf(“EG:\n”);
scanf(“%d”,&p->EG);
printf(“AG:\n”);
p->AG=(p->CG+p->MG+p->EG)/3;
printf(“%d\n”,p->AG);
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
printf(“name:\n”);
gets(name);
}
return h;
}
/* 排序*/
Ranking(struct node *head)
{struct node *r,*s;
struct node *u,*t;
int a;
s=head;r=s->next;
u=t=head;
while(r->AG<s->AG&&r!=NULL)
{s=r;r=r->next;}
if(r->AG>=s->AG)
{s->next=r->next;
a=r->AG;
if(t->AG>a&&t!=NULL)
{u=t;t=t->next;}
else {if(t->AG<=a)
{u->next=r;
r->next=t;}
if(t==NULL)
u->next=r;}
}
}
/*查询*/
Find(struct node *head)
{char num[10];
struct node *p=head;
printf(“Input the num to find:”);
gets(num);
while(strcmp(p->num,num)!=0)
p=p->next;
if(p!=NULL)
{puts(p->name);
puts(p->num);
printf(“\n%d\n%d,%d,%d,%d\n”,p->class,p->CG,p->MG,p->EG,p->AG);
}
else printf(“Not Found!”);
}
/*删除*/
Delete(struct node *head)
{char num[10];
struct node *p,*q;
p=q=head;
printf(“Input the num to delete:”);
gets(num);
while(strcmp(p->num,num)!=0)
{q=p;p=p->next;}
if(p!=NULL)
{q->next=p->next;
free(p);}
else printf(“Not Found the num to delete!”);
}
/*输出*/
Output(struct node *head)
{struct node *p;
p=head;
while(p!=NULL)
{puts(p->name);
puts(p->num);
printf(“\n%d\n%d,%d,%d,%d\n”,p->class,p->CG,p->MG,p->EG,p->AG);
p=p->next;
}
}
/*修改*/
Revise(struct node *head)
{char name[20];
char num[10];
int class;
int CG,MG,EG,AG;
int a;
struct node *p,*q;
struct node *r,*s;
r=s=head;
p=head;
printf(“Input the number to revise:\n”);
gets(num);
while(strcmp(p->num,num)!=0)
{q=p;p=p->next;}
a=p->AG;
printf(“Input the new name:”);
gets(p->name);
printf(“Input the new num;”);
gets(p->num);
printf(“Input the new class:”);
scanf(“%d”,p->class);
printf(“Please input the new CG,MG,EG:\n”);
scanf(“%d,%d,%d”,p->CG,p->MG,p->EG);
p->AG=(p->CG+p->MG+p->EG)/3;
if(a!=p->AG)
{if(p->next==NULL)
q->next=NULL;
else if(q==h)h=h->next;
else q->next=p->next;
while (r->AG>p->AG&&r!=NULL)
{s=r;r=r->next;}
if(r==h)
p->next=r;
else if(r==NULL)s->next=p;
else {p->next=r;s->next=p;}
}
}
/*主函数*/
main()
{int a;
struct node *head;
head=Create();
printf(“\n***************\n 1.Find\n 2.Delete\n 3.Output\n 4.Revise\n 0.Exit\n***************\n”);
do
{printf(“Input a number(0-4):”);
scanf(“%d”,&a);
switch(a)
{case 1:Find(head);break;
case 2:Delete(head);break;
case 3:Output(head);break;
case 4:Revise(head);break;
}
}while(a!=0);
getch();}
该代码经过win-tc测试,在/*创建链表*/时只能输入一个记录,不能输入第二个记录的name,怀疑是gets(name);语句用错,希望各位大虾指点迷津,谢谢。
>> 本文固定链接: http://www.vcgood.com/archives/3248