• [C]struct, union的结合


    #include <stdio.h>
    typedef union {
        double math;
        double phys;
        double chem;
    } Score;
    
    typedef struct student {
        char name[10];
        int age;
        Score grade;
        char whichSubject;
    } Student;
    
    void input(Student *s) {
        printf("input your name: ");
        scanf("%s", s->name);
        printf("input your age: ");
        scanf("%d", &s->age);
        printf("which subject m/p/c: ");
        scanf(" %c", &s->whichSubject); //<<这里注意" %c"前有个空格哦
        switch (s->whichSubject) {
            case 'm': scanf("%lf", &s->grade.math); break;
            case 'p': scanf("%lf", &s->grade.phys); break;
            case 'c': scanf("%lf", &s->grade.chem); break;
        }
    }
    
    void print(Student *s) {
        switch (s->whichSubject) {
            case 'm': printf("My name is %s, and I'm %d. I got %f in Math
    ", s->name, s->age, s->grade.math); break;
            case 'p': printf("My name is %s, and I'm %d. I got %f in Physics
    ", s->name, s->age, s->grade.phys); break;
            case 'c': printf("My name is %s, and I'm %d. I got %f in Chemistry
    ", s->name, s->age, s->grade.chem); break;
        }
    }
    
    int main( )
    {
        Student s1;
        input(&s1);
        print(&s1);
        return 0;
    }

    与数组结合

    //struc, union,与array结合
    #include <stdio.h>
    typedef union {
        double math;
        double phys;
        double chem;
    } Subject;
    
    typedef struct {
        char name[20];
        int age;
        Subject scores;
        char whichSubject;
    } Student;
    
    void input(Student *s, int n) {
        for (int i = 0; i < n; ++i) {
            printf("input your name: ");
            scanf(" %19[^
    ]", s[i].name); //" %19[^
    ]"中要有空格
            printf("input your age: ");
            scanf("%d", &s[i].age);
            printf("which subject m/p/c: ");
            scanf(" %c", &s[i].whichSubject); //" %c"中要有空格
            switch ( s[i].whichSubject ) {
                case 'm': printf("input your math score: "); scanf("%lf", &s[i].scores.math); break;
                case 'p': printf("input your physics score: "); scanf("%lf", &s[i].scores.phys); break;
                case 'c': printf("input your chemistry score: "); scanf("%lf", &s[i].scores.chem); break;
            }
        }
    }
    
    double getAve(Student *s, int n) {
        double sum = 0;
        int i;
        for ( i = 0; i < n; ++i ) {
            switch (s[i].whichSubject) {
                case 'm': sum += s[i].scores.math; break;
                case 'p': sum += s[i].scores.phys; break;
                case 'c': sum += s[i].scores.chem; break;
            }
        }
        return sum / n;
    }
    
    void display(Student *s, int n) {
        for ( int i = 0; i < n; ++i ) {
            switch (s[i].whichSubject) {
                case 'm': printf("%s's math score is %f
    ", s[i].name, s[i].scores.math); break;
                case 'p': printf("%s's physics score is %f
    ", s[i].name, s[i].scores.phys); break;
                case 'c': printf("%s's chemistry score is %f
    ", s[i].name, s[i].scores.chem); break;
            }
        }
    }
    
    int main() {
        const int size = 2;
        Student stu[size];
        input(stu, size);
        printf("average = %f
    ", getAve(stu, size));
        display(stu, size);
    }
  • 相关阅读:
    Android-PullToRefresh 下拉刷新增加setOnItemLongClickListener
    【453】周志华-机器学习-读书笔记
    【452】pandas筛选出表中满足另一个表所有条件的数据
    【451】python 同一行打印进度条
    【449】Win10 蓝牙耳机链接没有声音
    HBase(一)HBase入门简介
    kafka可视化客户端工具(Kafka Tool)的基本使用
    Kafka(五)Kafka的API操作和拦截器
    Kafka(四)Kafka在zookeeper中的存储
    Kafka(三)Kafka的高可用与生产消费过程解析
  • 原文地址:https://www.cnblogs.com/profesor/p/13276721.html
Copyright © 2020-2023  润新知