信息管理系统

学C语言有一段时间了,自己写了个学生成绩管理系统,不完善。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
struct Student
{
  char ID[20];
  char name[20];
  float chinese;
  float math;
  float english;
  float total;
  int rank;
};
void input()      //学生信息录入
{
   FILE *fp;
   struct Student stu[N];
   int m=0;
   int i;
   int j=1;
   char n; 
   if((fp=fopen("E:\\file_1.txt","rb+"))==NULL)
   {
     printf("cannot open file\n");
exit(1);


   }
   while(!feof(fp))
   {
     fread(&stu[m],sizeof(struct Student),1,fp);
m++;
   }
   for(;m<N;m++)
   {
     system("cls");
if(m>=2)
{
while(1)
{
  for(i=0;i<m-1;i++)
       if(!strcmp(stu[m-1].ID,stu[i].ID))
{
      printf("信息录入有误,请重新输入:\n");
               printf("学号:");
               scanf("%s",&stu[m-1].ID);
          printf("姓名:");
          scanf("%s",&stu[m-1].name);
          printf("语文成绩:");
          scanf("%f",&stu[m-1].chinese);
          printf("数学成绩:");
          scanf("%f",&stu[m-1].math);
          printf("英语成绩:");
          scanf("%f",&stu[m-1].english);
}
for(i=0;i<m-1;i++)
if(!strcmp(stu[m-1].ID,stu[i].ID))
j=0;
if(j==1&&i==(m-1))
                   break;
}
}
printf("输入学生信息\n");
     printf("学号:");
     scanf("%s",&stu[m].ID);
printf("姓名:");
scanf("%s",&stu[m].name);
printf("语文成绩:");
scanf("%f",&stu[m].chinese);
printf("数学成绩:");
scanf("%f",&stu[m].math);
printf("英语成绩:");
scanf("%f",&stu[m].english);
if(fwrite(&stu[m],sizeof(struct Student),1,fp)!=1)
printf("file write error\n");
printf("继续输入?(y/n):");
scanf("%s",&n);
if(n=='n'||n=='N')
     break;
else
continue;
   }
   fclose(fp);
}
void show()   //学生信息展示
{
  FILE *p;
  struct Student stu[N];
  int i=0;
  int m=0;
  if((p=fopen("E:\\file_1.txt","rb"))==NULL)
  {
     printf("cannot open file\n");
     exit(1);
  }
  while(!feof(p))
   {
     fread(&stu[m],sizeof(struct Student),1,p);
m++;
   }
  for(i=0;i<m-1;i++)
  {
     printf("%s %s %.1f %.1f %.1f\n",stu[i].ID,stu[i].name,stu[i].chinese,stu[i].math,stu[i].english);
  }
  fclose(p);
}
void serach()   //查询或修改学生的信息
{
   FILE *p;
  struct Student stu[N];
  int i,j=2000;
  int m=0;
  char id[20];
  char flag;
  printf("请输入你要查询的学生的学号:");
  scanf("%s",&id);
  if((p=fopen("E:\\file_1.txt","rb+"))==NULL)
  {
     printf("cannot open file\n");
     exit(1);
  }
  while(!feof(p))
   {
     fread(&stu[m],sizeof(struct Student),1,p);
m++;
   }
  for(i=0;i<m-1;i++)
  {
 if(!strcmp(id,stu[i].ID))
 {
   printf("学号:%s\n",stu[i].ID);
        printf("姓名:%s\n",stu[i].name);
   printf("语文成绩:%.1f\n",stu[i].chinese);
        printf("数学成绩:%.1f\n",stu[i].math);
   printf("英语成绩:%.1f\n",stu[i].english);
   j=i;
 }
  }
  if(j==2000)
 printf("查无此人\n");
  if(j!=2000)
  while(1)
  { printf("是否修改?(y/n):");
    scanf("%s",&flag);
    if(flag=='Y'||flag=='y')
   {
     printf("输入学生信息\n");
     printf("学号:");
     scanf("%s",&stu[j].ID);
printf("姓名:");
scanf("%s",&stu[j].name);
printf("语文成绩:");
scanf("%f",&stu[j].chinese);
printf("数学成绩:");
scanf("%f",&stu[j].math);
printf("英语成绩:");
scanf("%f",&stu[j].english);
    }
if(flag=='N'||flag=='n')
break;
  }
  rewind(p);
  for(i=0;i<m-1;i++)
if(fwrite(&stu[i],sizeof(struct Student),1,p)!=1)
printf("file write error\n");
  fclose(p);
}
void count()
{
  FILE *p;
  struct Student stu[N];
  int m=0;
  if((p=fopen("E:\\file_1.txt","rb+"))==NULL)
  {
     printf("cannot open file\n");
     exit(1);
  }
  while(!feof(p))
   {
     fread(&stu[m],sizeof(struct Student),1,p);
m++;
   }
   m--;
   printf("记录学生人数:%d\n",m);
}
void del()
{
  FILE *p,*ftp;
  struct Student stu[N];
  char ch[20];
  int m=0;
  int i;
  int flag;
  count();
  show();
  printf("输入要删除学生的学号:");
  scanf("%s",&ch);
  if((p=fopen("E:\\file_1.txt","rb+"))==NULL)
  {
     printf("cannot open file\n");
     exit(1);
  }
  while(!feof(p))
   {
     fread(&stu[m],sizeof(struct Student),1,p);
m++;
   }
  for(i=0;i<m-1;i++)
 if(!strcmp(ch,stu[i].ID))
 flag=i;
  for(i=flag;i<m-2;i++)
  {
    strcpy(stu[i].ID,stu[i+1].ID);
    strcpy(stu[i].name,stu[i+1].name);
    stu[i].chinese=stu[i+1].chinese;
stu[i].math=stu[i+1].math;
stu[i].english=stu[i+1].english;
  }
  fclose(p);
  if((ftp=fopen("E:\\file_1.txt","wb+"))==NULL)
  {
    printf("cannot open file\n");
exit(1);
  }
  for(i=0;i<m-2;i++)
if(fwrite(&stu[i],sizeof(struct Student),1,ftp)!=1)
printf("file write error\n");
  fclose(ftp);
}
void insert()
{
  FILE *p,*ftp;
  struct Student stu[N];
  int m=0;
  int i,j;
  int flag;
  char ch[20];
  if((p=fopen("E:\\file_1.txt","rb+"))==NULL)
  {
     printf("cannot open file\n");
     exit(1);
  }
  while(!feof(p))
   {
     fread(&stu[m],sizeof(struct Student),1,p);
m++;
   }
  show();
  printf("你要插入那个学生后?输入学号:");
  scanf("%s",&ch);
  for(i=0;i<m-1;i++)
 if(!strcmp(ch,stu[i].ID))
 flag=i;
  for(i=m-2;i>flag;i--)
  {
     strcpy(stu[i+1].ID,stu[i].ID);
    strcpy(stu[i+1].name,stu[i].name);
    stu[i+1].chinese=stu[i].chinese;
stu[i+1].math=stu[i].math;
stu[i+1].english=stu[i].english;
  }
     j=flag+1;
     printf("输入学生信息\n");
     printf("学号:");
     scanf("%s",&stu[j].ID);
printf("姓名:");
scanf("%s",&stu[j].name);
printf("语文成绩:");
scanf("%f",&stu[j].chinese);
printf("数学成绩:");
scanf("%f",&stu[j].math);
printf("英语成绩:");
scanf("%f",&stu[j].english);
       fclose(p);
  if((ftp=fopen("E:\\file_1.txt","wb+"))==NULL)
  {
    printf("cannot open file\n");
exit(1);
  }
  for(i=0;i<m;i++)
if(fwrite(&stu[i],sizeof(struct Student),1,ftp)!=1)
printf("file write error\n");
  fclose(ftp);
}
void menu()
{
  int i;
  while(1)
  {
 system("cls");
 count();
      printf("----------------学生成绩管理-----------------\n");
 printf("\n");
      printf("                 1.录入学生信息\n");
      printf("                 2.查询学生信息\n");
      printf("                 3.删除学生信息\n");
      printf("                 4.插入学生信息\n");
      printf("                 5.退出系统\n");
 printf("\n");
      printf("---------------------------------------------\n");
      printf("                 请选择(1-5):");
      scanf("%d",&i);
 if(i==5)
 break;
      switch(i)
 {
    case 1:
{
   input();
break;
}
case 2:
{
  serach();
  break;
}
case 3:
{
  del();
  printf("删除后:\n");
  show();
  system("PAUSE");
}
case 4:
{
  insert();
  printf("插入后:\n");
               show();
  system("PAUSE");
}
default:
{
printf("输入有误\n");
    system("PAUSE");
}
 }
  }
}
int main()
{
  menu();
  return 0;
}

猜你喜欢

转载自blog.csdn.net/fight_snail/article/details/79512359
今日推荐