69,371
社区成员
发帖
与我相关
我的任务
分享
#define STUDENT_SIZE (3)
typedef struct ScoreInfo{//分数信息
int mathScore;
int chineseScore;
int englishScore;
}ScoreInfo;
typedef struct StudentInfo{//学生信息 包含分数信息
char stuName[10];
int stuID;
int stuAge;
ScoreInfo stuScore;
}StudentInfo;
void displayStuInfo(StudentInfo stu[],int pos); //打印最高分学生详细信息
bool sumScore(StudentInfo* pStu,int *pSumArray); //计算总分
int findFirst(StudentInfo* pStu); //查找最高分数的学生 返回学生序号
int main(){
StudentInfo stuArray[STUDENT_SIZE]=({"Lisa",1,12,{25,80,75}},
{"Tom",2,13,{85,70,80}},
{"Lucy",3,12,{100,90,99}});
int pos=0;
pos=findFirst( ); //①
if( ){ //②
printf("\nError,please check");
}else{
displayStuInfo(stuArray,pos); //打印最高分学生详细信息
}
return 0;
}
bool sumScore(StudentInfo* pStu,int *pSumArray){
if(NULL==pStu){
return false;
}
if(NULL==pSumArray){
return false;
}
int i=0;
for(i=0;i<STUDENT_SIZE;i++){
pSumArray[i]=( ); //③
}
return true;
}
int findFirst(StudentInfo* pStuArray){
if(NULL==pStuArray){
return -1;
}
int sumArray[STUDENT_SIZE]= ; //④
int firstPos=0;
int ret=true;
int maxScore=0;
int t;
if(false==sumScore( )){ //⑤
return -1;
}
maxScore=sumArray[0];
for(i=1;i<STUDENT_SIZE;i++){
if(maxScore<sumArray[i]){
maxScore=sumArray[i];
//⑥记录位置
}
}
return firstPos;
}
void displayStuInfo(StudentInfo stu[],int pos){
printf("\n name:%s",stu[pos].stuName);
printf("\n ID:%d",stu[pos].stuID);
printf("\n Age:%d",stu[pos].stuAge);
printf("\n Math %d", ); //⑦
printf("\n Chinese %d", ); //⑧
printf("\n English %d", ); //⑨
}
#include <stdio.h>
#include <stdlib.h>
#define STUDENT_SIZE (3)
typedef struct ScoreInfo{//分数信息
int mathScore;
int chineseScore;
int englishScore;
}ScoreInfo;
typedef struct StudentInfo{//学生信息 包含分数信息
char stuName[10];
int stuID;
int stuAge;
ScoreInfo stuScore;
int total_score;
}StudentInfo;
void displayStuInfo(StudentInfo stu[],int pos); //打印最高分学生详细信息
void sumScore(StudentInfo* pStu); //计算总分
int findFirst(StudentInfo* pStu); //查找最高分数的学生 返回学生序号
int main(void)
{
StudentInfo stuArray[STUDENT_SIZE] = {
{"Lisa",1,12,{25,80,75}, 0},
{"Tom",2,13,{85,70,80}, 0},
{"Lucy",3,12,{100,90,99}, 0}
};
int pos=0;
sumScore(stuArray);
pos = findFirst(stuArray);
if(pos < 0){
printf("\nError,please check");
}else{
displayStuInfo(stuArray,pos); //打印最高分学生详细信息
}
return 0;
}
void sumScore(StudentInfo* pStu)
{
if(NULL==pStu){
return;
}
int i = 0;
for(i = 0; i < STUDENT_SIZE; i++){
pStu[i].total_score = pStu[i].stuScore.chineseScore + pStu[i].stuScore.mathScore + pStu[i].stuScore.englishScore;
}
}
int findFirst(StudentInfo* pStuArray)
{
if(NULL == pStuArray){
return -1;
}
int firstPos = -1;
int maxScore = 0;
int i;
for(i = 0;i < STUDENT_SIZE; i++){
if(maxScore < pStuArray[i].total_score){
maxScore = pStuArray[i].total_score;
firstPos = i;
}
}
return firstPos;
}
void displayStuInfo(StudentInfo stu[],int pos)
{
printf("\n name:%s",stu[pos].stuName);
printf("\n ID:%d",stu[pos].stuID);
printf("\n Age:%d",stu[pos].stuAge);
printf("\n Math %d", stu[pos].stuScore.mathScore);
printf("\n Chinese %d", stu[pos].stuScore.chineseScore);
printf("\n English %d", stu[pos].stuScore.englishScore);
}
在数据结构中加了一个变量,这样所有的函数都跟着变化。
可以参考一下,有问题继续提出来。