当前位置:   article > 正文

一个学生管理系统_学生管理系统 fp

学生管理系统 fp

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count(int n),size;


struct student
{char num[10];
 char name[10];
 float score[3];
} stu[100];


void main()
{
 void input(void);
 void display(void);
 void search(void);
 void sort(void);
 void insert(void);
 

 int choice0;
do{
   printf("************************************************************/n");
   printf("1.Input the data/n");
   printf("2.display the data/n");
   printf("3.search/n");
   printf("4.Sort/n");
   printf("5.insert/n");
   printf("6.exit/n");
   printf("************************************************************/n");
   scanf("%d",&choice0);
   switch(choice0) {
  case 1:input();break;
  case 2:display();break;
  case 3:search();break;
  case 4:sort();break;
  case 5:insert();break;
  case 6:break;
  default:printf("Error./n");break;
   }
 } while(choice0!=6);
 printf("Goodbye./n");
}

void input(void)
{
 FILE *fp;
 int i,j,size;
 float score_tmp;
 char ch;
 if((fp=fopen("score","wb"))==NULL) {
   printf("Can not open the file./n");
   exit(0);                          
 }    
 printf("Please input student's number!/n");
 scanf("%d",&size);
 for(i=0;i<size;i++) {
   printf("Input the data of student[%d]/n",i+1);
   printf("No.:");
   scanf("%s",stu[i].num);
   printf("Name:");
   scanf("%s",stu[i].name);
   for(j=0;j<4;j++) {
  printf("Score%d:",j+1);
  scanf("%f",&score_tmp);
  stu[i].score[j]=score_tmp;
   }
   printf("/n");
 }
 for(i=0;i<size;i++) fwrite(&stu[i],sizeof(struct student),1,fp);
 printf("Press ENTER to continue...");
 ch=getchar();                       
 printf("/n");
 fclose(fp);
}

void display(void)
{
 FILE *fp;
 int i;
 if((fp=fopen("score","rb"))==NULL) {
   printf("Can not open the file./n");
   exit(0);
 }
 printf("************************************************************/n");
 printf("No./tNAME/tSCORE1/tSCORE2/tSCORE3/t/n");
 printf("************************************************************/n");
 for(i=0;fread(&stu[i],sizeof(struct student),1,fp);i++) {
   printf("%s/t%s/t%5.1f/t%5.1f/t%5.1f/t/n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
 }
 printf("************************************************************/n");
 printf("Press ENTER to continue...");
 printf("/n");
 fclose(fp);
}


void sort(void)
{
 FILE *fp;
 int i,j,answer,n;
 float sum[100];
 char filename[10];
 struct student temp;
 n=count(1);
 for(i=0;i<n;i++) sum[i]=0;
 if((fp=fopen("score","rb"))==NULL) {
   printf("Can not open the file./n");
   exit(0);
 }
 for(i=0;(fread(&stu[i],sizeof(struct student),1,fp));i++)
   for(j=0;j<3;j++) sum[i]=sum[i]+stu[i].score[j];
 fclose(fp);

 for(i=0;i<n;i++)
   for(j=i+1;j<n;j++)
  if(sum[i]<sum[j]) {
    temp=stu[i],stu[i]=stu[j],stu[j]=temp;
  }
 printf("The new order is:/n");
 printf("************************************************************/n");
 printf("No./tNAME/tSCORE1/tSCORE2/tSCORE3/t/n");
 printf("************************************************************/n");
 for(i=0;i<n;i++)
   printf("%s/t%s/t%5.1f/t%5.1f/t%5.1f/t/n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2]);
 printf("************************************************************/n");

 printf("Do you want to rewrite the data?:/n");
 printf("1.Yes/n");
 printf("2.No/n");
 scanf("%d",&answer);
 if(answer==1) {
   fp=fopen(filename,"wb");
   for(i=0;i<n;i++) fwrite(&stu[i],sizeof(struct student),1,fp);
   fclose(fp);
 }
 else if(answer!=2) printf("Error./n");
}

void insert(void)
{
 FILE *fp;
 int j;
 float score_tmp;

 if((fp=fopen("score","ab"))==NULL) {
   printf("Can not open the file./n");
   exit(0);
 }
   printf("No.:");
   scanf("%s",stu[size+1].num);
   printf("Name:");
   scanf("%s",stu[size+1].name);
   for(j=0;j<3;j++) {
  printf("Score%d:",j+1);
  scanf("%f",&score_tmp);
  stu[size+1].score[j]=score_tmp;
   }
 fseek(fp,-0l,2);
 fwrite(&stu[size+1],sizeof(struct student),1,fp);
 printf("Press ENTER to continue...");
 printf("/n");
 fclose(fp);
 sort();
}

void search(void)
{
 FILE *fp;
 int i,j,sign=0;
 float sum;
 char str[10];
 if((fp=fopen("score","rb"))==NULL) {
   printf("Can not open the file./n");
   exit(0);
 }
 printf("Input No. or the name:");
 scanf("%s",str);

 for(i=0;fread(&stu[i],sizeof(struct student),1,fp);i++) {
   sum=0;
   if((strcmp(str,stu[i].num)==0)||(strcmp(str,stu[i].name)==0)) {
  printf("************************************************************/n");
  for(j=0;j<3;j++) {
    sum=sum+stu[i].score[j];
    printf("Score%d:%5.1f  ",j+1,stu[i].score[j]);
    if(j==2) printf("/n");
  }
  printf("%s's average of scores is %5.1f/n",stu[i].name,sum/3);
  printf("************************************************************/n");
  sign=1;
  break;
   }
 }
 if(sign==0) printf("Can not find the data./n");
 printf("Press ENTER to continue...");
 printf("/n");
 fclose(fp);
}

int count(int n)
{
 FILE *fp;
 int i;
 if((fp=fopen("score","rb"))==NULL) {
   printf("Can not open the file./n");
   exit(0);
 }
for(i=0;fread(&stu[i],sizeof(struct student),1,fp);i++) {
}
return i;
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/697029
推荐阅读
相关标签
  

闽ICP备14008679号