C语言学生成绩管理系统代码 急!!!!!!

2024-11-24 19:51:57
推荐回答(4个)
回答1:

参考
#include "stdio.h"

/*定义学生结构体*/

struct Student

{

char ID[20];

char Name[20];

float Mark1;

float Mark2;

float Mark3;

float Average;

};

/*声明学生数组及学生数量*/

struct Student students[1000];

int num=0;

/*求平均值*/

float Avg(struct Student stu)

{

return (stu.Mark1+stu.Mark2+stu.Mark3)/3;

}

/*通过学号返回数组下标*/

int Student_SearchByIndex(char id[])

{

int i;

for (i=0;i
{

if (strcmp(students[i].ID,id)==0)

{

return i;

}

}

return -1;

}

/*通过姓名返回数组下标*/

int Student_SearchByName(char name[])

{

int i;

for (i=0;i
{

if (strcmp(students[i].Name,name)==0)

{

return i;

}

}

return -1;

}

/*显示单条学生记录*/

void Student_DisplaySingle(int index)

{

printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","成绩","成绩","成绩","平均成绩");

printf("-------------------------------------------------------------\n");

printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[index].ID,students[index].Name,

students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average);

}

/*插入学生信息*/

void Student_Insert()

{

while(1)

{

printf("请输入学号:");

scanf("%s",&students[num].ID);

getchar();

printf("请输入姓名:");

scanf("%s",&students[num].Name);

getchar();

printf("请输入成绩:");

scanf("%f",&students[num].Mark1);

getchar();

printf("请输入成绩:");

scanf("%f",&students[num].Mark2);

getchar();

printf("请输入成绩:");

scanf("%f",&students[num].Mark3);

getchar();

students[num].Average=Avg(students[num]);

num++;

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*修改学生信息*/

void Student_Modify()

{

float mark1,mark2,mark3;

while(1)

{

char id[20];

int index;

printf("请输入要修改的学生的学号:");

scanf("%s",&id);

getchar();

index=Student_SearchByIndex(id);

if (index==-1)

{

printf("学生不存在!\n");

}

else

{

printf("你要修改的学生信息为:\n");

Student_DisplaySingle(index);

printf("-- 请输入新值--\n");

printf("请输入学号:");

scanf("%s",&students[index].ID);

getchar();

printf("请输入姓名:");

scanf("%s",&students[index].Name);

getchar();

printf("请输入成绩:");

scanf("%f",&students[index].Mark1);

getchar();

printf("请输入成绩:");

scanf("%f",&students[index].Mark2);

getchar();

printf("请输入成绩:");

scanf("%f",&students[index].Mark3);

getchar();

students[index].Average=Avg(students[index]);

}

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*删除学生信息*/

void Student_Delete()

{

int i;

while(1)

{

char id[20];

int index;

printf("请输入要删除的学生的学号:");

scanf("%s",&id);

getchar();

index=Student_SearchByIndex(id);

if (index==-1)

{

printf("学生不存在!\n");

}

else

{

printf("你要删除的学生信息为:\n");

Student_DisplaySingle(index);

printf("是否真的要删除?(y/n)");

if (getchar()=='y')

{

for (i=index;i
{

students[i]=students[i+1];

}

num--;

}

getchar();

}

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*按姓名查询*/

void Student_Select()

{

while(1)

{

char name[20];

int index;

printf("请输入要查询的学生的姓名:");

scanf("%s",&name);

getchar();

index=Student_SearchByName(name);

if (index==-1)

{

printf("学生不存在!\n");

}

else

{

printf("你要查询的学生信息为:\n");

Student_DisplaySingle(index);

}

printf("是否继续?(y/n)");

if (getchar()=='n')

{

break;

}

}

}

/*按平均值排序*/

void Student_SortByAverage()

{

int i,j;

struct Student tmp;

for (i=0;i
{

for (j=1;j
{

if (students[j-1].Average
{

tmp=students[j-1];

students[j-1]=students[j];

students[j]=tmp;

}

}

}

}

/*显示学生信息*/

void Student_Display()

{

int i;

printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","成绩","成绩","成绩","平均成绩");

printf("-------------------------------------------------------------\n");

for (i=0;i
{

printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[i].ID,students[i].Name,

students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average);

}

}

/*将学生信息从文件读出*/

void IO_ReadInfo()

{

FILE *fp;

int i;

if ((fp=fopen("Database.txt","rb"))==NULL)

{

printf("不能打开文件!\n");

return;

}

if (fread(&num,sizeof(int),1,fp)!=1)

{

num=-1;

}

else

{

for(i=0;i
{

fread(&students[i],sizeof(struct Student),1,fp);

}

}

fclose(fp);

}

/*将学生信息写入文件*/

void IO_WriteInfo()

{

FILE *fp;

int i;

if ((fp=fopen("Database.txt","wb"))==NULL)

{

printf("不能打开文件!\n");

return;

}

if (fwrite(&num,sizeof(int),1,fp)!=1)

{

printf("写入文件错误!\n");

}

for (i=0;i
{

if (fwrite(&students[i],sizeof(struct Student),1,fp)!=1)

{

printf("写入文件错误!\n");

}

}

fclose(fp);

}

/*主程序*/

main()

{

int choice;

IO_ReadInfo();

while(1)

{

/*主菜单*/

printf("\n------ 学生成绩管理系统------\n");

printf("1. 增加学生记录\n");

printf("2. 修改学生记录\n");

printf("3. 删除学生记录\n");

printf("4. 按姓名查询学生记录\n");

printf("5. 按平均成绩排序\n");

printf("6. 退出\n");

printf("请选择(1-6):");

scanf("%d",&choice);

getchar();

switch(choice)

{

case 1:

Student_Insert();

break;

case 2:

Student_Modify();

break;

case 3:

Student_Delete();

break;

case 4:

Student_Select();

break;

case 5:

Student_SortByAverage();

Student_Display();

break;

case 6:

exit();

break;

}

IO_WriteInfo();

}

}

回答2:

我有个现成的,不一定符合你的要求,呵呵
/*11.3.2 源程序*/
/***********xuesheng.c***********/
/******头文件(.h)***********/
#include "stdio.h" /*I/O函数*/
#include "stdlib.h" /*其它说明*/
#include "string.h" /*字符串函数*/
#include "conio.h" /*屏幕操作函数*/
#include "mem.h" /*内存操作函数*/
#include "ctype.h" /*字符操作函数*/
#include "alloc.h" /*动态地址分配函数*/
#define N 3 /*定义常数*/
typedef struct z1 /*定义数据结构*/
{
char no[11];
char name[15];
int score[N];
float sum;
float average;
int order;
struct z1 *next;
}STUDENT;
/*以下是函数原型*/
STUDENT *init(); /*初始化函数*/
STUDENT *create(); /*创建链表*/
STUDENT *delete(STUDENT *h); /*删除记录*/
void print(STUDENT *h); /* 显示所有记录*/
void search(STUDENT *h); /*查找*/
void save(STUDENT *h); /*保存*/
STUDENT *load(); /*读入记录*/
void computer(STUDENT *h); /*计算总分和均分*/
STUDENT *insert(STUDENT *h); /*插入记录*/
void append(); /*追加记录*/
void copy(); /*复制文件*/
STUDENT *sort(STUDENT *h); /*排序*/
STUDENT *index(STUDENT *h); /*索引*/
void total(STUDENT *h); /*分类合计*/
int menu_select(); /*菜单函数*/
/******主函数开始*******/
main()
{
int i;
STUDENT *head; /*链表定义头指针*/
head=init(); /*初始化链表*/
clrscr(); /*清屏*/
for(;;) /*无限循环*/
{
switch(menu_select()) /*调用主菜单函数,返回值整数作开关语句的条件*/
{ /*值不同,执行的函数不同,break 不能省略*/
case 0:head=init();break; /*执行初始化*/
case 1:head=create();break; /*创建链表*/
case 2:head=delete(head);break; /*删除记录*/
case 3:print(head);break; /*显示全部记录*/
case 4:search(head);break; /*查找记录*/
case 5:save(head);break; /*保存文件*/
case 6:head=load(); break; /*读文件*/
case 7:computer(head);break; /*计算总分和均分*/
case 8:head=insert(head); break; /*插入记录*/
case 9:copy();break; /*复制文件*/
case 10:head=sort(head);break; /*排序*/
case 11:append();break; /*追加记录*/
case 12:head=index(head);break; /*索引*/
case 13:total(head);break; /*分类合计*/
case 14:exit(0); /*如菜单返回值为14程序结束*/
}
}
}
/*菜单函数,返回值为整数*/
menu_select()
{
char *menu[]={"***************MENU***************", /*定义菜单字符串数组*/
" 0. init list", /*初始化*/
" 1. Enter list", /*输入记录*/
" 2. Delete a record from list", /*从表中删除记录*/
" 3. print list ", /*显示单链表中所有记录*/
" 4. Search record on name", /*按照姓名查找记录*/
" 5. Save the file", /*将单链表中记录保存到文件中*/
" 6. Load the file", /*从文件中读入记录*/
" 7. compute the score", /*计算所有学生的总分和均分*/
" 8. insert record to list ", /*插入记录到表中*/
" 9. copy the file to new file", /*复制文件*/
" 10. sort to make new file", /*排序*/
" 11. append record to file", /*追加记录到文件中*/
" 12. index on nomber", /*索引*/
" 13. total on nomber", /*分类合计*/
" 14. Quit"}; /*退出*/
char s[3]; /*以字符形式保存选择号*/
int c,i; /*定义整形变量*/
gotoxy(1,25); /*移动光标*/
printf("press any key enter menu......\n"); /*压任一键进入主菜单*/
getch(); /*输入任一键*/
clrscr(); /*清屏幕*/
gotoxy(1,1); /*移动光标*/
textcolor(YELLOW); /*设置文本显示颜色为黄色*/
textbackground(BLUE); /*设置背景颜色为蓝色*/
gotoxy(10,2); /*移动光标*/
putch(0xc9); /*输出左上角边框┏*/
for(i=1;i<44;i++)
putch(0xcd); /*输出上边框水平线*/
putch(0xbb); /*输出右上角边框 ┓*/
for(i=3;i<20;i++)
{
gotoxy(10,i);putch(0xba); /*输出左垂直线*/
gotoxy(54,i);putch(0xba);
} /*输出右垂直线*/
gotoxy(10,20);putch(0xc8); /*输出左上角边框┗*/
for(i=1;i<44;i++)
putch(0xcd); /*输出下边框水平线*/
putch(0xbc); /*输出右下角边框┛*/
window(11,3,53,19); /* 制作显示菜单的窗口,大小根据菜单条数设计*/
clrscr(); /*清屏*/
for(i=0;i<16;i++) /*输出主菜单数组*/
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK); /*设置背景颜色为黑色*/
window(1,1,80,25); /*恢复原窗口大小*/
gotoxy(10,21); /*移动光标*/
do{
printf("\n Enter you choice(0~14):"); /*在菜单窗口外显示提示信息*/
scanf("%s",s); /*输入选择项*/
c=atoi(s); /*将输入的字符串转化为整形数*/
}while(c<0||c>14); /*选择项不在0~14之间重输*/
return c; /*返回选择项,主程序根据该数调用相应的函数*/
}
STUDENT *init()
{
return NULL;
}

/*创建链表*/
STUDENT *create()
{
int i; int s;
STUDENT *h=NULL,*info; /* STUDENT指向结构体的指针*/
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申请空间*/
if(!info) /*如果指针info为空*/
{
printf("\nout of memory"); /*输出内存溢出*/
return NULL; /*返回空指针*/
}
inputs("enter no:",info->no,11); /*输入学号并校验*/
if(info->no[0]=='@') break; /*如果学号首字符为@则结束输入*/
inputs("enter name:",info->name,15); /*输入姓名,并进行校验*/
printf("please input %d score \n",N); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i {
do{
printf("score%d:",i+1); /*提示输入第几门课程*/
scanf("%d",&info->score[i]); /*输入成绩*/
if(info->score[i]>100||info->score[i]<0) /*确保成绩在0~100之间*/
printf("bad data,repeat input\n"); /*出错提示信息*/
}while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i]; /*累加各门课程成绩*/
}
info->sum=s; /*将总分保存*/
info->average=(float)s/N; /*求出平均值*/
info->order=0; /*未排序前此值为0*/
info->next=h; /*将头结点做为新输入结点的后继结点*/
h=info; /*新输入结点为新的头结点*/
}
return(h); /*返回头指针*/
}
/*输入字符串,并进行长度验证*/
inputs(char *prompt, char *s, int count)
{
char p[255];
do{
printf(prompt); /*显示提示信息*/
scanf("%s",p); /*输入字符串*/
if(strlen(p)>count)printf("\n too long! \n"); /*进行长度校验,超过count值重输入*/
}while(strlen(p)>count);
strcpy(s,p); /*将输入的字符串拷贝到字符串s中*/
}
/*输出链表中结点信息*/
void print(STUDENT *h)
{
int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr(); /*清屏*/
p=h; /*初值为头指针*/
printf("\n\n\n****************************STUDENT********************************\n");
printf("|rec|nO | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|---|----------|---------------|----|----|----|--------|-------|-----|\n");
while(p!=NULL)
{
i++;
printf("|%3d |%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", i, p->no,p->name,p->score[0],p->score[1],
p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("**********************************end*********************************\n");
}
/*删除记录*/
STUDENT *delete(STUDENT *h)
{
STUDENT *p,*q; /*p为查找到要删除的结点指针,q为其前驱指针*/
char s[11]; /*存放学号*/
clrscr(); /*清屏*/
printf("please deleted no\n"); /*显示提示信息*/
scanf("%s",s); /*输入要删除记录的学号*/
q=p=h; /*给q和p赋初值头指针*/
while(strcmp(p->no,s)&&p!=NULL) /*当记录的学号不是要找的,或指针不为空时*/
{
q=p; /*将p指针值赋给q作为p的前驱指针*/
p=p->next; /*将p指针指向下一条记录*/
}
if(p==NULL) /*如果p为空,说明链表中没有该结点*/
printf("\nlist no %s student\n",s);
else /*p不为空,显示找到的记录信息*/
{
printf("*****************************have found***************************\n");
printf("|no | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|----------|---------------|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,
p->average,p->order);
printf("********************************end*******************************\n");
getch(); /*压任一键后,开始删除*/
if(p==h) /*如果p==h,说明被删结点是头结点*/
h=p->next; /*修改头指针指向下一条记录*/
else
q->next=p->next; /*不是头指针,将p的后继结点作为q的后继结点*/
free(p); /*释放p所指结点空间*/
printf("\n have deleted No %s student\n",s);
printf("Don't forget save\n");/*提示删除后不要忘记保存文件*/
}
return(h); /*返回头指针*/
}

回答3:

i++;
}while(i<=maxTag);
i=writeData();
if(i==1)
{
printf("查找成功!\n");
}
else
{
printf("查找失败!\n");
}
printf("是否要继续:是->y,否->n");
fflush(stdin);
scanf("%c",&answer);
if(answer=='y')
{
continue;
}
else
{
break;
}
}
}
//修改学生
void updateStudent(int n)
{
struct student h;
int chazhao,i,g;
char answer;
if((fp=fopen("studentinfo.txt","w"))==NULL)
{
//提示错误
printf("打开文件失败!");
}
while(1)
{
maxTag=n;
printf("请入将要查找的学生学号:");
scanf("%d",&chazhao);
for(i=0;i<=maxTag;i++)
{
if(students[i].no==chazhao)
{
printf("请重新输入该学生的信息: 学号 姓名 性别 年龄 成绩\n");
scanf("%d %s %c %d %f",&h.no,h.name,&h.sex,&h.age,&h.score);
students[i]=h;
}
}
g=writeData();
if(g==1)
{
printf("修改成功!\n");
}
else
{
printf("修改失败!\n");
}
printf("是否要继续:是->y,否->n");
fflush(stdin);
scanf("%c",&answer);
if(answer=='y')
{
continue;
}
else
{
break;
}
}
fclose(fp);

}
//排序
void sort(int n)
{
int a,g,k;
maxTag=n;
if((fp=fopen("studentinfo.txt","w"))==NULL)
{
//提示错误
printf("打开文件失败!");
}
printf("排序的方式:\n1.学号\n2.成绩\n3.年龄\n");
scanf("%d",&a);
if(a==1)
{
for(g=0;g<=maxTag;g++)
{
for(k=0;k<=maxTag-1;k++)
{
if(students[k].no>students[k+1].no)
{
swap(students[k].no,students[k+1].no,k);
}
}
}
}
if(a==2)
{
for(g=0;g<=maxTag;g++)
{
for(k=0;k<=maxTag-1;k++)
{
swap1(students[k].score,students[k+1].score,k);
}
}
}
if(a==3)
{
for(g=0;g<=maxTag;g++)
{
for(k=0;k<=maxTag-1;k++)
{
swap(students[k].age,students[k+1].age,k);
}
}
}
fclose(fp);
g=writeData();
if(g==1)
{
printf("排序成功!\n");
}
else
{
printf("排序失败!\n");
}
displayAll(maxTag);
}
//读文件,返回最大下标
int readData()
{
int i=0;
//打开文件
if((fp=fopen("studentinfo.txt","r"))==NULL)
{
//提示错误
printf("打开文件失败!");
exit(1);//退出程序
}
//读文件,不确定文件有多少信息,使用循环
while(1)
{

students[i].no,
students[i].name,
students[i].sex,
students[i].age,
students[i].score);
}
//关闭文件
fclose(fp);
return 1;
}
// 查找位置
int findIndex(int no)
{
return 0;
}
//显示当前信息
void displayCurrent(int tag) //tag表示下标
{
printf("%-8s%-8s%-8s%-8s%-8s\n","学号","姓名","性别","年龄","成绩");
printf("%-8d%-8s%-8c%-8d%-8.2f\n",
students[tag].no,
students[tag].name,
students[tag].sex,
students[tag].age,
students[tag].score);
}
void displayAll(int m)//显示所有信息
{
int i;
maxTag=m;
printf("%-8s%-8s%-8s%-8s%-8s\n","学号","姓名","性别","年龄","成绩");
for(i=0;i<=maxTag;i++)
{
printf("%-8d%-8s%-8c%-8d%-8.2f\n",
students[i].no,
students[i].name,
students[i].sex,
students[i].age,
students[i].score);
}
}
//按照学号或年龄排序
void swap(int m,int n,int d)
{
int i;
struct student h;
i=d;
if(m>n)
{
h=students[i];
students[i]=students[i+1];
students[i+1]=h;
}
}
//按成绩排序
void swap1(float m,float n,int d)
{
int i;
struct student h;
i=d;
if(m>n)
{
h=students[i];
students[i]=students[i+1];
students[i+1]=h;
}
}

回答4:

参考
#include
"stdio.h"
/*定义学生结构体*/
struct
Student
{
char
ID[20];
char
Name[20];
float
Mark1;
float
Mark2;
float
Mark3;
float
Average;
};
/*声明学生数组及学生数量*/
struct
Student
students[1000];
int
num=0;
/*求平均值*/
float
Avg(struct
Student
stu)
{
return
(stu.Mark1+stu.Mark2+stu.Mark3)/3;
}
/*通过学号返回数组下标*/
int
Student_SearchByIndex(char
id[])
{
int
i;
for
(i=0;i{
if
(strcmp(students[i].ID,id)==0)
{
return
i;
}
}
return
-1;
}
/*通过姓名返回数组下标*/
int
Student_SearchByName(char
name[])
{
int
i;
for
(i=0;i{
if
(strcmp(students[i].Name,name)==0)
{
return
i;
}
}
return
-1;
}
/*显示单条学生记录*/
void
Student_DisplaySingle(int
index)
{
printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","成绩","成绩","成绩","平均成绩");
printf("-------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[index].ID,students[index].Name,
students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Average);
}
/*插入学生信息*/
void
Student_Insert()
{
while(1)
{
printf("请输入学号:");
scanf("%s",&students[num].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[num].Name);
getchar();
printf("请输入成绩:");
scanf("%f",&students[num].Mark1);
getchar();
printf("请输入成绩:");
scanf("%f",&students[num].Mark2);
getchar();
printf("请输入成绩:");
scanf("%f",&students[num].Mark3);
getchar();
students[num].Average=Avg(students[num]);
num++;
printf("是否继续?(y/n)");
if
(getchar()=='n')
{
break;
}
}
}
/*修改学生信息*/
void
Student_Modify()
{
float
mark1,mark2,mark3;
while(1)
{
char
id[20];
int
index;
printf("请输入要修改的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if
(index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要修改的学生信息为:\n");
Student_DisplaySingle(index);
printf("--
请输入新值--\n");
printf("请输入学号:");
scanf("%s",&students[index].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[index].Name);
getchar();
printf("请输入成绩:");
scanf("%f",&students[index].Mark1);
getchar();
printf("请输入成绩:");
scanf("%f",&students[index].Mark2);
getchar();
printf("请输入成绩:");
scanf("%f",&students[index].Mark3);
getchar();
students[index].Average=Avg(students[index]);
}
printf("是否继续?(y/n)");
if
(getchar()=='n')
{
break;
}
}
}
/*删除学生信息*/
void
Student_Delete()
{
int
i;
while(1)
{
char
id[20];
int
index;
printf("请输入要删除的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if
(index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要删除的学生信息为:\n");
Student_DisplaySingle(index);
printf("是否真的要删除?(y/n)");
if
(getchar()=='y')
{
for
(i=index;i{
students[i]=students[i+1];
}
num--;
}
getchar();
}
printf("是否继续?(y/n)");
if
(getchar()=='n')
{
break;
}
}
}
/*按姓名查询*/
void
Student_Select()
{
while(1)
{
char
name[20];
int
index;
printf("请输入要查询的学生的姓名:");
scanf("%s",&name);
getchar();
index=Student_SearchByName(name);
if
(index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要查询的学生信息为:\n");
Student_DisplaySingle(index);
}
printf("是否继续?(y/n)");
if
(getchar()=='n')
{
break;
}
}
}
/*按平均值排序*/
void
Student_SortByAverage()
{
int
i,j;
struct
Student
tmp;
for
(i=0;i{
for
(j=1;j{
if
(students[j-1].Average{
tmp=students[j-1];
students[j-1]=students[j];
students[j]=tmp;
}
}
}
}
/*显示学生信息*/
void
Student_Display()
{
int
i;
printf("%10s%10s%8s%8s%8s%10s\n","学号","姓名","成绩","成绩","成绩","平均成绩");
printf("-------------------------------------------------------------\n");
for
(i=0;i{
printf("%10s%10s%8.2f%8.2f%8.2f%10.2f\n",students[i].ID,students[i].Name,
students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Average);
}
}
/*将学生信息从文件读出*/
void
IO_ReadInfo()
{
FILE
*fp;
int
i;
if
((fp=fopen("Database.txt","rb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if
(fread(&num,sizeof(int),1,fp)!=1)
{
num=-1;
}
else
{
for(i=0;i{
fread(&students[i],sizeof(struct
Student),1,fp);
}
}
fclose(fp);
}
/*将学生信息写入文件*/
void
IO_WriteInfo()
{
FILE
*fp;
int
i;
if
((fp=fopen("Database.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if
(fwrite(&num,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for
(i=0;i{
if
(fwrite(&students[i],sizeof(struct
Student),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}
/*主程序*/
main()
{
int
choice;
IO_ReadInfo();
while(1)
{
/*主菜单*/
printf("\n------
学生成绩管理系统------\n");
printf("1.
增加学生记录\n");
printf("2.
修改学生记录\n");
printf("3.
删除学生记录\n");
printf("4.
按姓名查询学生记录\n");
printf("5.
按平均成绩排序\n");
printf("6.
退出\n");
printf("请选择(1-6):");
scanf("%d",&choice);
getchar();
switch(choice)
{
case
1:
Student_Insert();
break;
case
2:
Student_Modify();
break;
case
3:
Student_Delete();
break;
case
4:
Student_Select();
break;
case
5:
Student_SortByAverage();
Student_Display();
break;
case
6:
exit();
break;
}
IO_WriteInfo();
}
}