一道c语言练习题,求高手帮忙解答

偷偷闲浮生 2017-08-14 12:27:57
最近想入门c,做的一道练习题,因为有java基础,所以有些解题思路,但具体的语言写法不怎么会,求高手指导一二,非常感谢.

查询出最高分数的学生信息


#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", ); //⑨
}



...全文
178 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
偷偷闲浮生 2017-08-14
  • 打赏
  • 举报
回复
感谢楼上两位大神的无私帮助 想对cfjtaishan点对我有用,手滑点了板砖,sorry哈
自信男孩 2017-08-14
  • 打赏
  • 举报
回复
#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);
}
在数据结构中加了一个变量,这样所有的函数都跟着变化。 可以参考一下,有问题继续提出来。
fussier 2017-08-14
  • 打赏
  • 举报
回复
1.stuArray 2.pos < 0 3.pStu[i].stuScore.chineseScore + pStu[i].stuScore.englishScore + pStu[i].stuScore.mathScore 4.{0, 0, 0} 5.pStuArray, sumArray 6.firstPos = i; 7.stu[pos].stuScore.mathScore 8.stu[pos].stuScore.chineseScore 9.stu[pos].stuScore.englishScore

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧