#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#define LEN sizeof(struct student)
#define FORMAT "%-8d%-15s%-12.1lf%-12.1lf%-12.1lf%-12.1lf\n"
#define DATA stu[i].num,stu[i].name,stu[i].elec,stu[i].expe,stu[i].requ,stu[i].sum
float Felec,Fexpe,Frequ;
struct student/*定義學生成績結構體*/
{
int num;/*學號*/
char name[15];/*姓名*/
double elec;/*選修課*/
double expe;/*實驗課*/
double requ;/*必修課*/
double sum;/*總分*/
};
struct student stu[50];/*定義結構體數組*/
void in();/*錄入學生成績信息*/
void show();/*顯示學生信息*/
void order();/*按總分排序*/
void del();/*刪除學生成績信息*/
void modify();/*修改學生成績信息*/
void menu();/*主菜單*/
void insert();/*插入學生信息*/
void total();/*計算總人數*/
void search();/*查找學生信息*/
void main()/*主函數*/
{
int n;
menu();
scanf("%d",&n);/*輸入選擇功能的編號*/
while(n)
{
switch(n)
{
case 1:
in();
break;
case 2:
search();
break;
case 3:
del();
break;
case 4:
modify();
break;
case 5:
insert();
break;
case 6:
order();
break;
case 7:
total();
break;
default:break;
}
getch();
menu();/*執行完功能再次顯示菜單界面*/
scanf("%d",&n);
}
}
void in()/*錄入學生信息*/
{
int i,m=0;/*m是記錄的條數*/
char ch[2];
FILE *fp;/*定義文件指針*/
if((fp=fopen("data","ab+"))==NULL)/*打開指定文件*/
{
printf("can not open\n");
return;
}
while(!feof(fp))
{
if(fread(&stu[m] ,LEN,1,fp)==1)
m++;/*統計當前記錄條數*/
}
fclose(fp);
if(m==0)
printf("No record!\n");
else
{
system("cls");
show();/*調用show函數,顯示原有信息*/
}
if((fp=fopen("data","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(i=0;i<m;i++)
fwrite(&stu[i] ,LEN,1,fp);/*向指定的磁盤文件寫入信息*/
printf("please input(y/n):");
scanf("%s",ch);
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("please input per centum:");
printf("\nelective:");
scanf("%f",&Felec);
printf("\nexperiment:");
scanf("%f",&Fexpe);
printf("\nrequired course:");
scanf("%f",&Frequ);
}
while(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判斷是否要錄入新信息*/
{
printf("number:");
scanf("%d",&stu[m].num);/*輸入學生學號*/
for(i=0;i<m;i++)
if(stu[i].num==stu[m].num)
{
printf("the number is existing,press any to continue!");
getch();
fclose(fp);
return;
}
printf("name:");
scanf("%s",stu[m].name);/*輸入學生姓名*/
printf("elective:");
scanf("%lf",&stu[m].elec);/*輸入選修課成績*/
printf("experiment:");
scanf("%lf",&stu[m].expe);/*輸入實驗課成績*/
printf("required course:");
scanf("%lf",&stu[m].requ);/*輸入必修課成績*/
stu[m].sum=stu[m].elec*Felec+stu[m].expe*Fexpe+stu[m].requ*Frequ;/*計算出總成績*/
if(fwrite(&stu[m],LEN,1,fp)!=1)/*將新錄入的信息寫入指定的磁盤文件*/
{
printf("can not save!");
getch();
}
else
{
printf("%s saved!\n",stu[m].name);
m++;
}
printf("continue?(y/n):");/*詢問是否繼續*/
scanf("%s",ch);
}
fclose(fp);
printf("OK!\n");
}
void show()
{
FILE *fp;
int i,m=0;
fp=fopen("data","ab+");
while(!feof(fp))
{
if(fread(&stu[m] ,LEN,1,fp)==1)
m++;
}
fclose(fp);
printf("number name elective experiment required sum\t\n");
for(i=0;i<m;i++)
{
printf(FORMAT,DATA);/*將信息按指定格式打印*/
}
}
void menu()/*自定義函數實現菜單功能*/
{
system("cls");
printf("\n\n\n\n\n");
printf("\t\t|---------------------STUDENT-------------------|\n");
printf("\t\t|\t 0. exit |\n");
printf("\t\t|\t 1. input record |\n");
printf("\t\t|\t 2. search record |\n");
printf("\t\t|\t 3. delete record |\n");
printf("\t\t|\t 4. modify record |\n");
printf("\t\t|\t 5. insert record |\n");
printf("\t\t|\t 6. order |\n");
printf("\t\t|\t 7. number |\n");
printf("\t\t|-----------------------------------------------|\n\n");
printf("\t\t\tchoose(0-7):");
}
void order()/*自定義排序函數*/
{
FILE *fp;
struct student t;
int i=0,j=0,m=0;
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open!\n");
return;
}
while(!feof(fp))
if(fread(&stu[m] ,LEN,1,fp)==1)
m++;
fclose(fp);
if(m==0)
{
printf("no record!\n");
return;
}
for(i=0;i<m-1;i++)
for(j=i+1;j<m;j++)/*雙重循環實現成績比較並交換*/
if(stu[i].sum<stu[j].sum)
{
t=stu[i];
stu[i]=stu[j];
stu[j]=t;
}
if((fp=fopen("data","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(i=0;i<m;i++)/*將重新排好序的內容重新寫入指定的磁盤文件中*/
if(fwrite(&stu[i] ,LEN,1,fp)!=1)
{
printf("%s can not save!\n");
getch();
}
fclose(fp);
printf("save successfully\n");
}
void del()/*自定義刪除函數*/
{
FILE *fp;
int snum,i,j,m=0;
char ch[2];
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&stu[m],LEN,1,fp)==1)
m++;
fclose(fp);
if(m==0)
{
printf("no record!\n");
return;
}
printf("please input the number:");
scanf("%d",&snum);
for(i=0;i<m;i++)
if(snum==stu[i].num)
break;
if(i==m)
{
printf("can not find");
getchar();
return;
}
printf("find the student,delete?(y/n)");
scanf("%s",ch);
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判斷是否要進行刪除*/
{
for(j=i;j<m;j++)
stu[j]=stu[j+1];/*將後壹個記錄移到前壹個記錄的位置*/
m--;/*記錄的總個數減1*/
printf("delete successfully!\n");
}
if((fp=fopen("data","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(j=0;j<m;j++)/*將更改後的記錄重新寫入指定的磁盤文件中*/
if(fwrite(&stu[j] ,LEN,1,fp)!=1)
{
printf("can not save!\n");
getch();
}
fclose(fp);
}
void search()/*自定義查找函數*/
{
FILE *fp;
int snum,i,m=0;
char ch[2];
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&stu[m],LEN,1,fp)==1)
m++;
fclose(fp);
if(m==0)
{
printf("no record!\n");
return;
}
printf("please input the number:");
scanf("%d",&snum);
for(i=0;i<m;i++)
if(snum==stu[i].num)/*查找輸入的學號是否在記錄中*/
{
printf("find the student,show?(y/n)");
scanf("%s",ch);
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
{
printf("number name elective experiment required sum\t\n");
printf(FORMAT,DATA);/*將查找出的結果按指定格式輸出*/
break;
}
else
return;
}
if(i==m)
printf("can not find the student!\n");/*未找到要查找的信息*/
}
void modify()/*自定義修改函數*/
{
FILE *fp;
int i,j,m=0,snum;
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&stu[m],LEN,1,fp)==1)
m++;
if(m==0)
{
printf("no record!\n");
fclose(fp);
return;
}
printf("please input the number of the student which do you want to modify!\n");
scanf("%d",&snum);
for(i=0;i<m;i++)
if(snum==stu[i].num)/*檢索記錄中是否有要修改的信息*/
break;
if(i<m)
{
printf("find the student!you can modify!\n");
printf("please input per centum:");
printf("\nelective:");
scanf("%f",&Felec);
printf("\nexperiment:");
scanf("%f",&Fexpe);
printf("\nrequired course:");
scanf("%f",&Frequ);
printf("name:\n");
scanf("%s",stu[i].name);/*輸入名字*/
printf("\nelective:");
scanf("%lf",&stu[i].elec);/*輸入選修課成績*/
printf("\nexperiment:");
scanf("%lf",&stu[i].expe);/*輸入實驗課成績*/
printf("\nrequired course:");
scanf("%lf",&stu[i].requ);/*輸入必修課成績*/
stu[i].sum=stu[i].elec*Felec+stu[i].expe*Fexpe+stu[i].requ*Frequ;
}
else
{
printf("can not find!");
getchar();
return;
}
if((fp=fopen("data","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(j=0;j<m;j++)/*將新修改的信息寫入指定的磁盤文件中*/
if(fwrite(&stu[j] ,LEN,1,fp)!=1)
{
printf("can not save!");
getch();
}
fclose(fp);
}
void insert()/*自定義插入函數*/
{
FILE *fp;
int i,j,k,m=0,snum;
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&stu[m],LEN,1,fp)==1)
m++;
if(m==0)
{
printf("no record!\n");
fclose(fp);
return;
}
printf("please input position where do you want to insert!(input the number)\n");
scanf("%d",&snum);/*輸入要插入的位置*/
for(i=0;i<m;i++)
if(snum==stu[i].num)
break;
for(j=m-1;j>i;j--)
stu[j+1]=stu[j];/*從最後壹條記錄開始均向後移壹位*/
printf("now please input the new information.\n");
printf("number:");
scanf("%d",&stu[i+1].num);
for(k=0;k<m;k++)
if(stu[k].num==stu[i+1].num&&k!=i+1)
{
printf("the number is existing,press any to continue!");
getch();
fclose(fp);
return;
}
printf("please input per centum:");
printf("\nelective:");
scanf("%f",&Felec);
printf("\nexperiment:");
scanf("%f",&Fexpe);
printf("\nrequired course:");
scanf("%f",&Frequ);
printf("name:\n");
scanf("%s",stu[i+1].name);
printf("\nelective:");
scanf("%lf",&stu[i+1].elec);
printf("\nexperiment:");
scanf("%lf",&stu[i+1].expe);
printf("\nrequired course:");
scanf("%lf",&stu[i+1].requ);
stu[i+1].sum=stu[i+1].elec*Felec+stu[i+1].expe*Fexpe+stu[i+1].requ*Frequ;
if((fp=fopen("data","wb"))==NULL)
{
printf("can not open\n");
return;
}
for(k=0;k<=m;k++)
if(fwrite(&stu[k] ,LEN,1,fp)!=1)/*將修改後的記錄寫入磁盤文件中*/
{
printf("can not save!");
getch();
}
fclose(fp);
}
void total()
{
FILE *fp;
int m=0;
if((fp=fopen("data","ab+"))==NULL)
{
printf("can not open\n");
return;
}
while(!feof(fp))
if(fread(&stu[m],LEN,1,fp)==1)
m++;/*統計記錄個數即學生個數*/
if(m==0)
{
printf("no record!\n");
fclose(fp);
return;
}
printf("the class are %d students!\n",m);/*將統計的個數輸出*/
fclose(fp);
}