永发信息网

如何用单链表写一个学生管理系统

答案:1  悬赏:60  手机版
解决时间 2021-03-17 13:40
  • 提问者网友:山高云阔
  • 2021-03-16 17:32
如何用单链表写一个学生管理系统
最佳答案
  • 五星知识达人网友:轻熟杀无赦
  • 2021-03-16 17:50

#include
#include
#include
#include
#include
#include
#include
#define LEN sizeof(STUDENT)
typedef struct stu
{char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;


STUDENT *init();
int menu_select();
STUDENT *create();
void print(STUDENT *head);
void search(STUDENT *head);
STUDENT *delete(STUDENT *head);
STUDENT *sort(STUDENT *head);
STUDENT *insert(STUDENT *head,STUDENT *new);
void save(STUDENT *head);
STUDENT *load();


main()
{STUDENT *head,new;
head=init();
for(;;)
{switch(menu...
#include
#include
#include
#include
#include
#include
#include
#define LEN sizeof(STUDENT)
typedef struct stu
{char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;


STUDENT *init();
int menu_select();
STUDENT *create();
void print(STUDENT *head);
void search(STUDENT *head);
STUDENT *delete(STUDENT *head);
STUDENT *sort(STUDENT *head);
STUDENT *insert(STUDENT *head,STUDENT *new);
void save(STUDENT *head);
STUDENT *load();


main()
{STUDENT *head,new;
head=init();
for(;;)
{switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&new);break;
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0);
}
}
}


STUDENT *init()
{
return NULL;
}


menu_select()
{int n;
struct date d;
getdate(&d);
printf("\n按任一键进入主菜单...... \npress any key to enter the menu......");
getch();
clrscr();
printf("********************************************************************************\n");
printf("\t\t 欢迎 Welcome to\n");
printf("\n\t\t\t 使用学生管理系统1.0\n\n\t\t\t\t\t-----------景炎中学计算机组WJQ\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. 输入学生成绩记录 Enter the record\n");
printf("\t\t\t2. 显示 Print the record\n");
printf("\t\t\t3. 寻找 Search record on name\n");
printf("\t\t\t4. 删除 Delete a record\n");
printf("\t\t\t5. 排序 Sort to make new a file\n");
printf("\t\t\t6. 插入 Insert record to list\n");
printf("\t\t\t7. 保存 Save the file\n");
printf("\t\t\t8. 读取 Load the file\n");
printf("\t\t\t9. 退出 Quit\n");
printf("\n\t\t 制作吴俊遒WJQ Made by Wu Junqiu.\n");
printf("********************************************************************************\n");
printf("\t\t\t\t当前系统日期:%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day);
do{
printf("\n\t\t\t输入你的选择Enter your choice(1~9):");
scanf("%d",&n);
}while(n<1||n>9);
return(n);
}


STUDENT *create()
{int i,s;
STUDENT *head=NULL,*p;
clrscr();
for(;;)
{p=(STUDENT *)malloc(LEN);
if(!p)
{printf("\n输出内存溢出. Out of memory.");
return (head);
}
printf("输入学号Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break;
printf("输入名字Enter the name:");
scanf("%s",p->name);
printf("请输入3门成绩Please enter the %d scores\n",3);
s=0;
for(i=0;i<3;i++)
{
do{
printf("成绩score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100)
printf("数据错误,请重新输入 Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i];
}
p->sum=s;
p->average=(float)s/3;
p->order=0;
p->next=head;
head=p;
}
return(head);
}


void print(STUDENT *head)
{int i=0;
STUDENT *p;
clrscr();
p=head;
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}


void search(STUDENT *head)
{STUDENT *p;
char s[5];
clrscr();
printf("请输入个姓名来查找. Please enter name for searching.\n");
scanf("%s",s);
p=head;
while(strcmp(p->name,s) && p != NULL)
p=p->next;
if(p!=NULL)
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\n没有该学生 There is no num %s student on the list.\n",s);
}


STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2;
char c,s[6];
clrscr();
printf("请输入要删除的学号 Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head;
while(strcmp(p1->num,s) && p1 != NULL)
{p2=p1;
p1=p1->next;
}
if(strcmp(p1->num,s)==0)
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("\n是否要删除,输入Y删除,N则退出\nAre you sure to delete the student Y/N ?");
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break;
if(c=='y'||c=='Y')
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
n=n-1;
printf("\n学号为(Num): %s 学生以被删除(student have been deleted.)\n",s);
printf("别忘了保存. Don't forget to save.\n");break;
}
}
}
else
printf("\n没有这个学生在表上\nThere is no num %s student on the list.\n",s);
return(head);
}


STUDENT *sort(STUDENT *head)
{int i=0;
STUDENT *p1,*p2,*t,*temp;
temp=head->next;
head->next=NULL;
while(temp!=NULL)
{
t=temp;
temp=temp->next;
p1=head;
p2=head;
while(t->averageaverage&&p1!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1==p2)
{
t->next=p1;
head=t;
}
else
{
t->next=p1;
p2->next=t;
}
}
p1=head;
while(p1!=NULL)
{
i++;
p1->order=i;
p1=p1->next;
}
printf("排序成功 Sorting is sucessful.\n");
return (head);
}


STUDENT *insert(STUDENT *head,STUDENT *new)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head;
p0=new;
printf("\nPlease enter a new record.\n");
printf("输入学号Enter the num:");
scanf("%s",new->num);
printf("输入名字Enter the name:");
scanf("%s",new->name);
printf("Please enter the %d scores.\n",3);
sum1=0;
for(i=0;i<3;i++)
{
do{
printf("成绩score%d:",i+1);
scanf("%d",&new->score[i]);
if(new->score[i]>100||new->score[i]<0)
printf("数据错误Data error,please enter again.\n");
}while(new->score[i]>100||new->score[i]<0);
sum1=sum1+new->score[i];
}
new->sum=sum1;
new->average=(float)sum1/3;
new->order=0;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{while((p0->averageaverage)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(p0->average>=p1->average)
{if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
head=sort(head);
printf("\n学生Student %s 已被更新have been inserted.\n",new->name);
printf("不要忘了保存Don't forget to save the new file.\n");
return(head);
}


void save(STUDENT *head)
{FILE *fp;
STUDENT *p;
char outfile[10];
printf("输出文件例如:c:\\score Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"w"))==NULL)
{
printf("打不开文件Cannot open the file\n");
return;
}
printf("\n保存中...Saving the file......\n");
p=head;
while(p!=NULL)
{
fwrite(p,LEN,1,fp);
p=p->next;
}
fclose(fp);
printf("保存成功....Save the file successfully!\n");
}


STUDENT *load()
{STUDENT *p1,*p2,*head=NULL;
FILE *fp;
char infile[10];
printf("倒入文件例如:c:\\score Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"r"))==NULL)
{
printf("打不开文件Can not open the file.\n");
return(head);
}
printf("\n寻找文件...Loading the file!\n");
p1=(STUDENT *)malloc(LEN);
if(!p1)
{
printf("内存溢出!Out of memory!\n");
return(head);
}
head=p1;
while(!feof(fp))
{
if(fread(p1,LEN,1,fp)!=1) break;
p1->next=(STUDENT *)malloc(LEN);
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1;
p1=p1->next;
}
p2->next=NULL;
fclose(fp);
printf("\n你成功的从文件中读取了数据!\nYou have success to read data from the file!\n");
return (head);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯