zoukankan      html  css  js  c++  java
  • 7.4_结构体_返回结构体的函数

    #include <stdio.h>
    //函数返回值是结构体
    
    struct Student{
        unsigned int no;
        char name[16];
        float score;
    };
    
    //输入学生信息(参数:数组首地址)
    void input(struct Student *p);
    
    //寻找6个学生中成绩最高的学生(参数:数组首地址)
    struct Student find_max(struct Student *p);
    
    int main() {
    
        struct Student stu[6], max;
    
        input(stu);
    
        max = find_max(stu);
    
        printf("成绩最高的学生信息:
    ");
        printf("编号:%d 姓名:%s 成绩:%.2f
    ", max.no, max.name, max.score);
    
        return 0;
    }
    
    void input(struct Student *p){
        for(int i=0; i<6; i++){
            printf("输入第%d个学生的信息【编号 姓名 成绩】:", i+1);
            scanf("%d %s %f", &p[i].no, p[i].name, &p[i].score);
        }
    }
    
    struct Student find_max(struct Student *p){
        //假设第一个元素的成绩最高
        struct Student max = *p;
    
        p++;
        for(int i=0; i<6; i++){
            if(p->score > max.score){
                max = *p;
            }
            p++;
        }
    
        return max;
    }

  • 相关阅读:
    Hibernate入门
    安卓第四周作业
    第十三周作业
    第十三周上机作业
    第十二周作业
    第十二周上机作业
    第十一周作业
    第十一周上机作业
    第十周上机作业
    第九周上机作业
  • 原文地址:https://www.cnblogs.com/CPU-Easy/p/14052030.html
Copyright © 2011-2022 走看看