#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "dos.h"
#include "string.h"
#define LEN sizeof (struct student)
#define FORMAT " %d %s "
void menu();
void print(struct student * );
void in();
void show();
void search();
void modify();
void del();
void insert();
void order();
struct student
{
int num;
char name[15];
float score[8];
float sum;
float avr;
};
struct student stu[50];
void menu()
{
system("cls");
printf ("\n\n\n");
printf ("\t\t\t|--------成绩管理系统-------|\n");
printf ("\t\t\t| 1.input record |\n");
printf ("\t\t\t| 2.show record |\n");
printf ("\t\t\t| 3.search record |\n");
printf ("\t\t\t| 4.modify record |\n");
printf ("\t\t\t| 5.insert record |\n");
printf ("\t\t\t| 6.delete record |\n");
printf ("\t\t\t| 7.order |\n");
printf ("\t\t\t| 0.exit |\n");
printf ("\t\t\t|---------------------------|\n");
printf ("\t\t\t choose(0-7):\n");
}
void print(struct student *p)
{
printf (FORMAT,p->num,p->name);
printf (" ");
}
void show()
{
FILE *fp;
int i,j,m=0;
fp=fopen("data.txt","r");
while(!feof(fp))
{
if (fread(&stu[m],LEN,1,fp)==1)
m++;
}
fclose(fp);
printf ("学号 姓名 英语 数学 数据 体育 语文 物理 毛概 近代 总分 平均分\n");
for(i=0;i
print(&stu[i]);
for(j=0;j<8;j++)
printf (" %2.1f ",stu[i].score[j]);
printf (" %2.1f ",stu[i].sum);
printf (" %2.1f ",stu[i].avr);
printf ("\n");
}
}
void in()
{
int i,j,m=0;
char ch[2];
FILE *fp;
if ((fp=fopen("data.txt","a+"))==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();
}
if ((fp=fopen("data.txt","w"))==NULL)
{
printf ("can not open!\n");
return ;
}
for (i=0;i
printf ("please input (y/n):");
scanf("%s",ch);
while (strcmp(ch,"Y")==0 || strcmp(ch,"y")==0)
{
printf ("number:");
scanf("%d",&stu[i].num);
for (i=0;i
{
printf ("the number is existing.\n");
getch();
fclose(fp);
return ;
}
printf ("name:");
scanf ("%s",stu[m].name);
for (j=0;j<8;j++)
{ printf ("the score %d ",(j+1));
scanf("%f",&stu[m].score[j]);
stu[m].sum += stu[m].score[j];
}
stu[m].avr=stu[m].sum/8;
if (fwrite(&stu[m],LEN,1,fp)!=1)
{
printf ("can not save!");
getch();
}
else
{
printf ("%s saved!",stu[m].name);
m++;
}
printf ("continue?(y/n):");
scanf ("%s",ch);
i++;
}
fclose(fp);
printf ("OK!\n");
}
void search() /*自定义查找函数*/
{
FILE *fp;
int snum,i,j,m=0;
char ch[2];
if((fp=fopen("data.txt","r"))==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
{
printf("find the student,show?(y/n)");
scanf("%s",ch);
if(strcmp(ch,"Y")==0 || strcmp(ch,"y")==0)
{
printf(
"学号 姓名 英语 数学 数据 体育 语文 物理 毛概 近代 总分 平均分\t\n");
print(&stu[i]); /*将查找出的结果按指定格式输出*/
printf ("\n");
for (j=0;j<8;j++)
{
printf ("the score %d ",(j+1));
printf ("%2.1f\n ",stu[i].score[j]);
}
printf ("the sum is %2.1f\n",stu[i].sum);
printf ("the average is %2.1f",stu[i].avr);
break;
}
}
if(i==m)
printf("can not find the student!\n"); /*未找到要查找的信息*/
}
void modify() /*自定义修改函数*/
{
FILE *fp;
int i,j,k,m=0;
int snum;
if((fp=fopen("data.txt","r+"))==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
break;
printf("find the student!you can modify!\n");
printf("name:\n");
scanf("%s",stu[i].name); /*输入名字*/
printf("\n");
stu[i].sum =0;
for (k=0;k<8;k++)
{ printf ("the score %d ",(k+1));
scanf("%f",&stu[i].score[k]);
stu[i].sum += stu[i].score[k];
}
stu[i].avr=stu[i].sum/8;
if((fp=fopen("data.txt","w"))==NULL)
{
printf("can not open\n");
return;
}
for(j=0;j
{
printf("can not save!\n");
getch();
}
fclose(fp);
}
void del() /*自定义删除函数*/
{
FILE *fp;
int snum,i,j,m=0;
char ch[2];
if((fp=fopen("data.txt","r+"))==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
break;
printf("find the student,delete?(y/n)");
scanf("%s",ch);
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) /*判断是否要进行删除*/
{
for(j=i;j
m--;
if((fp=fopen("data.txt","w"))==NULL) /*记录的总个数减1*/
{
printf("can not open\n");
return;
}
for(j=0;j
{
printf(" can not save!\n");
getch();
}
printf("delete successfully!\n");
}
else
return;
fclose(fp);
}
void order() /*自定义排序函数*/
{
FILE *fp;
struct student t;
int i=0,j=0,m=0;
if((fp=fopen("data.txt","r+"))==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;
}
if((fp=fopen("data.txt","w"))==NULL)
{
printf("can not open!\n");
return;
}
for(i=0;i
t=stu[i];
stu[i]=stu[j];
stu[j]=t;
}
if((fp=fopen("data.txt","w"))==NULL)
{
printf("can not open\n");
return;
}
for(i=0;i
{
printf("%s can not save!\n");
getch();
}
fclose(fp);
printf("save successfully!\n");
}
void insert() /*自定义插入函数*/
{
FILE *fp;
int i,j,k,l;
int m=0;
int snum;
if((fp=fopen("data.txt","r+"))==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
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("name:\n");
scanf("%s",stu[i+1].name);
printf("\n");
stu[i+1].sum =0;
for (l=0;l<8;l++)
{
printf ("the score %d ",(l+1));
scanf ("%f",&stu[i+1].score[l]);
stu[i+1].sum += stu[i+1].score[l];
}
stu[i+1].avr=stu[i+1].sum/8;
if((fp=fopen("data.txt","w"))==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 main()
{
int n;
menu();
printf ("please input: ");
scanf ("%d",&n);
while (n)
{
switch (n)
{
case 1: in(); break;
case 2: show(); break;
case 3: search(); break;
case 4: modify(); break;
case 5: insert(); break;
case 6: del(); break;
case 7: order(); break;
default :
break;
}
if (n<0 ||n>7)
printf("该操作不存在,请重新输入!");
getch();
menu();
scanf("%d",&n);
}
}
我以前从书上抄的,没问题的