C/C++ 学习笔记(三)——结构体与共同体

in 编程技术分享 with 0 comment

注:对结构体与共同体不太熟悉,难免有错误的地方,欢迎指正。

//
// Created by imbytecat on 2020/5/20.
// 对下面的结构体的变量 s1 进行初始化,并输入出结果。
//

struct student {
    int num;
    char name[20];
    char sex;
};

int main() {
    struct student s1 = {2, "imbytecat", 'm'};
    return 0;
}
//
// Created by imbytecat on 2020/5/20.
// 有一个结构体变量 stud,内含学生学号、姓名和四门成绩,要求在 main() 函数中赋值,在另一函数 pr() 中将它们输出,这里用结构体变量做函数参数。
//

#include <string>

using namespace std;

struct Student {
    int num;
    const char *name;
    float score[4];
};

int main() {
    void pr(Student);
    Student stud{};
    stud.num = 2;
    stud.name = "imbytecat";
    stud.score[0] = 70.0;
    stud.score[1] = 80.0;
    stud.score[2] = 90.0;
    stud.score[3] = 100.0;
    pr(stud);
    return 0;
}

void pr(Student stu) {
    printf("%d %s %f %f %f %f", stu.num, stu.name, stu.score[0], stu.score[1], stu.score[2], stu.score[3]);
}
//
// Created by imbytecat on 2020/5/20.
// 对候选人得票的统计程序。设有 3 个候选人,每次输入一个得票的候选人的名字,要求最后输出各人得票结果。
//

#include<iostream>
#include<string>

using namespace std;

struct Person {
    char name[20];
    int count;
} candidate[3] = {"im", 0, "byte", 0, "cat", 0};

int main() {
    int i, j;
    char candidate_name[20];
    for (i = 1; i <= 10; i++) {
        cin >> candidate_name;
        for (j = 0; j < 3; j++)
            if (strcmp(candidate_name, candidate[j].name) == 0)
                candidate[j].count++;
    }
    cout << "Result:" << endl;
    for (i = 0; i < 3; i++)
        cout << candidate[i].name << candidate[i].count << endl;
    return 0;
}
//
// Created by imbytecat on 2020/5/20.
// 建立一个学生档案的结构体数组,输入并输出学生的信息。要求输入和输出要写出函数。
//

#include <cstdio>

struct student {
    int xue, a, b, e;
    char c[99];
} stu[5];

void input(struct student stu[], int n) {
    int i;
    for (i = 0; i < n; i++)
        scanf("%d%s%d%d%d", &stu[i].xue, stu[i].c, &stu[i].a, &stu[i].b, &stu[i].e);
}

void print(struct student stu[], int n) {
    int i;
    for (i = 0; i < n; i++)
        printf("%d %s %d %d %d\n", stu[i].xue, stu[i].c, stu[i].a, stu[i].b, stu[i].e);
}

int main() {
    const int n = 5;
    struct student stu[n];
    void input(struct student [], int);
    void print(struct student [], int);
    input(stu, n);
    print(stu, n);
    return 0;
}
//
// Created by imbytecat on 2020/5/20.
// 定义描述三维坐标点 (x, y, z) 的结构体类型的变量,完成坐标点的输入和输出,并求两点之间的距离。
//

#include <iostream>
#include <cmath>

using namespace std;

struct point {
    int x;
    int y;
    int z;
};

int main() {
    struct point p1, p2;
    float d;
    cout << "请输入两个点的坐标:";
    cin >> p1.x >> p1.y >> p1.z;
    cin >> p2.x >> p2.y >> p2.z;
    cout << "两个点的坐标分别为:" << endl;
    cout << "(" << p1.x << "," << p1.y << "," << p1.z << ")" << endl;
    cout << "(" << p2.x << "," << p2.y << "," << p2.z << ")" << endl;
    d = sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y) + (p1.z - p2.z) * (p1.z - p2.z));
    cout << "两点之间的距离:" << d << endl;
}
//
// Created by imbytecat on 2020/5/20.
// 定义一个共同体,内含两个成员变量 int i 和 char ch;要求输出整数从 48 到 57 的字符。
//

#include <cstdio>

typedef union _Number {
    int i;
    char ch;
} Number;

int main() {
    Number *num;
    for (int i = 0; i < 10; i++) {
        num[i].i = 48 + i;
        num[i].ch = (char) num[i].i;
        printf("%c\n", num[i].ch);
    }
    return 0;
}
//
// Created by imbytecat on 2020/5/20.
// 给出今天是星期几,输出 100 天后是星期几,用枚举类型实现。
//

#include <iostream>

using namespace std;

int main() {
    int d;
    enum {
        sun, mon, tue, wed, thu, fri, sat
    } day;
    char weekday[7][4] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
    cout << "请输入今天星期几:" << endl;
    cin >> d;
    switch (d % 7) {
        case 0:
            day = sun;
        case 1:
            day = mon;
        case 2:
            day = tue;
        case 3:
            day = wed;
        case 4:
            day = thu;
        case 5:
            day = fri;
        case 6:
            day = sat;
    }
    cout << "100天后为:" << weekday[(d + 100) % 7] << endl;
    return 0;
}
//
// Created by imbytecat on 2020/5/20.
// 有 10 个学生,每个学生的数据包括学号、姓名、英语、数学、物理三门课的成绩,从键盘输入 10 个学生数据,
// 要求打印出三门课程的总平均成绩,以及最高分的学生的数据(包括学号,姓名,三门课的平均成绩,平均分数)。
//

#include <iostream>

using namespace std;
struct Student {
    int num;
    char name[20];
    int score[3];
};

int main() {
    Student stu[10];
    int i, swap, x;
    float sum[10], avg[10], max = 0;
    for (i = 0, x = 1; i < 10; i++, x++) {
        cout << "请输入第" << x << "个人的学号、姓名和英语、数学、物理成绩:" << endl;
        cin >> stu[i].num >> stu[i].name;
        for (int j = 0; j < 3; j++)
            cin >> stu[i].score[j];
        sum[i] = stu[i].score[0] + stu[i].score[1] + stu[i].score[2];
        avg[i] = sum[i] / 3;
    }
    for (i = 0; i < 10; i++) {
        cout << "学号:" << stu[i].num << ",姓名:" << stu[i].name << ",平均成绩:" << avg[i] << endl;
        if (sum[i] > max) {
            max = sum[i];
            swap = i;
        }
    }
    i = swap;
    cout << "成绩最高的学生的信息:" << endl << "学号:" << stu[i].num << ",姓名:" << stu[i].name << ",数学成绩:" << stu[i].score[1]
         << ",物理成绩:" << stu[i].score[2] << ",英语成绩:" << stu[i].score[0] << ",平均成绩:" << avg[i] << endl;
    return 0;
}
Responses