这是我写的学生管理系统的一个程序,但老师要求模块儿化编程;就是很多的点C和点H具体要怎么做呀?
#include
#include
#include
/********学生信息管理系统********
1. 添加信息
2. 删除信息
3. 修改(更新)信息
4. 查询信息
5. 输出所有学生信息
6. 插入新学员信息
0. 退出系统
********************************/
/*请输入你要想执行的操作(0-7):*/
typedef struct
{
long num;
char name[20];
float score;
}DATE;//定义结构体;
typedef struct tagSTUDENT
{
DATE date;
struct tagSTUDENT *pnext;
}STUDENT;//定义结构体;
int addstudent(STUDENT *ph);//添加学生;
STUDENT *creatlist (STUDENT *ph,int n);//创建链表函数的声明;
int print (STUDENT *ph);//输出函数的声明;
int del_stu(STUDENT *ph);//删除学生的声明;
int del_point(STUDENT *ph,int n);//删除链表节点的声明;
int up_stu(STUDENT *ph);//更新一个学生的信息:
int up_date (STUDENT*ph,int n);//更新一个学生的信息:
int seek_stu (STUDENT *ph);//查询一个学生的信息;
STUDENT* seek_point(STUDENT*ph,int n);//查询一个节点的信息;
int new_stu (STUDENT *ph);//插入一个学生的信息;
int new_point (STUDENT*ph,STUDENT*pn);//插入一个节点的信息;
int orders (STUDENT *ph);//按成绩从新排列学生的顺序;
STUDENT* maxd(STUDENT *p1,STUDENT *p2);//比较两个学生的分数;
int file_save(STUDENT *ph);//文件保存:
int file_read(STUDENT *ph);//文件读取;
int main ()
{
int i;
STUDENT*phead,*ptemp;
phead = ptemp = (STUDENT*)malloc(sizeof(STUDENT));
phead->pnext = NULL;
while (1)
{
printf("/*************************\n1.添加\t2.删除\t3.更新\t4.查询\
\n5.输出\t6.插入\t7.排序\t0.退出\n*************************/\n");
scanf("%d",&i);
switch(i)
{
case 1:addstudent(phead);break;
case 2:del_stu(phead);break;
case 3:up_stu(phead);break;
case 4:seek_stu(phead);break;
case 5:print(phead);break;
case 6:new_stu(phead);break;
case 7:orders(phead);break;
case 0:exit (0);
}
fflush(stdin);//输入缓冲区清空;
}
return 0;
}
//添加学生;
int addstudent(STUDENT *ph)
{
STUDENT *pt = ph;
STUDENT *pt_a = ph;
int n;
file_read(ph);
printf("输入添加学生的个数:");
scanf ("%d",&n);
pt = creatlist(ph,n);
printf("输入添加学生的信息:\n");
while (pt->pnext != NULL)
{
pt = pt->pnext;
scanf("%d %s %f",&pt->date.num,pt->date.name,&pt->date.score);
while (pt_a != pt)
{
if (pt->date.num == pt_a->date.num)
{
printf("输入错误,请从新输入!\n");
scanf("%d %s %f",&pt->date.num,pt->date.name,&pt->date.score);
}
pt_a = pt_a->pnext;
}
}
file_save(ph);
return 1;
}
//创建链表函数
STUDENT *creatlist(STUDENT *ph,int n)
{
int i;
STUDENT *pt = ph;
STUDENT *pta = ph;
while (pt->pnext != NULL)
{
pt = pt->pnext;
}
pta = pt;
for (i = 0;i < n;i++)
{
pt->pnext = (STUDENT*)malloc(sizeof(STUDENT));
pt = pt->pnext;
pt->pnext = NULL;
}
return pta;
}
/**********输出学生信息函数*******/
int print (STUDENT* ph)
{
STUDENT*pt = ph;
file_read(ph);
if (pt->pnext == NULL)
{
printf("没有输入学生:\n");
return 0;
}
printf("输出全部学生的信息:\n");
pt = ph->pnext;
while (pt != NULL)
{
printf("学号:%d 姓名:%s 分数:%.2f\n",pt->date.num,\
pt->date.name,pt->date.score);
pt = pt->pnext;
}
return 1;
}
/****删除一个学生******/
int del_stu(STUDENT *ph)
{
STUDENT *pta = ph;
long int n;
file_read(ph);
if(pta->pnext == NULL)
{
printf("错误操作\n");
return 0;
}
printf("请输入要删除学生的学号:\n");
scanf("%d",&n);
n = del_point(ph,n);
if(n == 1)
printf("已删除:\n");
else
printf("没有这个学生:\n");
file_save(ph);
return 1;
}
/*****删除一个节点的函数:******/
int del_point(STUDENT* ph,int n)
{
STUDENT *ptb,*pta = ph;
ptb = pta;
pta = pta->pnext;
while (pta != NULL)
{
if(pta->date.num == n)
{
ptb->pnext = pta->pnext;
free(pta);
return 1;
}
ptb = pta;
pta = pta->pnext ;
}
return 0;
}
/****更新一个学生的信息******/
int up_stu(STUDENT *ph)
{
STUDENT *pta = ph;
long int n;
file_read(ph);
if(pta->pnext == NULL)
{
printf("错误操作\n");
return 0;
}
printf("请输入要更新学生的学号:\n");
scanf("%d",&n);
n = up_date(ph,n);
if(n == 1)
printf("已更新:\n");
else
printf("没有这个学生:\n");
file_save(ph);
return 1;
}
//更新一个节点的函数:
int up_date (STUDENT*ph,int n)
{
STUDENT *pt = ph;
pt = pt->pnext;
while (pt != NULL)
{
if(pt->date.num == n)
{
printf("请从新输入这个学生的信息:\n");
scanf("%d %s %f",&pt->date.num,pt->date.name,&pt->date.score);
return 1;
}
pt = pt->pnext;
}
return 0;
}
//查找一个学生。
int seek_stu (STUDENT *ph)
{
long int n;
STUDENT *pt;
file_read(ph);//读取文件;
if(ph->pnext == NULL)
{
printf("没有这个学生\n");
return 0;
}
printf("请输入要查询学生的学号:\n");
scanf("%d",&n);
pt = seek_point(ph,n);
if (pt == NULL)
{
printf("没有这个学生:\n");
return 0;
}
printf("%d %s %.2f\n",pt->date.num,pt->date.name,pt->date.score);
return 1;
}
//查询一个节点的信息;
STUDENT* seek_point(STUDENT*ph,int n)
{
STUDENT *pt = ph;
pt = pt->pnext;
while (pt != NULL)
{
if(pt->date.num == n)
{
return pt;
}
pt = pt->pnext;
}
return NULL;
}
//文件保存
int file_save(STUDENT *ph)
{
FILE *fp;
if ((fp = fopen("student.txt","wb+")) == NULL)
{
printf("保存文件失败!");
return 0;
}
while (ph->pnext != NULL)
{
ph = ph->pnext;
fwrite(ph,sizeof(STUDENT),1,fp);
}
fclose(fp);
return 1;
}
//读取文件
int file_read(STUDENT *ph)
{
FILE *fp;
STUDENT *pt,*phead;
if ((fp = fopen("student.txt","rb+")) == NULL)
{
printf("读取文件失败!");
return 0;
}
phead = ph;
pt = phead->pnext;
while (!feof(fp))
{
pt = (STUDENT*)malloc(sizeof(STUDENT));
pt->pnext = NULL;
fread(pt,sizeof(STUDENT),1,fp);
if (feof(fp))
{
phead->pnext = NULL;
break;
}
phead->pnext = pt;
phead = pt;
pt->pnext = NULL;
}
if(fclose(fp))
{
printf("文件异常关闭");
return 0;
}
return 1;
}