永发信息网

关于C语言的问题,高手帮我看看,看看我错在哪里了?!百分感谢!!!!

答案:1  悬赏:0  手机版
解决时间 2021-05-03 10:18
  • 提问者网友:書生途
  • 2021-05-03 01:36

要编一个程序,帮我看看错哪里了

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 2
struct student
{int num;
char name[10];
char sex[1];
int score[3];
float ave;
}stud[SIZE];


void display(void){
void main();
int i;
FILE *fp1;
printf("\n No. name sex english math computer ave\n");
printf("--------------------------------------------\n");
if((fp1=fopen("student.dat","rb"))==NULL)
{
printf("The file can not open\n");
return;
}
for(i=0;i<SIZE;i++)
{
fread(&stud[i],sizeof(struct student),1,fp1);
printf("%3d %-10s%s%7d%7d%7d%8.2f\n",stud[i].num,stud[i].name,stud[i].sex,
stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave);
}
fclose(fp1);
main();
}


void save(void)
{FILE *fp;
int i;
float sum[SIZE];
printf("\n No. name sex english math computer\n");
printf("-------------------------------------\n");
for(i=0;i<SIZE;i++)
{
scanf("%d%s%s%d%d%d",&stud[i].num,stud[i].name,stud[i].sex,
&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
sum[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
stud[i].ave=sum[i]/3;
}
if((fp=fopen("student.dat","wb"))==NULL)
{
printf("The file can not open\n");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&stud[i],sizeof(struct student),1,fp)!=1)
{
printf("file write error\n");
return;
}
fclose(fp);
}


void seek_num()
{void seek();
int i,a,t;
char ya='y';
FILE *fp;
if((fp=fopen("student.dat","rb"))==NULL)
{
printf("can not open the file\n");
exit(0);
}
for(i=0;i<SIZE;i++)
fread(&stud[i],sizeof(struct student),1,fp);
fclose(fp);
while(ya=='y'){
printf("\ninput the number:\n");
scanf("%d",&a);
for (i=0;i<SIZE;i++)
if (stud[i].num==a)
{
printf("\n no. name sex maths English computer ave\n");
printf("************************************************************\n");
printf(" %d %-10s%s %d %d %d %.2f\n",stud[i].num,stud[i].name,stud[i].sex,stud[i].score[0],
stud[i].score[1],stud[i].score[2],stud[i].ave);
t=1;
}
if (t!=1)printf("没有该学号的学生!\n");
printf("是否继续查找(y/n)?:");scanf("%s",&ya);}
if(ya=='n')seek();
}


void seek_name()
{void seek();
int i,j;
char name[10];
char ya='y';
FILE *fp;
if((fp=fopen("student.dat","rb"))==NULL)
{ printf("can not open the file\n");
exit(0);
}
for(i=0;i<SIZE;i++)
fread(&stud[i],sizeof(struct student),1,fp);
fclose(fp);
while (ya=='y')
{
printf("\n input the name:\n");
scanf("%s",name);
for(i=0;i<SIZE;i++)
{j=strcmp(name,stud[i].name);
if(j==0)
{printf("\n no. name sex maths English computer ave\n");
printf("*****************************************************************\n");
printf("%d %-10s%s %d %d %d %.2f\n",stud[i].num,stud[i].name,stud[i].sex,
stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave);break;} }
if (j!=0)
printf("找不到该学生!\n");
printf("继续查找?(y/n):");
scanf("%s",&ya);}
if(ya=='n')seek();
}


void seek_score()
{void seek();
float x;
int i,flag;
char j='y';
FILE *fp;
if((fp=fopen("student.dat","rb"))==NULL)
{ printf("can not open the file\n");
exit(0);
}
for(i=0;i<SIZE;i++)
fread(&stud[i],sizeof(struct student),1,fp);
fclose(fp);
while(j=='y') {
printf("\n input the average score:\n");
scanf("%f",&x);
for(i=0;i<SIZE;i++)
if(stud[i].ave<x)
{printf("name:%-10s sex:%s no.:%d maths: %d English: %d computer: %d average: %.2f\n",stud[i].name,stud[i].sex,stud[i].num,stud[i].score[0],
stud[i].score[1],stud[i].score[2],stud[i].ave);
flag=1; }
if (!flag) printf("没有任何学生的平均成绩低于查询成绩\n");
printf("继续查找?(y/n):\n");scanf("%s",&j);}
if (j=='n')seek();
}


void seek()
{int choice;
void main();
printf("_______________________________________\n");
printf("***************************************\n");
printf(" 1-----------按学号查询 \n");
printf(" 2-----------按姓名查询 \n");
printf(" 3-----------按成绩查询 \n");
printf(" 4-----------回到主界面 \n");
printf("***************************************\n");
printf("_______________________________________\n");
printf("请输入选择\n");
scanf("%d",&choice);
while(1){
switch(choice){
case 1:seek_num();break;
case 2:seek_name();break;
case 3:seek_score();break;
case 4:main();break;
default:printf("输入错误,请输入1——4中的一个:");scanf("%d",&choice);
}
}
}


void sort()
{void main();
int i,j; char g;
student temp;
FILE *fp;
if((fp=fopen("student.dat","rb"))==NULL)
{printf("can not open the file\n");
exit(0);}
for(i=0;i<SIZE;i++)
fread(&stud[i],sizeof(struct student),1,fp);
fclose(fp);
for(i=0;i<SIZE;i++)
for(j=i+1;j<SIZE;j++)
if (stud[i].ave<stud[j].ave)
{temp=stud[i];
stud[i]=stud[j];
stud[j]=temp;}
printf("\nno. name sex maths English computer ave\n");
printf("************************************************************\n");
for(i=0;i<SIZE;i++)
printf("%d %-10s%s %d %d %d %.2f\n",stud[i].num,stud[i].name,stud[i].sex,
stud[i].score[0], stud[i].score[1],stud[i].score[2],stud[i].ave);
printf("要不要回到主界面?(y/n):");
scanf("%s",&g);
if(g=='y')main();else exit(0);}

void main()
{ int choice;
printf("_______________________________________\n");
printf("***************************************\n");
printf(" 1-----------输入并显示 \n");
printf(" 2-----------查询 \n");
printf(" 3-----------排序 \n");
printf(" 4-----------退出 \n");
printf("***************************************\n");
printf("_______________________________________\n");
printf("请输入你的选择:\n");
scanf("%d",&choice);
while(1){
switch(choice){
case 1:save();display();break;
case 2:seek();break;
case 3:sort();break;
case 4:exit(0);
default:printf("输入错误数值,请输入1——4中的一个:");scanf("%d",&choice);
}
}
}

最佳答案
  • 五星知识达人网友:纵马山川剑自提
  • 2021-05-03 02:06
我大概运行了下,基本是正常的。你说的问题是哪个地方有问题?请说的明确一点。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯