C語言 從文件讀數據

早年練手作品,學生成績管理

#include<stdio.h>
#include<string.h>
#include<conio.h>


/*結構體*/
struct student
{int classno;
 int no;
 char name[10];
 float cj1;
 float cj2;
 float cj3;
}stud;

struct student_1
{int classno;
 int no;
 char name[10];
 float cj1;
 float cj2;
 float cj3;
 float total;
}stud_1[10];


/*子主調函數*/
zzhs()
{
int p;
printf("\n\n\n\n            1: lu ru yu xie ru\n");
printf("            2: shu ju du chu yu xian shi \n");
printf("            3: cha xun  \n");
printf("            4: tong ji zong fen ji pai ming ci\n");
printf("            5: tui chu\n");
printf ("            please input 1-5:");
scanf("%d",&p);
switch(p)
{
case 1: lx();break;/*lx=lu ru yu xie ru*/
case 2: dx();break;/*dx=du chu yu xian shi*/
case 3: cx();break;/*cx=cha xun*/
case 4: tp();break; /*tp=tong ji pai ming ci*/
case 5: break;
}
}






/*數據錄入*/
lx()
{
FILE *fp;
char ch;
if((fp=fopen("cj.txt","a+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(0);
}
printf("ClssNO. NO.     Name   Score1 Score2 Score3\n");
do
{
scanf("%d%d%s%f%f%f",&stud.classno,&stud.no,stud.name,&stud.cj1,&stud.cj2,&stud.cj3);
fwrite(&stud,sizeof(stud),1,fp);
printf("have another student recorder(y/n)?");
getchar();
ch=getchar();
}while(ch=='y');
fclose(fp);
zzhs();
}






/*數據讀出*/
dx()
{
FILE *fp;
if((fp=fopen("cj.txt","r"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(0);
}
printf("ClssNO. NO.     Name   Score1 Score2 Score3\n");
{
  while(fread(&stud,sizeof(stud),1,fp)==1)
  {
   printf("%4d %4d %10s %6.1f %6.1f %6.1f\n",stud.classno,stud.no,stud.name,stud.cj1,stud.cj2,stud.cj3);
  }
}
fclose(fp);
getch();
zzhs();
}






/*查詢*/
cx()
{
int i,xh;
FILE *fp;
if((fp=fopen("cj.txt","r"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(0);
}
for(i=0;i<2;i++)
    {
    fread(&stud_1[i],sizeof(stud),1,fp);
    }
fclose(fp);
printf("ClssNO. NO.     Name   Score1 Score2 Score3\n");
for(i=0;i<2;i++)
printf("%4d %4d %10s %6.1f %6.1f %6.1f\n",stud_1[i].classno,stud_1[i].no,stud_1[i].name,stud_1[i].cj1,stud_1[i].cj2,stud_1[i].cj3);
/*算總分*/
for(i=0;i<3;i++)
    stud_1[i].total=stud_1[i].cj1+stud_1[i].cj2+stud_1[i].cj3;
/*遍歷查詢*/
printf("qing shu ru xue hao:");
scanf("%d",&xh);
printf("ClssNO. NO.     Name   Score1 Score2 Score3 total\n");
for(i=0;i<3;i++)
    {
    if(stud_1[i].no==xh)
         printf("%4d %4d %10s %6.1f %6.1f %6.1f %6.1f\n",stud_1[i].classno,stud_1[i].no,stud_1[i].name,stud_1[i].cj1,stud_1[i].cj2,stud_1[i].cj3,stud_1[i].total);
    }
getch();
zzhs();
}






/*統計排名次*/
tp()
{
int i,j,xh,temp,temp1;
char temp2[10];
float temp3,temp4,temp5,temp6;
FILE *fp;
if((fp=fopen("cj.txt","r"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(0);
}
for(i=0;i<2;i++)
    {
    fread(&stud_1[i],sizeof(stud),1,fp);
    }
fclose(fp);
printf("ClssNO. NO.     Name   Score1 Score2 Score3 Total\n");
/*算總分*/
for(i=0;i<2;i++)
    stud_1[i].total=stud_1[i].cj1+stud_1[i].cj2+stud_1[i].cj3;
/*用冒泡法對總成績排序*/
  for(i=0;i<2;i++)
  {
    for(j=0;j<2-i;j++)
    {
       if(stud_1[j].total<stud_1[j+1].total)
          {
          /*交換班級*/
          temp=stud_1[j].classno;
          stud_1[j].classno=stud_1[j+1].classno;
          stud_1[j+1].classno=temp;
          /*交換序號no*/
          temp1=stud_1[j].no;
          stud_1[j].no=stud_1[j+1].no;
          stud_1[j+1].no=temp1;
          /*交換姓名*/
          strcpy(temp2,stud_1[j].name);
          strcpy(stud_1[j].name,stud_1[j+1].name);
          strcpy(stud_1[j+1].name,temp2);
          /*交換3科成績*/
          /*1*/
          temp3=stud_1[j].cj1;
          stud_1[j].cj1=stud_1[j+1].cj1;
          stud_1[j+1].cj1=temp3;
          /*2*/
          temp4=stud_1[j].cj2;
          stud_1[j].cj2=stud_1[j+1].cj2;
          stud_1[j+1].cj2=temp4;
          /*3*/
          temp5=stud_1[j].cj3;
          stud_1[j].cj3=stud_1[j+1].cj3;
          stud_1[j+1].cj3=temp5;
          /*swap total*/
          temp6=stud_1[j].total;
          stud_1[j].total=stud_1[j+1].total;
          stud_1[j+1].total=temp6;
          }
    }
  }
for(i=0;i<2;i++)
     printf("%4d %4d %10s %6.1f %6.1f %6.1f %6.1f\n",stud_1[i].classno,stud_1[i].no,stud_1[i].name,stud_1[i].cj1,stud_1[i].cj2,stud_1[i].cj3,stud_1[i].total);
getch();
zzhs();
}


/*主函數*/
main()
{
zzhs();
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章