您的位置:首页 > 其它

学生管理系统—链表

2014-01-04 14:57 225 查看
这个学生管理系统能输入学生的姓名,3门成绩,学号信息,并且能根据学生的姓名对学生的信息实现修改,查找,插入,删除,添加功能,整个代码都是由链表实现的。

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define AVER(x,y,z) ((x+y+z)/3)

#define NEW (struct student *)malloc(sizeof(struct student))

FILE *fp;

struct student

{

char name[20],num[10];

double sub1,sub2,sub3,aver;

struct student *next;

};

struct student *creat_1() /*重新输入*/

{

if((fp=fopen("file.txt","wt"))==NULL)

{

printf("can not open file\n");

exit(1);

}

int count=0;

static struct student *head;

struct student *p,*q;

char name[20];

head=NULL;

printf("若无需输入学生,请按回车结束输入\n");

printf("name:\n");

gets(name);

while(strlen(name)!=0)

{

p=NEW;

strcpy(p->name,name);

printf("num:\n");

gets(p->num);

printf("sub1,sub2,sub3:\n");

scanf("%lf%lf%lf",&(p->sub1),&(p->sub2),&(p->sub3));

p->aver=AVER(p->sub1,p->sub2,p->sub3);

fprintf(fp,"%s\t%s\t%lf\t%lf\t%lf\t%lf\n",p->name,p->num,p->sub1,p->sub2,p->sub3,p->aver);

p->next=NULL;

if(head==NULL)

head=p;

else

q->next=p;

q=p;

printf("\n");

printf("name:\n");

fflush(stdin);

gets(name);

count++;

}

printf("学生数目:%d\n",count);

fclose(fp);

return(0);

}

struct student *creat_2() /*追加*/

{

if((fp=fopen("file.txt","at"))==NULL)

{

printf("can not open file\n");

exit(1);

}

static struct student *head;

struct student *p,*q;

char name[20];

head=NULL;

printf("若无需输入学生,请按回车结束输入:\n");

printf("name:\n");

gets(name);

while(strlen(name)!=0)

{

p=NEW;

strcpy(p->name,name);

printf("num:\n");

gets(p->num);

printf("sub1,sub2,sub3:\n");

scanf("%lf%lf%lf",&(p->sub1),&(p->sub2),&(p->sub3));

p->aver=AVER(p->sub1,p->sub2,p->sub3);

fprintf(fp,"%s\t%s\t%lf\t%lf\t%lf\t%lf\n",p->name,p->num,p->sub1,p->sub2,p->sub3,p->aver);

p->next=NULL;

if(head==NULL)

head=p;

else

q->next=p;

q=p;

printf("\n");

printf("name:\n");

fflush(stdin);

gets(name);

}

fclose(fp);

return(0);

}

struct student *creat_3() /*创建新的节点*/

{

static struct student *head;

struct student *p,*q;

char name[20];

head=NULL;

printf("如果不想输入学生,请在name后输入回车结束输入\n");

printf("name:\n");

gets(name);

while(strlen(name)!=0)

{

p=NEW;

strcpy(p->name,name);

printf("num:\n");

gets(p->num);

printf("sub1,sub2,sub3:\n");

scanf("%lf%lf%lf",&(p->sub1),&(p->sub2),&(p->sub3));

p->aver=AVER(p->sub1,p->sub2,p->sub3);

p->next=NULL;

if(head==NULL)

head=p;

else

q->next=p;

q=p;

printf("\n");

printf("name:\n");

fflush(stdin);

gets(name);

}

return head;

}

struct student *delstu(struct student *head,char *x) /*删除*/

{

struct student *p,*q;

static struct student *h;

p=head;

if(head==NULL)

{

printf("the list is empty\n");

return head;

}

while(p->next!=NULL&&strcmp(x,p->name)!=0)

{

q=p;

p=p->next;

}

if(strcmp(x,p->name)==0)

{

if(p==head)

head=p->next;

else

q->next=p->next;

free(p);

}

else

printf("No found\n");

h=head;

return h;

}

struct student *rea() /*读取文件*/

{

int count=0;

static struct student *head;

struct student *p,*q;

if((fp=fopen("file.txt","rt"))==NULL)

{

printf("can not open file\n");

exit(1);

}

head=NULL;

while(!feof(fp))

{

p=NEW;

fscanf(fp,"%s%s%lf%lf%lf%lf\n",p->name,p->num,&p->sub1,&p->sub2,&p->sub3,&p->aver);

p->next=NULL;

if(head==NULL)

head=p;

else

q->next=p;

q=p;

count++;

}

printf("学生数目:%d\n",count);

fclose(fp);

return head;

}

void prlist(struct student *head) /*转化成文件*/

{

if((fp=fopen("file.txt","wt+"))==NULL)

{

printf("can not open file\n");

exit(1);

}

int count=0;

struct student *p;

p=head;

for(;p!=NULL;p=p->next)

{

fprintf(fp,"%s\t%s\t%lf\t%lf\t%lf\t%lf\n",p->name,p->num,p->sub1,p->sub2,p->sub3,p->aver);

count++;

}

printf("学生数目:%d\n",count);

fclose(fp);

}

void fstu(struct student *head,char *x) /*查找*/

{

struct student *q,*p;

p=head;

while(p->next!=NULL&&strcmp(p->name,x)!=0)

{

q=p;

p=p->next;

}

if (strcmp(p->name,x)==0)

{

printf("name=%s,num=%s,sub1=%lf,sub2=%lf,sub3=%lf,aver=%lf\n",p->name,p->num,p->sub1,p->sub2,p->sub3,p->aver);

}

}

struct student *insert(struct student *head,struct student *op,char *x) /*插入*/

{

struct student *p,*q;

static struct student *h;

p=head;

if(head==NULL)

{

head=op;

op->next=NULL;

}

else

{

while(strcmp(x,p->name)!=0&&p->next!=NULL)

{

q=p;

p=p->next;

}

if(strcmp(x,p->name)==0)

{

if(p==head)

head=op;

else

q->next=op;

op->next=p;

}

else

{

p->next=op;

op->next=NULL;

}

}

h=head;

return h;

}

struct student *change(struct student *head,char *x)

{

char m[20];

struct student *p,*q;

static struct student *h;

p=head;

if(head==NULL)

{

printf("the list is empty\n");

return head;

}

while(p->next!=NULL&&strcmp(x,p->name)!=0)

{

q=p;

p=p->next;

}

if(strcmp(x,p->name)==0)

{

printf("名字修改为:");

gets(m);

strcpy(p->name,m);

printf("学号修改为:");

gets(m);

strcpy(p->num,m);

printf("科目1,科目2,科目3修改为:");

scanf("%lf%lf%lf",&p->sub1,&p->sub2,&p->sub3);

p->aver=AVER(p->sub1,p->sub2,p->sub3);

}

h=head;

return h;

}

void zhibaio(struct student *head)

{

int count=0;

struct student *p;

p=head;

for(;p!=NULL;p=p->next)

{

printf("%s\t%s\t%lf\t%lf\t%lf\t%lf\n",p->name,p->num,p->sub1,p->sub2,p->sub3,p->aver);

count++;

}

printf("学生数目:%d\n",count);

}

void select()

{

struct student *head,*op;

char c[20];

int n;

printf("************************************\n");

printf("1,重新输入学生成绩 2,添加学生成绩\n");

printf("3,删除学生成绩 4,查找学生成绩\n");

printf("5,插入学生成绩 6,修改学生成绩\n");

printf("7,查看已有所有学生信息\n");

printf("************************************\n");

printf("please input the selection:");

scanf("%d",&n);

fflush(stdin);

switch(n)

{

case 1:creat_1();

printf("返回上级菜单请按 y,否则回车结束程序\n");

gets(c);

if(strlen(c)!=0)

{

select();

}

break;

case 2:creat_2();

printf("返回上级菜单请按 y,否则回车结束程序\n");

gets(c);

if(strlen(c)!=0)

{

select();

}

break;

case 3:head=rea();

printf("被删学生名字为:");

gets(c);

while(strlen(c)!=0)

{

head=delstu(head,c);

printf("若需要继续删除其他学生,请输入该学生的名字,否则按回车结束删除:\n");

gets(c);

}

prlist(head);

printf("返回上级菜单请按 y,否则回车结束程序\n");

gets(c);

if(strlen(c)!=0)

{

select();

}

break;

case 4:head=rea();

printf("被查学生名字为:");

gets(c);

while(strlen(c)!=0)

{

fstu(head,c);

printf("若无需继续查找学生,请输入 回车 结束查找,若需查找,请输入查找学生的名字为:\n");

gets(c);

}

printf("返回上级菜单请按 y,否则回车结束程序\n");

gets(c);

if(strlen(c)!=0)

{

select();

}

break;

case 5:head=rea();

printf("请输入 y :\n");

gets(c);

while(strlen(c)!=0)

{

fflush(stdin);

printf("请输入插入学生的信息\n");

op=creat_3();

printf("\n");

printf("新输入的学生信息需插入哪位学生之前,请输入name:\n");

gets(c);

head=insert(head,op,c);

printf("\n");

printf("继续插入学生信息请按 y,否则按 回车 结束插入\n");

gets(c);

}

prlist(head);

printf("返回上级菜单请按 y,否则回车结束程序\n");

gets(c);

if(strlen(c)!=0)

{

select();

}

break;

case 6:head=rea();

printf("被修改学生名字为:");

gets(c);

while(strlen(c)!=0)

{

head=change(head,c);

printf("若需继续修改学生信息,请输入需被修改学生的名字,否则按 回车 结束:");

fflush(stdin);

gets(c);

}

prlist(head);

printf("返回上级菜单请按 y,否则回车结束程序\n");

gets(c);

if(strlen(c)!=0)

{

select();

}

break;

case 7:head=rea();

printf("name\tnum\tsub1\t sub2\t sub3\t aver\n");

zhibaio(head);

printf("返回上级菜单请按 y,否则回车结束程序\n");

gets(c);

if(strlen(c)!=0)

{

select();

}

break;

}

}

void main()

{

select();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: