C语言_结构体练习
发布日期:2021-05-14 23:42:52 浏览次数:16 分类:精选文章

本文共 5796 字,大约阅读时间需要 19 分钟。

结构体练习

结构体的定义与使用

结构体是一种在C语言中用于存储多个变量及其类型信息的数据结构。它可以将相关的数据项整合在一起,便于处理和操作。本节将详细讲解结构体的使用方法。

结构体的基本定义

结构体由以下几个部分组成:

  • 结构体名:结构体的名称,以struct关键字开头,后跟名称。
  • 成员列表:结构体内部的数据项,每个成员有一个名称和类型。
  • 分割符:在每行结束后使用;,标识结构体成员的结束。
  • 例如,以下定义了一个学生结构体:

    struct student {
    char name[20]; // 姓名,字符数组,长度为20
    int id; // 学号,整数
    int score; // 成绩,整数
    };

    结构体的初始化

    结构体可以通过静态分配或动态分配来初始化。在静态分配中,需要提前定义结构体并使用& 运算符获取地址。

    struct student stu; // 结构体变量名
    stu.name = "张三";
    stu.id = 1;
    stu.score = 88;

    在动态分配中,使用malloc函数:

    struct student* stu = malloc(sizeof(struct student));
    stu->name = "李四";
    stu->id = 2;
    stu->score = 90;

    结构体的嵌套

    结构体可以嵌套包含同一类型或不同类型的结构体成员。例如,学生结构体中可以包含一个成绩结构体。

    struct score { // 定义一个成绩结构体
    int Chinese;
    int English;
    int Math;
    };
    struct student { // 学生结构体包含一个score结构体成员
    char name[20];
    int id;
    struct score scr; //嵌套的成绩结构体
    };

    注意:结构体不能递归嵌套自身,但可以包含指向自身的指针类型。

    结构体的数组与链表

    结构体可以像数组一样进行存储和访问操作,或者通过链表方式实现动态数据结构。

    结构体数组

    struct score {
    int English;
    int Math;
    float avg;
    };
    struct student {
    char name[20];
    int id;
    int Chinese;
    struct score scr[20]; // 数组方式存储成绩
    };
    int main() {
    struct student stu[20]; // 定义一个学生数组
    stu[0].name = "张三";
    stu[0].id = 1;
    // 填充其他成员
    ...
    return 0;
    }

    结构体链表

    #include 
    struct student { // 包含下一个指针的结构体
    int id;
    char name[20];
    struct student* next;
    };
    int main() {
    struct student* head = malloc(sizeof(struct student));
    head->next = NULL; // 初始化链表
    // 往循环中添加节点...
    return 0;
    }

    题目解答

    题1:学生信息管理

    需求:定义一个结构体表示学生的基本信息,并实现输入和输出。

    #include 
    struct student { // 结构体定义
    char name[20]; // 姓名
    int id; // 学号
    char province[20]; // 籍贯
    int c_score; // C语言考试成绩
    };
    int main() {
    struct student stu; // 结构体变量
    printf("请输入学生的姓名:");
    scanf("%s", stu.name);
    printf("请输入学生的学号:");
    scanf("%d", &stu.id);
    printf("请输入学生的籍贯:");
    scanf("%s", stu.province);
    printf("请输入C语言考试成绩:");
    scanf("%d", &stu.c_score);
    printf("\n学生信息如下:\n");
    printf("姓名:%s\t学号:%d\n籍贯:%s\tC成绩:%d\n",
    stu.name, stu.id, stu.province, stu.c_score);
    return 0;
    }

    输出示例

    请输入学生的姓名:张三
    请输入学生的学号:1
    请输入学生的籍贯:北京
    请输入C语言考试成绩:90
    学生信息如下:
    姓名:张三 学号:1 籍贯:北京 C成绩:90

    题2:学生成绩计算

    需求:定义结构体包含基本信息和三门成绩,计算平均分。

    方法一:结构体作为函数参数和返回值

    #include 
    struct student {
    char name[20]; // 姓名
    int id; // 学号
    int Chinese; // 中文成绩
    int English; // 英语成绩
    int Math; // 数学成绩
    float avg; // 平均分
    };
    struct student avg_fun(struct student s) {
    s.avg = (s.Chinese + s.English + s.Math) / 3.0;
    return s;
    }
    int main() {
    struct student stu = {"徐文涵", 12, 77, 88, 99, 0};
    stu = avg_fun(stu);
    printf("%s的三门主课均分:%.2f\n", stu.name, stu.avg);
    // 定义第二个学生
    struct student stu2 = {"王永先", 13, 77, 90, 77, 0};
    stu2 = avg_fun(stu2);
    printf("%s的三门主课均分:%.2f\n", stu2.name, stu2.avg);
    return 0;
    }

    题3:学员初始化与打印

    需求:定义学员结构体数组,动态初始化,从键盘输入学员信息,并自增学号,最后遍历打印。

    方法二:使用链表

    #include 
    #include
    #define SIZE 3
    struct student { // 学员结构体
    char name[20]; // 姓名
    int id; // 学号
    int score; // 成绩
    struct student* next; // 下一个节点指针
    };
    stud* creat(stud* p) {
    p = malloc(sizeof(stud));
    p->next = NULL;
    return p;
    }
    void insert(stud* head) {
    stud* p = creat(head);
    if (head == NULL) {
    head = p;
    } else {
    stud* current = head;
    while (current->next != NULL) {
    current = current->next;
    }
    current->next = p;
    }
    }
    void output(stud* head) {
    stud* current = head;
    while (current != NULL) {
    printf("%d %s %d\n", current->id, current->name, current->score);
    current = current->next;
    }
    }
    int main() {
    stud* head = creat(head);
    printf("已输入0个学生信息,要继续录入吗?(按q退出录入):");
    while (true) {
    insert(head);
    printf("已输入%d个学生信息,要继续录入吗?(按q退出):", NumberOfStudents);
    while (getchar() != '\n');
    if (getchar() == 'q') break;
    }
    printf("\n初始化后的学员信息:");
    output(head);
    free(head);
    return 0;
    }

    题4:日期计算函数

    需求:编写一个函数,输入日期,返回下一天日期,并考虑闰年。

    #include 
    struct date { // 日期结构体
    int year; // 年
    int mon; // 月
    int day; // 日
    };
    int adj_date(struct date* p) {
    if (p->year >= 0) {
    if (p->mon > 0 && p->mon < 13) {
    if (p->day > 0 && p->day <= day_of_month(p)) {
    return 1; // 表示日期合法
    }
    }
    }
    return 0; // 日期无效
    }
    int day_of_month(struct date* p) {
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (p->mon == 2 && is_leap(p)) {
    return 29;
    } else {
    if (p->mon < 12) {
    return daysInMonth[p->mon];
    } else {
    return daysInMonth[p->mon - 1];
    }
    }
    }
    bool is_leap(struct date* p) {
    if (p->year % 4 == 0 && p->year % 100 != 0) {
    return true;
    }
    if (p->year % 400 == 0) {
    return true;
    }
    return false;
    }
    struct date next_day(struct date* p) {
    struct date q = *p;
    if (q.day == day_of_month(p)) {
    q.day = 1;
    q.mon++;
    if (q-mon % 12 == 0) {
    q.mon = 1;
    q.year++;
    }
    } else {
    q.day++;
    }
    return q;
    }
    int main() {
    struct date date1;
    do {
    printf("请按顺序输入年份、月份、日期:");
    scanf("%d %d %d", &date1.year, &date1.mon, &date1.day);
    struct date* p1 = &date1;
    if (adj_date(p1)) {
    struct date next = next_day(p1);
    printf("输入的日期为:%d %d %d\n", date1.year, date1(mon), date1.day);
    printf("日期的下一天为:%d %d %d\n", next.year, next(mon), next.day);
    break;
    } else {
    printf("输入日期不合法!\n");
    continue;
    }
    } while (true);
    return 0;
    }

    通过以上的示例,可以了解如何在C语言中使用结构体来完成各种数据处理任务。结构体使得数据的存储和操作更加高效和直观。

    上一篇:C语言_文件流练习
    下一篇:练习_C指针与排序算法

    发表评论

    最新留言

    第一次来,支持一个
    [***.219.124.196]2025年04月23日 03时40分16秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章