#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 20
struct subject
{
int num;
char sname[20];
int score;
};
typedef struct node
{
int id;
char name[20];
char add[20];
char n;
struct subject subj[N];
struct node *next;
}linklist;
typedef struct indexs
{
int id;
int address;
struct indexs *next;
}index1;
linklist *head;
FILE *fp;
char filename[10];
FILE *fp2;
char filename2[20];
void main()
{
int x;
linklist *creat();
void search();
void index();
void edit();
void count();
void beifen();
void print2();
void print1();
system("cls");
printf("**********************学生管理系统****************************\n");
printf(" 1:输入学生信息\n");
printf(" 2:查找学生信息\n");
printf(" 3:学生信息索引\n");
printf(" 4:学生信息编辑\n");
printf(" 5:学生成绩统计\n");
printf(" 6:备份\n");
printf(" 7:退出\n");
printf(" 请输入你要操作的步骤:\n");
scanf("%d",&x);
getchar();
switch(x)
{
case 1:{
creat();
print2();
printf("\n按回车键返回主菜单:");
getchar();
main();
}
break;
case 2:{
search();
main();
}
break;
case 3:{
index();
print1();
printf("\n按回车键返回主菜单:");
getchar();
main();
}
break;
case 4:{
edit();
main();
}
break;
case 5:{
count();
main();
}
break;
case 6:{
beifen();
printf("\n按回车键返回主菜单:");
getchar();
main();
}
break;
case 7:break;
default:printf("输入错误!请重试");
}
}
linklist *creat()
{
int id;
int i;
linklist *r,*s;
linklist *subj;
printf("请输入文件名:");
gets(filename);
if((fp=fopen(filename,"w+"))==NULL)
{
printf("error!\n");
exit(0);
}
head=(linklist *)malloc(sizeof(linklist));
subj=(linklist *)malloc(sizeof(linklist));
r=head;
printf("输入学号:");
scanf("%d",&id);
getchar();
while (id!=0)
{
s=(linklist *)malloc(sizeof(linklist));
s->id=id;
printf("输入姓名:");
gets(s->name);
printf("输入学院:");
gets(s->add);
printf("输入该学生的课程数:");
scanf("%d",&s->n);
getchar();
fprintf(fp,"%d %s %s %d\n",id,s->name,s->add,s->n);
for(i=0;i<s->n;i++)
{
printf("课程号:");
scanf("%d",&s->subj[i].num);
getchar();
printf("课程名:");
gets(s->subj[i].sname);
printf("成绩:");
scanf("%d",&s->subj[i].score);
fprintf(fp,"%d %s %d\n",s->subj[i].num,s->subj[i].sname,s->subj[i].score);
}
r->next=s;
r=s;
printf("输入学生学号:");
scanf("%d",&id);
getchar();
}
r->next=NULL;
fclose(fp);
return head;
}
void print2()
{
int i;
linklist *p,*subj;
system("cls");
printf("操作后文件中学生信息是:\n");
printf("请输入文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
printf("打开文件错误!\n");
p=(linklist *)malloc(sizeof(linklist));
subj=(linklist *)malloc(sizeof(linklist));
while(feof(fp)==0)
{
fscanf(fp,"%d%s%s%d",&p->id,p->name,p->add,&p->n);
printf("学号:%d\n",p->id);
printf("姓名:%s\n",p->name);
printf("学院:%s\n",p->add);
printf("课程数:%d\n",p->n);
printf(" 课程号 课程名 成绩\n");
for(i=0;i<p->n;i++)
{
fscanf(fp,"%d%s%d\n",&p->subj[i].num,p->subj[i].sname,&p->subj[i].score);
printf(" %d %s %d\n",p->subj[i].num,p->subj[i].sname,p->subj[i].score);
}
}
fclose(fp);
}
void search()
{
int x;
void search_id();
void search_add();
void search_compose();
system("cls");
printf("*********************Search Menu***********************\n");
printf(" 1:按学号查找\n");
printf(" 2:按学院查找\n");
printf(" 3:组合查找\n");
printf(" 4:退出\n");
printf("输入您要进行的步骤:");
scanf("%d",&x);
getchar();
switch(x)
{
case 1:{
search_id();
printf("\n按回车键返回主菜单:");
getchar();
}
break;
case 2:{
search_add();
printf("\n按回车键返回主菜单:");
getchar();
}
break;
case 3:{
search_compose();
printf("\n按回车键返回主菜单:");
getchar();
}
break;
case 4:
break;
}
}
void search_id()
{
int id,i;
linklist *p,*subj;
printf("请输入文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
printf("打开文件错误!\n");
printf("输入你要查找学生的学号:");
scanf("%d",&id);
getchar();
p=(linklist *)malloc(sizeof(linklist));
subj=(linklist *)malloc(sizeof(linklist));
while(feof(fp)==0)
{
fscanf(fp,"%d%s%s%d",&p->id,p->name,p->add,&p->n);
for(i=0;i<p->n;i++)
fscanf(fp,"%d%s%d\n",&p->subj[i].num,p->subj[i].sname,&p->subj[i].score);
if(p->id==id)
{
printf("你要查找信息是:\n");
printf("学号:%d\n",p->id);
printf("姓名:%s\n",p->name);
printf("学院:%s\n",p->add);
printf("课程数:%d\n",p->n);
printf(" 课程号 课程名 成绩\n");
for(i=0;i<p->n;i++)
printf(" %d %s %d\n",p->subj[i].num,p->subj[i].sname,p->subj[i].score);
break;
}
}
fclose(fp);
}
void search_add()
{
char add[20];
int i;
linklist *p,*subj;
printf("请输入文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
printf("打开文件错误!\n");
printf("输入你要查找学生的学院:");
gets(add);
p=(linklist *)malloc(sizeof(linklist));
subj=(linklist *)malloc(sizeof(linklist));
printf("您要查找的学生信息是:\n");
while(feof(fp)==0)
{
fscanf(fp,"%d%s%s%d",&p->id,p->name,p->add,&p->n);
for(i=0;i<p->n;i++)
fscanf(fp,"%d%s%d\n",&p->subj[i].num,p->subj[i].sname,&p->subj[i].score);
if(strcmp(p->add,add)==0)
{
printf("姓名:%s\n",p->name);
}
}
fclose(fp);
}
void search_compose()
{
char add[20];
char name[20];
int i;
int num;
linklist *p,*subj;
printf("请输入文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
printf("打开文件错误!\n");
printf("请输入你要查找的学生姓名:");
gets(name);
printf("请输入你要查找的学生学院:");
gets(add);
printf("请输入你要查找的学生课程号:");
scanf("%d",&num);
getchar();
subj=(linklist *)malloc(sizeof(linklist));
printf("你要查找的学生信息为:");
while(feof(fp)==0)
{
p=(linklist *)malloc(sizeof(linklist));
fscanf(fp,"%d%s%s%d",&p->id,p->name,p->add,&p->n);
for(i=0;i<p->n;i++)
fscanf(fp,"%d%s%d\n",&p->subj[i].num,p->subj[i].sname,&p->subj[i].score);
if(strcmp(p->name,name)==0)
if(strcmp(p->add,add)==0)
for(i=0;i<p->n;i++)
{
if(p->subj[i].num==num)
{
printf("你要查找信息是:\n");
printf("学号:%d\n",p->id);
printf("姓名:%s\n",p->name);
printf("学院:%s\n",p->add);
printf("课程数:%d\n",p->n);
printf(" 课程号 课程名 成绩\n");
for(i=0;i<p->n;i++)
{
printf(" %d %s %d\n",p->subj[i].num,p->subj[i].sname,p->subj[i].score);
}
}
break;
}
}
fclose(fp);
}
index1 *selesort(index1 *h)
{
index1 *p,*q,*r,*min;
q=h;
min=h->next;
while(q->next!=NULL)
{
r=min;
p=min->next;
while(p!=NULL)
{
if(p->id<min->id)
{
r->next=p->next;
p->next=min;
q->next=p;
min=p;
p=r->next;
}
else
if(p->id==min->id)
{
r->next=p->next;
p->next=min->next;
min->next=p;
p=r->next;
q=min;
min=min->next;
}
else
{
r=p;
p=p->next;
}
}
q=min;
min=min->next;
}
return h;
}
void index()
{
int i;
linklist *subj,*r,*p;
index1 *p1,*r1,*h;
index1 *selesort(index1 *h);
system("cls");
subj=(linklist *)malloc(sizeof(linklist));
head=(linklist *)malloc(sizeof(linkl
- 1
- 2
前往页