《一个小型的学生管理系统》

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define SIZE 2

//函数声明
void add(); //添加学生信息函数
void show(); //显示学生信息函数
void fail(); //不及格学生信息函数
void excellent(); //优秀学生信息函数
void Delete(); //删除指定学生信息函数
void Exit(); //退出学生管理系统

//结构体。
struct Student
{
int num; //学号
char name[20]; //姓名
char sex[10]; //性别
int age; //年龄
char add[20]; //地址
float score[3]; //成绩
}stu[SIZE];


//界面函数
void interFace()
{
printf(" ******************************************");
printf(" ******************************************");
printf(" ***欢迎进入学生管理系统*** ");
printf(" ****************************************** ");
printf(" ****************************************** ");
printf(" <1>添加学生信息 <2>显示学生信息 ");
printf(" <3>不及格的学生有 <4>优秀的学生有(每门课程都在85分以上) ");
printf(" <5>删除指定学生信息 <6>退出系统 ");

//选择函数声明。
void select();
//调用选择函数。
select();
}

//添加学生信息的函数。
void add()
{
int i;
printf("请输入学生的信息: ");
for(i=0;i<SIZE;i++)
{
printf("学号:");
scanf("%d",&stu[i].num);
printf("姓名:");
scanf("%s",stu[i].name);
printf("年龄:");
scanf("%d",&stu[i].age);
printf("性别:");
scanf("%s",stu[i].sex);
printf("语文成绩:");
scanf("%f",&stu[i].score[0]);
printf("数学成绩:");
scanf("%f",&stu[i].score[1]);
printf("英语成绩:");
scanf("%f",&stu[i].score[2]);
//打开文件操作
FILE *fp;
if((fp = fopen("F:\FILE.txt","wb"))==NULL)
{
printf("cannot open file ");
}

for(i=0;i<SIZE;i++)
if(fwrite(&stu[i],sizeof(struct Student),1,fp)!=1)
printf("file write error! ");
fclose(fp);
system("cls"); //清屏
interFace(); //调用界面函数
}
}

//显示学生信息。
void show()
{
FILE *fp;
int i;
if((fp = fopen("F:\FILE.txt","rb"))==NULL)
{
printf("cannot open file ");
}
printf("学号 姓名 年龄 性别 语文成绩 数学成绩 英语成绩 ");
printf(" ");
for(i=0;i<SIZE;i++)
{
fread(&stu[i],sizeof(struct Student),1,fp);
printf(" %2d %-6s %4d %-6s %4.2f %4.2f %4.2f ",stu[i].num,stu[i].name,
stu[i].age,stu[i].sex,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
}
fclose(fp);
printf("按任意键返回:");
getch();
system("cls");
interFace();
}

//不及格学生信息函数。
void fail()
{
int i;
FILE *fp;
if((fp = fopen("F:\FILE.txt","r"))==NULL)
{
printf("cannot open file! ");
}
printf("不及格学生信息如下:");
printf("学号 姓名 年龄 性别 语文成绩 数学成绩 英语成绩");
printf(" ");
for(i=0;i<SIZE;i++)
{
fread(&stu[i],sizeof(struct Student),1,fp);
if(stu[i].score[0]<60 || stu[i].score[1]<60 || stu[i].score[2]<60)
printf(" %2d %-6s %4d %-6s %4.2f %4.2f %4.2f ",
stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].score[0],
stu[i].score[1],stu[i].score[2]);
}
fclose(fp);
printf("按任意键返回:");
getch();
system("cls");
interFace();
}

//优秀学生信息如下。
void excellent()
{
int i;
FILE *fp;
if((fp = fopen("F:\FILE.txt","r"))==NULL)
{
printf("cannot open file! ");
}
printf("优秀学生信息如下:");
printf("学号,姓名,年龄,性别, 语文成绩, 数学成绩, 英语成绩");
printf(" ");
for(i=0;i<SIZE;i++)
{
fread(&stu[i],sizeof(struct Student),1,fp);
if(stu[i].score[0]>85 || stu[i].score[1]>85 || stu[i].score[2]>85)
printf(" %2d %-6s %4d %-6s %4.2f %4.2f %4.2f ",
stu[i].num,stu[i].name,stu[i].age,stu[i].sex,stu[i].score[0],
stu[i].score[1],stu[i].score[2]);
}
fclose(fp);
printf("按任意键返回:");
getch();
system("cls");
interFace();

}

//删除指定学生的信息。
void Delete()
{
int i,n;
FILE *fp;
if((fp = fopen("F:\FILE.txt","r"))==NULL)
{
printf("cannot open file ");
}
printf("请输入要删除的学生的学号:");
scanf("%d",&n);
for(i=0;i<SIZE;i++)
{
fread(&stu[i],sizeof(struct Student),1,fp);
if(stu[i].num == n)
continue;
printf(" %2d %-6s %4d %-6s %4.2f %4.2f %4.2f ",stu[i].num,stu[i].name,
stu[i].age,stu[i].sex,stu[i].score[0],stu[i].score[1],stu[i].score[2]);

}
fclose(fp);
printf("按任意键返回:");
getch();
system("cls");
interFace();
}

//退出系统。
void Exit()
{
printf("按任意键退出");
exit(0);
}

//选择操作方式函数。
void select()
{
int n;
printf("Enter the integer number:(1~6)");
scanf("%d",&n);
switch(n)
{
case 1:system("cls"); add(); break;
case 2:system("cls"); show(); break;
case 3:system("cls"); fail(); break;
case 4:system("cls"); excellent(); break;
case 5:system("cls"); Delete(); break;
case 6:system("cls"); Exit(); break;
default:printf("error!"); break;
}
}


//主函数
int main()
{
interFace();
return 0;
}

/*
void add()
{
int i;
printf("请输入学生的信息:学号,姓名,年龄,性别,语文成绩,数学成绩,英语成绩): ");
for(i=0;i<SIZE;i++)
{
printf("学号:");
scanf("%d",&stu[i].num);
printf("姓名:");
scanf("%s",stu[i].name);
printf("年龄:");
scanf("%d",&stu[i].age);
printf("性别:");
scanf("%s",stu[i].sex);
printf("语文成绩:");
scanf("%f",stu[i].score1);
printf("数学成绩:");
scanf("%f",stu[i].score2);
printf("英语成绩:");
scanf("%f",stu[i].score3);
//打开文件操作。
FILE *fp;
if((fp = fopen("F:\FILE.txt","wb"))==NULL)
{
printf("cannot open file ");
}

for(i=0;i<SIZE;i++)
if(fwrite(&stu[i],sizeof(struct Student),1,fp)!=1)
{
printf("file write error! ");
}
fclose(fp);
system("cls"); //清屏。
interFace(); //调用界面函数。

}
*/

原文地址:https://www.cnblogs.com/sun-/p/4823226.html