|
|
#include<stdio.h>
|
|
|
#include<stdlib.h>
|
|
|
#include<string.h>
|
|
|
#include<conio.h>
|
|
|
#define MAX 2000
|
|
|
struct Student
|
|
|
{
|
|
|
int num; //学号
|
|
|
char name[8]; //姓名
|
|
|
char sex; //性别
|
|
|
int mark[4]; //三门课成绩,最后为平均成绩
|
|
|
};
|
|
|
int get_menu_choice() ;//接受菜单选择
|
|
|
void menu() ;//菜单响应
|
|
|
void main_menu(); //显示菜单
|
|
|
void Sort_menu();//排序菜单
|
|
|
int all; //总人数
|
|
|
char password[20];//密码
|
|
|
struct Student student[MAX];
|
|
|
void Init(); //初始化,文件不存在,则建立,同时记录下文件中的记录数
|
|
|
FILE *file_operate(const char *mode);//文件操作
|
|
|
void Delete(FILE *fp); //删除记录
|
|
|
void Alter(FILE *fp); //修改记录信息
|
|
|
void Show(); //显示打印所有的信息
|
|
|
void Save(); //备份信息到文件
|
|
|
void Find(); //查找记录的函数
|
|
|
void Input(FILE *fp); //向管理系统中增加记录
|
|
|
int Inputoneperson(int i); //向数组增加一条记录
|
|
|
void set_psw(); //设置密码
|
|
|
int key_check(); //密码验证
|
|
|
void exchange(int i,int j);//交换两个人的所有信息
|
|
|
void PrintTitle(); //打印头信息
|
|
|
void clear(); //清屏
|
|
|
void Show_student(int i); //显示打印一个人的信息
|
|
|
int CheckNumber(int num_temp); //检查学号是否存在,存在返回序号,不存在返回-1
|
|
|
void Inputfile(int i,FILE *fp); //把下标为i 的记录写入文件
|
|
|
void Readfile(int i,FILE *fp); //读一条记录从文件
|
|
|
int Sort_menu_choice();//选择是按学号还是成绩排序
|
|
|
void Sort_save();//排序后数据写入文件
|
|
|
void Sort_num();//选择是按学号排序
|
|
|
void Sort_mark();//选择是按成绩排序
|
|
|
main() //主函数
|
|
|
{
|
|
|
printf("\n");
|
|
|
printf(" |**************************************************************************|\n");
|
|
|
printf(" | |\n");
|
|
|
printf(" | |\n");
|
|
|
printf(" | |\n");
|
|
|
printf(" | 欢迎使用学生成绩管理系统 |\n");
|
|
|
printf(" | |\n");
|
|
|
printf(" | |\n");
|
|
|
printf(" | |\n");
|
|
|
printf(" |**************************************************************************|\n");
|
|
|
printf("\n");
|
|
|
if(key_check())
|
|
|
{
|
|
|
Init();
|
|
|
clear();
|
|
|
menu();
|
|
|
}
|
|
|
}
|
|
|
int get_menu_choice()//接受菜单选择
|
|
|
{
|
|
|
int menu_ch; //菜单选项
|
|
|
do
|
|
|
{
|
|
|
printf("Select the choice:");
|
|
|
scanf("%d",&menu_ch);
|
|
|
if((menu_ch<0)||(menu_ch>9))
|
|
|
printf("error!");
|
|
|
}
|
|
|
while((menu_ch<0)||(menu_ch>9));
|
|
|
return(menu_ch);
|
|
|
}
|
|
|
void main_menu() //主菜单
|
|
|
{
|
|
|
|
|
|
printf("\n");
|
|
|
printf(" 欢迎使用学生成绩管理系统 \n");
|
|
|
printf(" ***************************************************************\n");
|
|
|
printf(" || 0.退出系统 | 1.查询信息 ||\n");
|
|
|
printf(" || 2.添加信息 | 3 显示信息 ||\n");
|
|
|
printf(" || 4.删除信息 | 5.修改信息 ||\n");
|
|
|
printf(" || 6.备份信息 | 7.设置密码 ||\n");
|
|
|
printf(" || 8.数据排序 ||\n");
|
|
|
printf(" ***************************************************************\n");
|
|
|
}
|
|
|
void clear()//清屏
|
|
|
{
|
|
|
system("pause");
|
|
|
system("cls");
|
|
|
}
|
|
|
void menu() //菜单响应
|
|
|
{
|
|
|
while(1)
|
|
|
{
|
|
|
main_menu();
|
|
|
switch(get_menu_choice())
|
|
|
{
|
|
|
case 1:Find();clear();
|
|
|
break;
|
|
|
case 2:Input(file_operate("ab"));clear();
|
|
|
break;
|
|
|
case 3:Show();clear();
|
|
|
break;
|
|
|
case 4:Delete(file_operate("wb"));clear();
|
|
|
break;
|
|
|
case 5:Alter(file_operate("wb"));clear();
|
|
|
break;
|
|
|
case 6:Save();clear();
|
|
|
break;
|
|
|
case 7:set_psw();clear();
|
|
|
break;
|
|
|
case 8:Sort_menu();
|
|
|
break;
|
|
|
case 0:system("cls");
|
|
|
printf("\n");
|
|
|
printf("\n");
|
|
|
printf("\n");
|
|
|
printf(" ||**************************************||\n");
|
|
|
printf(" ||**************************************||\n");
|
|
|
printf(" || ||\n");
|
|
|
printf(" || ||\n");
|
|
|
printf(" || 感谢使用! || \n");
|
|
|
printf(" || ||\n");
|
|
|
printf(" || ||\n");
|
|
|
printf(" ||**************************************||\n");
|
|
|
printf(" ||**************************************||\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void Show()//显示打印所有的信息
|
|
|
{
|
|
|
int i;
|
|
|
printf("有%d个记录:\n",all);
|
|
|
PrintTitle();
|
|
|
for(i=0;i<all;i++)
|
|
|
Show_student(i);
|
|
|
}
|
|
|
void Show_student(int i) //显示打印一个人的信息
|
|
|
{
|
|
|
printf(" %8d %8s %1c %3d %3d %3d %3d \n",student[i].num,student[i].name,student[i].sex,student[i].mark[0],student[i].mark[1],student[i].mark[2],student[i].mark[3]);
|
|
|
}
|
|
|
void PrintTitle()//打印头信息
|
|
|
{
|
|
|
printf("-----------------------------------------------------------------\n");
|
|
|
printf(" 学号 姓名 性别 语文 数学 英语 平均成绩 \n");
|
|
|
printf("-----------------------------------------------------------------\n");
|
|
|
}
|
|
|
void Init() //初始化,文件不存在,则建立,同时记录下文件中的记录数
|
|
|
{
|
|
|
int i;
|
|
|
FILE *fp;
|
|
|
all=0;
|
|
|
if((fp=fopen("student.txt","rb"))==NULL)
|
|
|
{
|
|
|
fp=fopen("student.txt","ab");
|
|
|
clear();
|
|
|
menu();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
Readfile(i,fp);
|
|
|
}
|
|
|
all=i-1;
|
|
|
fclose(fp);
|
|
|
}
|
|
|
void Find()//查找记录的函数
|
|
|
{
|
|
|
int i,num_temp;
|
|
|
FILE *fp;
|
|
|
fp=file_operate("rb");
|
|
|
for(i=0;feof(fp)==0;i++)
|
|
|
Readfile(i,fp);
|
|
|
all=i-1;
|
|
|
fclose(fp);
|
|
|
printf("Input the number that someone you want to find:(如10141303)");
|
|
|
scanf("%d",&num_temp);
|
|
|
i=CheckNumber(num_temp);
|
|
|
if(i>=0)
|
|
|
{
|
|
|
PrintTitle();
|
|
|
Show_student(i);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("numbody is the number:%d\n",num_temp);
|
|
|
}
|
|
|
}
|
|
|
void Readfile(int i,FILE *fp)//从文件中读记录
|
|
|
{
|
|
|
int j;
|
|
|
fscanf(fp,"%8d",&student[i].num);
|
|
|
fscanf(fp,"%8s ",&student[i].name);
|
|
|
fscanf(fp,"%1c",&student[i].sex);
|
|
|
for(j=0;j<4;j++)
|
|
|
fscanf(fp,"%3d",&student[i].mark[j]);
|
|
|
}
|
|
|
int CheckNumber(int num_temp)//检查学号是否存在,存在返回序号,不存在返回-1
|
|
|
{
|
|
|
int i,result;
|
|
|
for(i=0;i<all;i++)
|
|
|
{
|
|
|
if(student[i].num==num_temp)
|
|
|
{
|
|
|
result=1;
|
|
|
}
|
|
|
}
|
|
|
if(result==1)
|
|
|
return i;
|
|
|
else
|
|
|
return -1;
|
|
|
}
|
|
|
int Inputoneperson(int i)//向数组增加一条记录
|
|
|
{
|
|
|
int j,sum=0;
|
|
|
int num_temp;
|
|
|
do
|
|
|
{
|
|
|
printf("输入学号:");
|
|
|
scanf("%d",&num_temp);
|
|
|
if(num_temp<10141301)
|
|
|
printf("error!");
|
|
|
}
|
|
|
while(num_temp<10140301);
|
|
|
if(CheckNumber(num_temp)>0)
|
|
|
{
|
|
|
printf("Number repeatly!\n");
|
|
|
return 0;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
student[i].num=num_temp;
|
|
|
printf("Input name(lessthan 20 numbers):");
|
|
|
scanf("%s",&student[i].name);
|
|
|
printf("Sex(M/W):");
|
|
|
scanf("%s",&student[i].sex);
|
|
|
printf("\n 语文\t 数学\t 英语\n");
|
|
|
for(j=0;j<3;j++)
|
|
|
{
|
|
|
scanf("%d",&student[i].mark[j]);
|
|
|
sum=sum+student[i].mark[j];
|
|
|
}
|
|
|
student[i].mark[3]=sum/3;
|
|
|
PrintTitle();
|
|
|
Show_student(i);
|
|
|
return 1;
|
|
|
}
|
|
|
}
|
|
|
void Input(FILE *fp)//向管理系统中增加记录
|
|
|
{
|
|
|
int i;
|
|
|
i=all;
|
|
|
if(Inputoneperson(i))
|
|
|
{
|
|
|
all++;
|
|
|
Inputfile(i,fp);
|
|
|
}
|
|
|
fclose(fp);
|
|
|
}
|
|
|
void Inputfile(int i,FILE *fp) //把下标为i 的记录写入文件
|
|
|
{
|
|
|
int j;
|
|
|
fprintf(fp,"%8d",student[i].num);
|
|
|
fprintf(fp,"%8s ",student[i].name);
|
|
|
fprintf(fp,"%1c ",student[i].sex);
|
|
|
for(j=0;j<4;j++)
|
|
|
fprintf(fp,"%3d ",student[i].mark[j]);
|
|
|
}
|
|
|
|
|
|
|
|
|
void exchange(int i,int j)//交换两个人的所有信息
|
|
|
{
|
|
|
int k;
|
|
|
int num_temp,mark_temp;
|
|
|
char name_temp[20],sex_temp;
|
|
|
num_temp=student[i].num;
|
|
|
student[i].num=student[j].num;
|
|
|
student[j].num=num_temp;
|
|
|
for(k=0;k<4;k++)
|
|
|
{
|
|
|
mark_temp=student[i].mark[k];
|
|
|
student[i].mark[k]=student[j].mark[k];
|
|
|
student[j].mark[k]=mark_temp;
|
|
|
}
|
|
|
strcpy(name_temp,student[i].name);
|
|
|
strcpy(student[i].name,student[j].name);
|
|
|
strcpy(student[j].name,name_temp);
|
|
|
sex_temp=student[i].sex;
|
|
|
student[i].sex=student[j].sex;
|
|
|
student[j].sex=sex_temp;
|
|
|
}
|
|
|
FILE *file_operate(const char *mode)//文件操作
|
|
|
{
|
|
|
char choice;
|
|
|
FILE *fp;
|
|
|
do
|
|
|
{
|
|
|
fflush(stdin);
|
|
|
if((fp=fopen("student.txt",mode))==NULL)
|
|
|
{
|
|
|
puts("Fail to open the file!");
|
|
|
puts("Try again!(Y/N)?");
|
|
|
scanf("%c",&choice);
|
|
|
}
|
|
|
}
|
|
|
while(choice=='y'||choice=='Y');
|
|
|
if(choice=='n'||choice=='N')
|
|
|
exit(1);
|
|
|
return(fp);
|
|
|
}
|
|
|
void Save()//备份信息到文件
|
|
|
{
|
|
|
int i;
|
|
|
char filename[10],ch;
|
|
|
FILE *fp;
|
|
|
printf("Name the new file:(less than ten bits)");
|
|
|
scanf("%s",filename);
|
|
|
if((fp=fopen(filename,"wb"))==NULL)
|
|
|
{
|
|
|
printf("Fail to build the file!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
ch=getchar();
|
|
|
for(i=0;i<all;i++)
|
|
|
Inputfile(i,fp);
|
|
|
Show();
|
|
|
fclose(fp);
|
|
|
}
|
|
|
void Delete(FILE *fp)//删除记录
|
|
|
{
|
|
|
int i,j,k,num_temp,choice2,t=0;
|
|
|
char str[5];
|
|
|
printf("make sure you will delete it(Y/N)");
|
|
|
gets(str);
|
|
|
if(str[0]!='N'&&str[0]!='n');
|
|
|
t=1;
|
|
|
if(t==1)
|
|
|
{
|
|
|
printf("Inpute the number someone you needed:(如10141301)");
|
|
|
scanf("%d",&num_temp);
|
|
|
i=CheckNumber(num_temp);
|
|
|
if(i>=0)
|
|
|
{
|
|
|
PrintTitle();
|
|
|
Show_student(i);
|
|
|
printf("Sure to delete the person?(1,Y/2,N)");
|
|
|
scanf("%d",&choice2);
|
|
|
if(choice2==1)
|
|
|
{
|
|
|
for(j=i;j<all;j++)
|
|
|
{
|
|
|
strcpy(student[j].name,student[j+1].name);
|
|
|
student[j].sex=student[j+1].sex;
|
|
|
student[j].num=student[j+1].num;
|
|
|
for(k=0;k<4;k++)
|
|
|
{
|
|
|
student[j].mark[k]=student[j+1].mark[k];
|
|
|
}
|
|
|
}
|
|
|
all--;
|
|
|
for(k=0;k<all;k++)
|
|
|
Inputfile(k,fp);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
printf("numbody is the number:%d\n",num_temp);
|
|
|
}
|
|
|
void Alter(FILE *fp)//修改记录信息
|
|
|
{
|
|
|
int i,j,num_temp,sum=0,t;
|
|
|
char str[5];
|
|
|
printf("make sure you will alter it(Y/N)");
|
|
|
gets(str);
|
|
|
if(str[0]!='N'&&str[0]!='n')
|
|
|
t=1;
|
|
|
if(t==1)
|
|
|
{
|
|
|
printf("Inpute the number somesoe you want to alter:(如10141301)");
|
|
|
scanf("%d",&num_temp);
|
|
|
i=CheckNumber(num_temp);
|
|
|
if(i>=0)
|
|
|
{
|
|
|
student[i].num=num_temp;
|
|
|
printf("Input name(lessthan 20 numbers):");
|
|
|
scanf("%s",&student[i].name);
|
|
|
printf("Sex(M/W):");
|
|
|
scanf("%s",&student[i].sex);
|
|
|
printf("\n 语文\t 数学\t 英语\n");
|
|
|
for(j=0;j<3;j++)
|
|
|
{
|
|
|
scanf("%d",&student[i].mark[j]);
|
|
|
sum=sum+student[i].mark[j];
|
|
|
}
|
|
|
student[i].mark[3]=sum/3;
|
|
|
PrintTitle();
|
|
|
Show_student(i);
|
|
|
for(j=0;j<all;j++)
|
|
|
Inputfile(j,fp);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
printf("numbody is the number:%d\n",num_temp);
|
|
|
}
|
|
|
int Sort_menu_choice()//选择是按学号还是成绩排序
|
|
|
{
|
|
|
int choice;
|
|
|
do
|
|
|
{
|
|
|
printf("输入排序方式(1 按学号 2 按平均成绩): ");
|
|
|
scanf("%d",&choice);
|
|
|
if((choice<0)||(choice>2))
|
|
|
printf("error!");
|
|
|
}
|
|
|
while((choice<0)||(choice>2));
|
|
|
return(choice);
|
|
|
}
|
|
|
void Sort_num()//选择是按学号排序
|
|
|
{
|
|
|
int i,j;
|
|
|
for(i=0;i<all-1;i++)
|
|
|
{
|
|
|
for(j=i+1;j<all;j++)
|
|
|
{
|
|
|
if(student[i].num>student[j].num)
|
|
|
exchange(i,j);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void Sort_mark()//选择是按成绩排序
|
|
|
{
|
|
|
int i,j;
|
|
|
for(i=0;i<all-1;i++)
|
|
|
{
|
|
|
for(j=i+1;j<all;j++)
|
|
|
{
|
|
|
if(student[i].mark[3]<student[j].mark[3])
|
|
|
exchange(i,j);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
void Sort_menu()//排序菜单
|
|
|
{
|
|
|
switch(Sort_menu_choice())
|
|
|
{
|
|
|
case 1:Sort_num();
|
|
|
Sort_save();
|
|
|
clear();
|
|
|
break;
|
|
|
case 2:Sort_mark();
|
|
|
Sort_save();
|
|
|
clear();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
void Sort_save()//排序后数据写入文件
|
|
|
{
|
|
|
int i;
|
|
|
FILE *fp;
|
|
|
if((fp=fopen("student.txt","wb"))==NULL)
|
|
|
{
|
|
|
printf("Fail to save the file!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
for(i=0;i<all;i++)
|
|
|
Inputfile(i,fp);
|
|
|
Show();
|
|
|
fclose(fp);
|
|
|
}
|
|
|
|
|
|
|
|
|
void set_psw()//设置密码
|
|
|
{
|
|
|
char key_set[20],key_check[20],c;
|
|
|
unsigned int i;
|
|
|
FILE *fp;
|
|
|
do
|
|
|
{ printf("You must set password first!\n");
|
|
|
printf("Enter password:(lessthan 12 numbers and key'.'for end)\n");
|
|
|
for(i=0;(c=getch())!=' ';i++)
|
|
|
{
|
|
|
putchar('*');
|
|
|
key_set[i]=c;
|
|
|
}
|
|
|
key_set[i]='\0';
|
|
|
printf("\n------------\n");
|
|
|
printf("conform password:");
|
|
|
for(i=0;(c=getch())!=' ';i++)
|
|
|
{
|
|
|
putchar('*');
|
|
|
key_check[i]=c;
|
|
|
}
|
|
|
key_check[i]='\0';
|
|
|
printf("\n------------\n");
|
|
|
if(strcmp(key_set,key_check)==0)
|
|
|
{printf("Set password success!");
|
|
|
strcpy(password,key_set);
|
|
|
}
|
|
|
else
|
|
|
printf("error!\n");
|
|
|
}
|
|
|
while(strcmp(key_set,key_check)!=0);
|
|
|
clear();
|
|
|
fp=fopen("password.txt","wb");
|
|
|
fprintf(fp,"%s",password);
|
|
|
fclose(fp);
|
|
|
}
|
|
|
|
|
|
|
|
|
int key_check()//密码验证
|
|
|
{
|
|
|
unsigned int i=1,j=1;
|
|
|
FILE *fp;
|
|
|
char pword[20],c;
|
|
|
if((fp=fopen("password.txt","rb"))==NULL)
|
|
|
{
|
|
|
fp=fopen("password.txt","a");
|
|
|
set_psw();
|
|
|
}
|
|
|
fscanf(fp,"%s",password);
|
|
|
fclose(fp);
|
|
|
do
|
|
|
{
|
|
|
printf("\nInput password:key' 'for end(%d/three times)",j);
|
|
|
for(j=0;(c=getch())!=' ';j++)
|
|
|
{
|
|
|
putchar('*');
|
|
|
pword[j]=c;
|
|
|
}
|
|
|
pword[j]='\0';
|
|
|
i++;
|
|
|
}
|
|
|
while(strcmp(pword,password)!=0&&i<=3);
|
|
|
if(i<=3)
|
|
|
return 1;
|
|
|
else
|
|
|
{
|
|
|
printf("You have tryed for three times,fail to open the file!\n");
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|