一个课程设计的代码,想优化下。关于文件的
#include <stdio.h>#include<stdlib.h>
struct course
{
int num;
char name[15];
char xingzhi[10];
int time;
int ttime;
int etime;
int mark;
int term;
};
struct student
{
int snum;
int cnum;
};
/*insert*/
void insert()
{
void menu();
FILE *fp;
struct course c;
if((fp=fopen("course.txt","a+"))==NULL)
{
printf("cannot open file\n");
}
printf("\n\tInput course's information :\n");
printf("\tthe format is num name xingzhi time ttime etime mark term:\n");
scanf("%d %s %s %d %d %d %d %d",&c.num,&c.name,&c.xingzhi,&c.time,
&c.ttime,&c. etime,&c.mark,&c.term); //想在这里加一个判断语句,判断输入是否错误,确保course.txt中的内容是正确的。
fprintf(fp,"%d %s %s %d %d %d %d %d",c.num,c.name,c.xingzhi,c.time,
c.ttime,c.etime,c.mark,c.term);
fclose(fp);
menu();
}
/*browse liu_lan*/
void browser()
{
void menu();
FILE *fp;
struct course c;
if((fp=fopen("course.txt","r"))==NULL)
{
printf("\nCannot open course!\n");
}
for(;!feof(fp);)
{
fscanf(fp,"%d%s%s%d%d%d%d%d",&c.num,&c.name,&c.xingzhi,&c.time,
&c.ttime,&c.etime,&c.mark,&c.term);
printf("%d %s %s %d %d %d %d %d",c.num,c.name,c.xingzhi,c.time,
c.ttime,c.etime,c.mark,c.term);
}
fclose(fp);
menu();
}
/*search*/
void search()
{
void menu();
FILE *fp;
struct course c;
int score;
printf("\ninput mark your wantto search:");
scanf("%d",&score);
if((fp=fopen("course.txt","r"))==NULL)
{
printf("\nCannot open course!\n");
}
for(;!feof(fp);)
{
fscanf(fp,"%d %s %s %d %d %d %d %d",&c.num,&c.name,&c.xingzhi,&c.time,&c.ttime,&c.etime,&c.mark,&c.term);
if(c.mark==score)
printf("%d %s %s %d %d %d %d %d",c.num,c.name,c.xingzhi,c.time,
c.ttime,c.etime,c.mark,c.term);
}
fclose(fp);
menu();
}
/*tong ji xuan ke ren shu*/
void xuanke_information()
{
void menu();
int total=0;
FILE *fp;
struct student c;
int num;
printf("\ninput course -number your wantto search:");
scanf("%d",&num);
if((fp=fopen("student.txt","r"))==NULL)
{
printf("\nCannot open student!\n");
}
for(;!feof(fp);)
{
fscanf(fp,"%d%d",&c.snum,&);
if()
{
total++;
printf("student_num=%d,course_num=%d",c.snum,);
}
}
fclose(fp);
printf("\n The number choose course_num %d is %d!",num,total);
menu();
}
/*xuanke*/
void xuan_ke()
{
void menu();
int snum,cnum;
FILE *fp;
printf("\nPlease input your student_number and course number you want to choose:");
scanf("%d%d",&snum,&cnum);
if((fp=fopen("student.txt","a+"))==NULL)
{
printf("\nCannot open student!\n");
}
fprintf(fp,"%d %d",snum,cnum);
fclose(fp);
menu();
}
void menu()
{
int n,w=0;
printf("\n\t\t PLEASE ENTER A NUMBER FROM 0 TO 5!");
printf("\n\n\n\t*******************MENU**************************");
printf("\n\t\t 1 - insert informations");
printf("\n\t\t 2 - browers informations");
printf("\n\t\t 3 - search informations");
printf("\n\t\t 4 - xuan ke informations");
printf("\n\t\t 5- xuan ke ");
printf("\n\t\t 0 - Exit system");
printf("\n\n\n\t*************************************************");
printf("\n\n\n\n\n\n\t\tChoose the number,please:");
scanf("%d",&n);
do
{
if(n>5||n<0)
{
printf("\nWrong! input again!");
scanf("%d",&n);
}
else w=1;
}while(w==0);
switch(n)
{
case 1:insert();break;
case 2:browser();break;
case 3:search();break;
case 4:xuan_ke();break;
case 5:xuanke_information();break;
case 0:exit(0);
}
}
main()
{
menu();
}