# 找找错误，怎么学号查询得到的sum aver是0.0

qq_57698169 2021-06-11 00:26:59

#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
#define M 10             /*字符串最大长度*/
#define N 6              /*最多考试科目数*/
typedef struct student
{
long num;                /* 每个学生的学号 */
char name[M];        /* 每个学生的姓名 */
float sum;               /* 每个学生的总成绩 */
float score[N];      /* 每个学生各门功课的成绩 */
float aver;              /* 每个学生的平均成绩 */
}STU;
void Input(STU *p,int n,int m);
void AverSumEveryCourse(STU *p,int n,int m);
void AverSumEveryStudent(STU *p,int n,int m);
void SortDescend(STU *p,int n,int m);
void SwapL(long *x,long *y);
void SwapF(float *x,float *y);
void SwapC(char x[],char y[]);
void AsSortbyNum(STU *p,int n,int m);
void SearchbyNum(STU *p,int n,int m);
void PrintScore(STU *p,int n,int m);
void StatNumAndPer(STU *p,int n);
int main()
{
int choice;
int n,m;                    /* 学生人数为n,课程门数为m*/
STU *p=NULL;
do
{
printf("Please input student number:");
scanf("%d",&n);
}while(n<0||n>30);
do
{
printf("Please input course number:");
scanf("%d",&m);
}while(m<0||m>6);
p=( STU * ) malloc ( n*sizeof( STU ));
if(p==NULL)
{
exit(0);
}
while(1)
{
switch(choice)
{
case 1: Input(p,n,m);
break;

case 2: AverSumEveryCourse(p,n,m);
break;

case 3: AverSumEveryStudent(p,n,m);
printf("Hello the result is:\n");
PrintScore(p,n,m);
break;

case 4: SortDescend(p,n,m);
printf("Hello the result is:\n");
PrintScore(p,n,m);
break;

case 5: AsSortbyNum(p,n,m);
printf("Hello the result is:\n");
PrintScore(p,n,m);
break;

case 6: SearchbyNum(p,n,m);
break;

case 0: printf("End of program!\n");
exit(0);

default:printf("Input error!\n");
}
}

}
/* 函数功能:显示菜单并获取用户键盘输入选项*/
{
int choice;
printf("*****************************************************************\n");
printf("1.Input student's ID ,name and score:\n");
printf("2.Calculate the total score and average score of every course\n");
printf("3.Calculate the total score and average score of every student\n");
printf("4.Sort in descending order by total score of every student\n");
printf("5.Sort in ascending order by number\n");
printf("6.Search by number\n");
printf("0.Exit\n");
printf("Please input you choice:");
scanf("%d",&choice);
return choice;
}
/* 函数功能:输入学生信息及成绩*/

void Input(STU *p,int n,int m)
{
int i,j;
printf("Please input student's ID ,name and score:\n");
for(i=0;i<n;i++)
{
scanf("%ld",&(p+i)->num);
scanf("%s",(p+i)->name);
for(j=0;j<m;j++)
{
scanf("%f",&(p+i)->score[j]);
}
}

}
/* 函数功能：计算每门课程的总分和平均分并输出每门课程总分及平均分*/

void AverSumEveryCourse(STU *p,int n,int m)
{
int i,j;
float sum[N],aver[N];
printf("Hello,the result is:\n");
for(j=0;j<m;j++)
{
sum[j]=0;
for(i=0;i<n;i++)
{
sum[j]=sum[j]+(p+i)->score[j];
}
aver[j]=sum[j]/n;
printf("course %d:sum=%.1f,aver=%.1f\n",j+1,sum[j],aver[j]);
}
}
/* 函数功能：计算每个学生的总分和平均分*/

void AverSumEveryStudent(STU *p,int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
(p+i)->sum=0;
for(j=0;j<m;j++)
{
(p+i)->sum=(p+i)->sum+(p+i)->score[j];
}
(p+i)->aver=(p+i)->sum/m;
}
}
/* 函数功能：按每个学生的总分由高到低排序*/

void SortDescend(STU *p,int n,int m)
{
int i,j,g;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((p+i)->sum<(p+j)->sum)
{
for(g=0;g<m;g++)
{
SwapF(&(p+i)->score[g],&(p+j)->score[g]);
}
SwapL(&(p+i)->num,&(p+j)->num);
SwapF(&(p+i)->sum,&(p+j)->sum);
SwapF(&(p+i)->aver,&(p+j)->aver);
SwapC((p+i)->name,(p+j)->name);
}
}
}
}
/* 函数功能：交换两个长整型数据*/

void SwapL(long *x,long *y)
{
long temp;
temp=*x;
*x=*y;
*y=temp;
}
/* 函数功能：交换两个单精度浮点型数据*/

void SwapF(float *x,float *y)
{
float temp;
temp=*x;
*x=*y;
*y=temp;
}
/* 函数功能：交换两个字符串*/

void SwapC(char x[],char y[])
{
char temp[M];
strcpy(temp,x);
strcpy(x,y);
strcpy(y,temp);
}
/* 函数功能：按学号由小到大排序*/

void AsSortbyNum(STU *p,int n,int m)
{
int i,j,g;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if((p+i)->num>(p+j)->num)
{
for(g=0;g<m;g++)
{
SwapF(&(p+i)->score[g],&(p+j)->score[g]);
}
SwapL(&(p+i)->num,&(p+j)->num);
SwapF(&(p+i)->sum,&(p+j)->sum);
SwapF(&(p+i)->aver,&(p+j)->aver);
SwapC((p+i)->name,(p+j)->name);
}
}
}
}
/* 函数功能：按学号查询学生各科成绩、总分、平均分并输出*/

void SearchbyNum(STU *p,int n,int m)
{
long number;
int i,j,flag=0;
printf("Please input the number you want to search:\n");
scanf("%ld",&number);
for(i=0;i<n;i++)
{
if((p+i)->num==number)
{
printf("The information you want to search is:\n");
printf("%ld %s ",(p+i)->num,(p+i)->name);
for(j=0;j<m;j++)
{
printf("%.1f ",(p+i)->score[j]);
}
printf("%.1f %.1f\n",(p+i)->sum,(p+i)->aver);
flag=1;
}
}
if(flag==0)
{
}
}
/* 函数功能：输出学生信息及各科成绩、总分、平均分*/

void PrintScore(STU *p,int n,int m)
{
int i,j;
for(i=0;i<n;i++)
{
printf("%ld %s",(p+i)->num,(p+i)->name);
for(j=0;j<m;j++)
{
printf(" %.1f ",(p+i)->score[j]);
}
printf("%.1f %.1f\n",(p+i)->sum,(p+i)->aver);
}
}
/*函数功能：输出学生学生信息及各科成绩、总分、平均分*/

...全文
962 2 打赏 收藏 举报

2 条回复

dark9spring 2021-06-14

• 打赏
• 举报

qq_57698169 2021-06-11

• 打赏
• 举报

10.8w+

.NET技术 C#

2021-06-11 00:26