#include
#include
#include
#include
//**********************************结构体*************************************
struct score
{
char class_num[10];
char person_num[10];
char name[15];
char sex;
int num;
}stu;
//***********************************输出*******************************************
void print1() //输出科目函数
{
printf("班级\t\t学号\t\t姓名\t\t性别\t\t分数\n");
}
//************************************菜单***********************************************
int menu() //菜单函数
{
char i;
printf("\n\n\n\n");
puts("\t\t**********************主菜单******************\t\n");
puts("\t\t\t\t1.学生信息输入\n");
puts("\t\t\t\t2.学生信息浏览\n");
puts("\t\t\t\t3.学生信息检索\n");
puts("\t\t\t\t4.学生信息修改\n");
puts("\t\t\t\t5.学生信息删除\n");
puts("\t\t\t\t6.退出系统\n");
puts("\t\t**********************************************\t\n");
i=getchar();
system("cls");
return i;
}
//**************************************** 输入函数*******************************************
void Enter()
{
FILE *fp;
char ch;
if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
{
printf("\nCannot open file!\n");
getch();
exit(1);
}
do
{
printf("Enter class_num:");scanf("%s",&stu.class_num);getchar(); printf("\n");
printf("Enter person_num:");gets(stu.person_num); printf("\n");
printf("Enter name:");gets(stu.name); printf("\n");
printf("Enter sex(m/f):");scanf("%c",&stu.sex);getchar(); printf("\n");
printf("Enter score:");scanf("%d",&stu.num); printf("\n");
fwrite(&stu,sizeof(stu),1,fp);
printf("continue(y/n)?");
ch=getchar(); getchar();
}while(ch=='y'||ch=='Y');
fclose(fp);
system("cls");
}
//*********************************************浏览函数**********************************************
void Browse()
{
FILE *fp;
int total=0;
if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
while(fread(&stu,sizeof(stu),1,fp)==1)
{
total++;
if(total==1)
print1();
printf("%s\t\t%s\t\t%s",stu.class_num,stu.person_num,stu.name);
printf("\t\t%c",stu.sex);
printf("\t\t%-4d ",stu.num);
printf("\n");
}
fclose(fp);
printf("\n\n\tThere are %d record in all!\n",total);
printf("\tpress any key to continue!");
getchar();
getchar();
system("cls");
}
//**********************************查询函数*********************************************
void Query()
{
FILE * fp;
char num1[10];
char ch;
if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
do
{
getchar();
rewind(fp);
printf("Please input the student number you want to search:");
gets(num1);
while(fread(&stu,sizeof(stu),1,fp)==1)
if(strcmp(num1,stu.person_num)==0)
{
print1();
printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
printf("%-8c",stu.sex);
printf("%-4d ",stu.num);
printf("\n");
break;
}
printf("Search any more?(Y/N):[ ]\b\b");
ch=getchar(); getchar();
}while(ch=='Y'||ch=='y');
fclose(fp);
system("cls");
}
//**********************************学生成绩修改函数***********************************
void Recopose()
{
FILE * fp1,* fp2;
char num1[10];
char ch;
int j=0;
getchar();
do
{
if((fp1=fopen("f:\\zimou.txt","a+"))==NULL)
{
printf("\tCan not open the infom file!");
getch();
exit(1);
}
if((fp2=fopen("f:\\ziye.txt","a+"))==NULL)
{
printf("\tCan not creat the temp file!");
getch();
exit(1);
}
printf("Please input the num you want to modify:");
gets(num1);
while(fread(&stu, sizeof(stu),1,fp1)==1)
{
if(strcmp(num1,stu.person_num)==0)
{
print1();
printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
printf("%-8c",stu.sex);
printf("%-4d ",stu.num);
printf("\n");
printf("\n\nPlease input the new information:\n");
printf("\t班级代号:");gets(stu.class_num);
printf("\t学生学号:");gets(stu.person_num);
printf("\t学生姓名:"); gets(stu.name);
printf("\t学生性别:");gets(&stu.sex);
printf("\t学生成绩:");scanf("%d",&stu.num);
getchar();
}
fwrite(&stu,sizeof(stu),1,fp2);
}
fclose(fp1);
fclose(fp2);
printf("Modify sucess!\n");
remove("f:\\zimou.txt");
rename("f:\\ziye.txt","f:\\zimou.txt");
printf("Modify ang more?(Y/N):[ ]\b\b");
ch=getchar(); getchar();
}while(ch=='Y'||ch=='y');
system("cls");
}
//***************************************删除函数******************************************
void Delete()
{
FILE * fp1,* fp2;
int flag;
char num1[10];
char ch;
getchar();
do
{
if((fp1=fopen("f:\\zimou.txt","a+"))==NULL)
{
printf("\tCan not open the inform file!");
getch();
exit(1);
}
if((fp2=fopen("f:\\ziye.txt","a+"))==NULL)
{
printf("\tCan not creat the temp file!");
getch();
exit(1);
}
printf("请输入你要删除的学生的学号(person_num):");
gets(num1);
flag=0;
while(fread(&stu,sizeof(stu),1,fp1)==1)
{
if(strcmp(num1,stu.person_num)==0)
{
print1();
printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
printf("%-8c",stu.sex);
printf("%-4d ",stu.num);
printf("\n");
flag=1;
}
else
fwrite(&stu,sizeof(stu),1,fp2);
}
fclose(fp1);
fclose(fp2);
if(flag==1)
{
remove("f:\\zimou.txt");
rename("f:\\ziye.txt","f:\\zimou.txt");
}
else
printf("can not find this record!\n");
printf("Delete any more?(Y/N):[ ]\b\b");
ch=getchar(); getchar();
}while(ch=='Y'||ch=='y');
system("cls");
}
//***********************************出口**************************************
void Exit()
{
exit(0);
}
//***********************************主函数****************************************
int main()
{
while(1)
switch(menu())
{
case '1':Enter();break;
case '2':Browse();break;
case '3':Query();break;
case '4':Recopose();break;
case '5':Delete();break;
case '6':Exit();break;
}
return 0;
}