先建立一个名为score的txt。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu
{
int num;
int wuli;
int huaxue;
int math;
};
FILE *fp;
//添加学生信息,可以排除与已存的冲突;
void add()
{
int a,b,c,d,n=0;
struct stu student;
fp=fopen("score.txt","r+");
printf("请输入要添加学生的学号、物理成绩、化学成绩及数学成绩\n");
scanf("%d%d%d%d",&student.num,&student.wuli,&student.huaxue,&student.math);
while(!feof(fp))
{
fscanf(fp,"%d%d%d%d",&a,&b,&c,&d);
if(student.num==a)
{
printf("该学生信息已存在,不需要添加\n");
fclose(fp);
n=1;
break;
}
}
if(n==0)
{
printf("信息库中没有该学生信息,可以添加\n");
fseek(fp,0,2);//文件末尾添加;
fprintf(fp,"%d\t%d\t%d\t%d\n",student.num,student.wuli,student.huaxue,student.math);
fclose(fp);
}
}
//修改学生信息;
void modify()
{
int n=0,number,a,b,c,d,offset;
struct stu student;
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息库为空\n");
exit(0);
}
printf("请输入要修改学生的学号\n");
scanf("%d",&number);
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%d%d%d%d",&a,&b,&c,&d);
if(number==a)
{
printf("该学生信息为:\n");
printf("%d\t%d\t%d\t%d\n",a,b,c,d);
printf("请输入要修改的信息\n");
scanf("%d%d%d%d",&student.num,&student.wuli,&student.huaxue,&student.math);
fseek(fp,offset,0);
fprintf(fp,"%d\t%d\t%d\t%d\n",student.num,student.wuli,student.huaxue,student.math);
n=1;
break;
}
}
if(n==0)
{
printf("该学生信息不存在\n");
}
fclose(fp);
}
//删除学生信息;
void del()
{
int n=0,number,a,b,c,d,m,offset;
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息库为空\n");
exit(0);
}
printf("请输入删除学生的学号\n");
scanf("%d",&number);
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%d%d%d%d",&a,&b,&c,&d);
if(number==a)
{
printf("该学生信息为:\n");
printf("%d\t%d\t%d\t%d\n",a,b,c,d);
printf("是否要删除该学生信息,是:1 ;否:0\n");
scanf("%d",&m);
if(m)
{
fseek(fp,offset,0);
fprintf(fp,"%s\t%s\t%s\t%s"," "," "," "," ");
printf("\n");
printf("该学生信息已删除\n");
}
else printf("您已放弃删除该学生信息\n");
n=1;
break;
}
}
if(n==0)
{
printf("该学生信息不存在\n");
}
fclose(fp);
}
//查找学生信息;
void search()
{
int n=0,number,a,b,c,d,m;
char s[10];
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息库为空\n");
exit(0);
}
printf("请选择查找方式:1--按学号查找;0--按学科查找\n");
scanf("%d",&m);
if(m)
{
printf("请输入要查找学生的学号\n");
scanf("%d",&number);
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
if(number==a)
{
printf("已找到,该学生信息为:\n");
printf("%s %s %s %s\n","学号","物理成绩","化学成绩","数学成绩");
printf("%d\t %d\t %d\t %d\n",a,b,c,d);
n=1;
break;
}
}
if(n==0)
{
printf("该学生信息不存在\n");
}
fclose(fp);
}
else
{
printf("请输入要查找的学科\n");
scanf("%s",s);
if(strcmp(s,"wuli")==0)
{
printf("学号\t物理成绩\n");
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
printf("%d\t%d\n",a,b);
fclose(fp);
}
if(strcmp(s,"huaxue")==0)
{
printf("学号\t化学成绩\n");
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
printf("%d\t%d\n",a,c);
fclose(fp);
}
if(strcmp(s,"math")==0)
{
printf("学号\t数学成绩\n");
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
printf("%d\t%d\n",a,d);
fclose(fp);
}
if(strcmp(s,"wuli")&&strcmp(s,"huaxue")&&strcmp(s,"math") )
{
printf("错误,要查找的学科不存在\n");
fclose(fp);
}
}
}
//统计学生信息;
void tongji()
{
int n=0,a,b,c,d,m,k,x[100],i,j,l=1;
float sum=0,h[100],k1;
fp=fopen("score.txt","r+");
if(fp==NULL)
{
printf("信息库为空\n");
exit(0);
}
printf("0--统计单科平均成绩\n");
printf("1--统计单个学生的平均成绩和总成绩,对学生排名\n");
printf("请选择统计类型\n");
scanf("%d",&m);
if(!m)
{
printf("1--统计物理的平均成绩\n");
printf("2--统计化学的平均成绩\n");
printf("3--统计数学的平均成绩\n");
printf("请选择要统计的学科\n");
scanf("%d",&m);
if(m==1)
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
sum=sum+b;
n++;
}
printf("物理的平均成绩为 %2.2f\n",sum/n);
fclose(fp);
}
if(m==2)
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
sum=sum+c;
n++;
}
printf("化学的平均成绩为 %.2f\n",sum/n);
fclose(fp);
}
if(m==3)
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
sum=sum+d;
n++;
}
printf("数学的平均成绩为 %.2f\n",sum/n);
fclose(fp);
}
}
else
{
while(fscanf(fp,"%d%d%d%d",&a,&b,&c,&d)!=-1)
{
h[n]=(float)(b+c+d);
x[n]=a;
n++;
}
for(i=0;l==1&&i<n-1;i++)
{
l=0;
for(j=0;j<n-1-i;j++)
{
if(h[j]>h[j+1])
{
k1=h[j];
h[j]=h[j+1];
h[j+1]=k1;
k=x[j];
x[j]=x[j+1];
x[j+1]=k;
l=1;
}
}
}
printf("排序后\n");
printf("学号\t总成绩\t平均成绩\n");
for(i=0;i<n;i++)
printf("%d\t%3.0f\t%.2f\n",x[i],h[i],h[i]/3.0);
}
fclose(fp);
}
void main()
{
int m;
/*fp=fopen("score.txt","w");
fprintf(fp,"%s\t%s\t%s\t%s\n","学号","物理成绩","化学成绩","数学成绩");
fclose(fp);*/
while(1)
{
printf("\n\t\t*********欢迎使用学生信息管理系统*********\n\n");
printf("\n\t\t添加,请按1");
printf("\n\t\t修改,请按2");
printf("\n\t\t删除,请按3");
printf("\n\t\t查询,请按4");
printf("\n\t\t统计,请按5");
printf("\n\t\t退出,请按0\n");
printf("\n\t\t********************************************\n\n");
printf("Please select(0--5):");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: add();
break;
case 2: modify();
break;
case 3: del();
break;
case 4: search();
break;
case 5: tongji();
break;
case 0: exit(0);
}
printf("\n\n操作完毕,请再次选择!");
}
else
printf("\n\n选择错误,请再次选择!");
}
}
温馨提示:内容为网友见解,仅供参考