C语言编程问题求大神解答
帮我写一段代码、再有相应的解释我主要想理解来背1定义structstudent类型,每个学生的数据包括学号(num),姓名(name),性别(sex),年龄(age),5...
帮我写一段代码、再有相应的解释我主要想理解来背
1 定义struct student类型,每个学生的数据包括学号(num),姓名(name),性别(sex),年龄(age),5门课程成绩(score[5]),个人平均分(average)。要求,编写一个函数求个人平均分,再编写一个函数求每门课程的平均分,在main()函数中输入10个学生的各项数据,调用以上两个函数分别获得每人的平均分,在main()函数输入10个学生的各项数据,调用以上两个函数分别获得每个人的平均分,和每门课程平均分,和每门课程平均分,再输出每个学生的各项成绩 展开
1 定义struct student类型,每个学生的数据包括学号(num),姓名(name),性别(sex),年龄(age),5门课程成绩(score[5]),个人平均分(average)。要求,编写一个函数求个人平均分,再编写一个函数求每门课程的平均分,在main()函数中输入10个学生的各项数据,调用以上两个函数分别获得每人的平均分,在main()函数输入10个学生的各项数据,调用以上两个函数分别获得每个人的平均分,和每门课程平均分,和每门课程平均分,再输出每个学生的各项成绩 展开
展开全部
struct student
{
int iNum; // 学号
char cName[20]; // 姓名
char cSex[2]; // 性别
int iAge; // 年龄
int iScore[5]; // 5门课成绩
float fAverage; // 个人平均分
};
// 求个人平均分
float ClacAverage(student *pStudent)
{
if (NULL == pStudent)
{
return 0.0f;
}
int iSum = 0;
for (int i = 0; i < 5; i++)
{
iSum += pStudent->iScore[i];
}
pStudent->fAverage = iSum / 5.0;
printf("学生 %s 的个人平均分为:%.2f\n", pStudent->cName, pStudent->fAverage);
return pStudent->fAverage;
}
// 求每门课程的平均分
void CalcAverageEachCourse(student Students[], int iCount)
{
for (int i = 0; i < 5; i++)
{
int iSum = 0;
for (int j = 0; j < iCount; j++)
{
iSum += Students[j].iScore[i];
}
float fAvg = iSum / iCount;
printf("第 %d 门课的平均分为:%.2f\n", i + 1, fAvg);
}
};
void GetInput(student *pStudent, int iNum)
{
if (NULL == pStudent)
{
return;
}
printf("请输入第 %d 位学生的学号:\n", iNum + 1);
scanf("%d", &pStudent->iNum);
printf("请输入第 %d 位学生的姓名:\n", iNum + 1);
scanf("%s", pStudent->cName);
printf("请输入第 %d 位学生的性别:\n", iNum + 1);
scanf("%s", pStudent->cSex);
for (int i = 0; i < 5; i++)
{
printf("请输入第 %d 位学生的第%d门课的成绩:\n", iNum + 1, i + 1);
scanf("%d", &pStudent->iScore[i]);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
/*test(); */
student Students[10];
for (int i = 0; i < 10; i++)
{
GetInput(&Students[i], i);
}
printf("以下是每个学生的个人平均分:\n");
for (int i = 0; i < 10; i++)
{
ClacAverage(&Students[i]);
}
printf("以下是每门课程的平均分:\n");
CalcAverageEachCourse(Students, 10);
system("pause");
return 0;
}
展开全部
帮你随便写了一个,没有测试验证,自己修改完善吧
#define MAX_NUM 10
#define MAX_CLASS 5
typedef struct tag_Student {
int num;
char name[20];
char sex;
int age;
float score[MAX_CLASS];
float average;
} STUDENT;
STUDENT g_student[MAX_NUM];
float GetClassAverage(int classnum)
{
int i = 0;
float total = 0.0;
if (classnum > MAX_CLASS || classnum < 0)
{
printf("No such class existed. \r\n");
return -1;
}
for(i = 0; i < MAX_NUM; i ++)
{
total += g_student[i].score[classnum];
}
return (total/MAX_NUM);
}
float GetStudentAverage(STUDENT *student)
{
int i =0;
int classnum = 5;
float total = 0.0;
if(student == 0)
{
printf("NULL Ponter.\r\n");
return -1;
}
for(i=0; i < classnum; i ++)
{
total += student->score[i];
}
return (total/classnum);
}
void main()
{
int i = 0;
float averagescore[MAX_CLASS];
printf("Please input student's information.\n");
for(i = 0; i < MAX_NUM; i ++)
{
printf("please input student i's 学号.\n", i);
scanf("%d \n", &(g_student[i].num));
printf("please input student i's 姓名.\n", i);
scanf("%s \n", &(g_student[i].name));
printf("please input student i's 性别(M/F).\n", i);
scanf("%c \n", &(g_student[i].sex));
printf("please input student i's 年龄.\n", i);
scanf("%d \n", &(g_student[i].age));
printf("please input student i's 各门课成绩.\n", i);
scanf("%f %f %f %f %f \n", &(g_student[i].score[0]), &(g_student[i].score[1]),
&(g_student[i].score[2]),&(g_student[i].score[3]),&(g_student[i].score[4]));
}
//get student's average
for(i = 0; i < MAX_NUM; i ++)
{
g_student[i].average = GetStudentAverage(&(g_student[i]));
//print the student's average if needed here
}
//get class average
for(i = 0; i < MAX_CLASS; i++)
{
averagescore[i] = GetClassAverage(i);
//print the class's average if needed here
}
}
#define MAX_NUM 10
#define MAX_CLASS 5
typedef struct tag_Student {
int num;
char name[20];
char sex;
int age;
float score[MAX_CLASS];
float average;
} STUDENT;
STUDENT g_student[MAX_NUM];
float GetClassAverage(int classnum)
{
int i = 0;
float total = 0.0;
if (classnum > MAX_CLASS || classnum < 0)
{
printf("No such class existed. \r\n");
return -1;
}
for(i = 0; i < MAX_NUM; i ++)
{
total += g_student[i].score[classnum];
}
return (total/MAX_NUM);
}
float GetStudentAverage(STUDENT *student)
{
int i =0;
int classnum = 5;
float total = 0.0;
if(student == 0)
{
printf("NULL Ponter.\r\n");
return -1;
}
for(i=0; i < classnum; i ++)
{
total += student->score[i];
}
return (total/classnum);
}
void main()
{
int i = 0;
float averagescore[MAX_CLASS];
printf("Please input student's information.\n");
for(i = 0; i < MAX_NUM; i ++)
{
printf("please input student i's 学号.\n", i);
scanf("%d \n", &(g_student[i].num));
printf("please input student i's 姓名.\n", i);
scanf("%s \n", &(g_student[i].name));
printf("please input student i's 性别(M/F).\n", i);
scanf("%c \n", &(g_student[i].sex));
printf("please input student i's 年龄.\n", i);
scanf("%d \n", &(g_student[i].age));
printf("please input student i's 各门课成绩.\n", i);
scanf("%f %f %f %f %f \n", &(g_student[i].score[0]), &(g_student[i].score[1]),
&(g_student[i].score[2]),&(g_student[i].score[3]),&(g_student[i].score[4]));
}
//get student's average
for(i = 0; i < MAX_NUM; i ++)
{
g_student[i].average = GetStudentAverage(&(g_student[i]));
//print the student's average if needed here
}
//get class average
for(i = 0; i < MAX_CLASS; i++)
{
averagescore[i] = GetClassAverage(i);
//print the class's average if needed here
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询