69,371
社区成员
发帖
与我相关
我的任务
分享
修改之后能运行,也不崩溃了,但是输出的个人总成绩和总平均成绩都是错误的.
代码如下,请再帮忙找下错误,实在找不到哪里有错了,,,,,
#include"malloc.h"
#include"stdio.h"
#include"string.h"
#include"math.h"
#define SIZE 10
#define NUM 3
typedef struct student /*define one struct*/
{
char sno[SIZE];
char name[SIZE];
float score[NUM];
float s_sum;
}stu;
stu *p;
void input(stu*p_one); /*input_func instruction*/
stu*max(stu*p_two,float record);/*max_func instruction*/
float average(float s_sum);/*.....*/
void main(void) /*main function*/
{
int j,i;
float sum=0;
float record=0;
stu*q,*p1;
stu*stub;
p1=stub=(stu*)malloc(SIZE*sizeof(stu));
printf("plese input the data:\n");
input(stub);
p=stub;
for(j=0;j<SIZE;j++,p1++)
{for(i=0;i<NUM;i++)
p1->s_sum+=p1->score[i];
sum+=p1->s_sum;
q=max(p1,record);/*the max's adress*/
record=q->s_sum;
}
printf("please output the max_score_student's information:\n");
printf("sno name score1 score2 score3 s_sum\n");
printf("%-8s%-9s",q->sno,q->name);
for(j=0;j <NUM;j++)
printf("%-6.1f",q->score[j]);
printf("%f",q->s_sum);
printf("please output the total_average:%f",average(sum));
}
void input(stu*p_one)
{
int i,j;
for(i=1;i<=SIZE;i++,p_one++)
{
printf("please input the %d student's information:\n",i);
printf("sno:");
scanf("%s",p_one->sno);
printf("name:");
scanf("%s",p_one->name);
for(j=0;j<NUM;j++)
{ printf("score%d:",j);
scanf("%f",&(p_one->score[j]));
}
printf("\n");
}
}
stu*max(stu*p1, float record)
{
if(record<(p1->s_sum))
p=p1 ;
return p;
}
float average(float s_sum)
{
return (s_sum/SIZE);
}
#include "malloc.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#define STR_LEN 10 // sno or name max len
#define MAX_NUM 10 // student num
#define SCORE_NUM 3
typedef struct student /*define one struct*/
{
char sno[STR_LEN];
char name[STR_LEN];
float score[SCORE_NUM];
float s_sum;
}stu;
stu tStudent[MAX_NUM] = {0}; // stu *p;此处如果使用指针,那么内存分配与
// 释放问题要考虑好了,要不然内存泄露或越界的了
void input(void); /*input_func instruction*/
//stu *max(void); /*max_func instruction*/
int max(void); /*max_func instruction*/
float average(int nMaxIdx); /*.....*/
void main(void) /*main function*/
{
int nLoop;
int nMaxIdx;
input();
nMaxIdx = max(); /* the max's adress */
printf("please output the max_score_student's information:\n");
printf("sno name score1 score2 score3 s_sum\n");
printf("%-8s%-9s", tStudent[nMaxIdx].sno, tStudent[nMaxIdx].name);
for (nLoop = 0; nLoop < SCORE_NUM; nLoop++)
{
printf("%-6.1f",tStudent[nMaxIdx].score[nLoop]);
}
printf("%f", tStudent[nMaxIdx].s_sum);
printf("\n");
printf("please output the total_average:%f", (float)average(nMaxIdx));
}
void input(void)
{
int i,j;
printf("plese input the data:\n");
for (i=1; i<=MAX_NUM; i++)
{
printf("please input the %d student's information:\n",i);
printf("sno:");
scanf("%s", tStudent[i].sno);
printf("name:");
scanf("%s", tStudent[i].name);
for (j=0; j<SCORE_NUM; j++)
{
printf("score%d:", j);
scanf("%f", &(tStudent[i].score[j]));
tStudent[i].s_sum += tStudent[i].score[j];
}
printf("\n");
}
}
int max(void)
{
int nIdx;
int nMaxIdx = 0;
float fMaxSum = tStudent[0].s_sum;
for (nIdx = 1; nIdx < MAX_NUM; nIdx++)
{
if (fMaxSum < tStudent[nIdx].s_sum)
{
fMaxSum = tStudent[nIdx].s_sum;
nMaxIdx = nIdx;
}
}
return nMaxIdx;
}
float average(int nIdx)
{
return (tStudent[nIdx].s_sum/SCORE_NUM);
}