#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();
}
}
#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();
}
}
你的串号我已经记下,采纳后我会帮你制作
随便写了些
不满意你自己在改改吧
#include
#include
#include
#include
typedef
struct
student{
int
Num;
char
name[12];
int
age;
struct
student*
pNext;
}STUDENT,*PSTUDENT;
#define
LEN
sizeof(STUDENT)
PSTUDENT
pHeader,pNail;
int
n=0;
void
PrintMenu();
bool
ExistStudent(int
Num);
void
InsertInfo();
bool
DeleteInfo(int
Num);
bool
UpdateInfo(int
Num,PSTUDENT
pNewInfo);
void
PrintInfo();
int
main()
{
PrintMenu();
int
iSel=0;
while(1)
{
printf("输入操作选择:");
scanf("%d",&iSel);
switch(iSel)
{
case
0:
return
0;
case
1:
PrintInfo();
continue;
case
2:
InsertInfo();
continue;
case
3:
{
int
num=0;
printf("输入要删除的学生学号:");
scanf("%d",&num);
if(DeleteInfo(num))
printf("删除成功!\n");
else
printf("删除失败!\n");
continue;
}
case
4:
{
int
num=0;
printf("输入要修改的学生学号:");
scanf("%d",&num);
STUDENT
stu={0};
printf("输入该学生的新信息(空格分隔):");
scanf("%d%s%d",&stu.Num,&stu.name,&stu.age);
if(UpdateInfo(num,&stu))
printf("修改成功!\n");
else
printf("修改失败!\n");
continue;
}
continue;
default:
continue;
}
}
return
0;
}
void
PrintMenu()
{
printf("******操作菜单******\n");
printf("*
1.
打印学生信息\n");
printf("*
2.
添加学生信息\n");
printf("*
3.
删除学生信息\n");
printf("*
4.
修改学生信息\n");
printf("*
0.
退出系统\n");
printf("*********************\n");
}
void
InsertInfo()
{
PSTUDENT
pCur;
printf("请输入学生记录(各元素用空格分隔,输入学号为0时停止)\n");
while(true)
{
pCur
=
(PSTUDENT)malloc(LEN);
memset(pCur,NULL,LEN);
printf("==>:");
scanf("%d%s%d",&pCur->Num,pCur->name,&pCur->age);
if(pCur->Num==0)
break;
if(ExistStudent(pCur->Num))
{
printf("该学生信息已经存在!\n");
break;
}
if(n==0)
{
pNail=pHeader=pCur;
pHeader->pNext=NULL;
}
else
{
pNail->pNext=pCur;
pNail=pCur;
pCur->pNext=NULL;
}
n++;
}
}
bool
DeleteInfo(int
Num)
{
PSTUDENT
pStu=pHeader;
if(!pStu)
return
false;
for(pStu;pStu!=NULL;pStu=pStu->pNext)
{
if(Num==pStu->Num)
{
pHeader=pStu->pNext;
return
true;
}
else
if(pStu->pNext&&Num==pStu->pNext->Num)
{
pStu->pNext=pStu->pNext->pNext;
return
true;
}
}
return
false;
}
bool
UpdateInfo(int
Num,PSTUDENT
pNewInfo)
{
PSTUDENT
pStu=pHeader;
if(!pStu)
return
false;
if(Num!=pNewInfo->Num&&ExistStudent(pNewInfo->Num))
return
false;
for(pStu;pStu!=NULL;pStu=pStu->pNext)
{
if(Num==pStu->Num)
{
pStu->Num=pNewInfo->Num;
memset(pStu->name,0,sizeof(pStu->name));
strcpy(pStu->name,pNewInfo->name);
pStu->age=pNewInfo->age;
return
true;
}
}
return
true;
}
void
PrintInfo()
{
PSTUDENT
pStu=pHeader;
if(!pStu)
{
printf("没有学生信息!\n");
return;
}
for(pStu;pStu!=NULL;pStu=pStu->pNext)
printf("学号:%d
姓名:%s
年龄:%d\n",pStu->Num,pStu->name,pStu->age);
}
bool
ExistStudent(int
Num)
{
PSTUDENT
pStu=pHeader;
if(!pStu)
return
false;
for(pStu;pStu!=NULL;pStu=pStu->pNext)
{
if(Num==pStu->Num)
return
true;
}
return
false;
}
int
main
(void)
//定义了
student
函数,用来管理学生信息
{
int
a,t1,t2,t3,t4
struct
student
*
st=(struct
student
*)malloc(sizeof(struct
student)*200);
//最难的语句
定义了动态的结构体数组
struct
student
t5;
//定义了结构体类型的临时变量t5
printf("欢迎来到学生信息管理系统!\n");
printf("请输入你想处理几个学生的信息?:");
scanf("%d",&t1);
//t1表示学生的个数
for
(t2=0;t2
{
printf("第%d个学生\n",t2+1);
printf("姓名:");
scanf("%s",st[t2].name);
printf("年龄:");
scanf("%d",&st[t2].age);
printf("成绩:");
scanf("%lf",&st[t2].score);
}
printf("\n\n你想升序排成绩,还是降序排成绩(升序请按1,降序请按其他字符)?:");
scanf("%d",&a);
//a用来判断升序还是降序
for
(t3=0;t3
for
(t4=t3+1;t4
if(a==1)
{
if(
(((st+t3))->score)
>
((st+t4)->score)
)
//t3,t4用来结束循环
{
t5=*(st+t3);
*(st+t3)=*(st+t4);
*(st+t4)=t5;
}
}
else
{
if(
(((st+t3))->score)
<
((st+t4)->score)
)
{
t5=*(st+t3);
*(st+t3)=*(st+t4);
*(st+t4)=t5;
}
}
}
}
for
(t2=0;t2
%lf
\n",t2+1,(st+t2)->name,(st+t2)->score);
return
0;
}
打字好累,希望帮你,谢谢