C语言之结构体

1、用数据结构定义学生信息,有学号,姓名,5门课程的成绩,编一程序,输入20个学生成绩,求出总分最高的学生姓名并输出结果。要求编写3个函数,它们的功能分别为:

1)输入函数,用于从键盘读入学号、姓名和5门课的成绩;

2)计算总分函数,以计算每位学生的总分;

3)输出函数,显示每位学生的学号、总分和分数。

说明:这三个函数的形式参数均为结构体指针和整型变量,函数的类型均为void.

#include<stdio.h>

struct stu

{

       int b;

       char a[20];

       float one;

       float two;

       float three;

       float four;

       float five;

};

int main()

{

       struct stu s[20];

       float sum[20];

       for(int i=0;i<20;i++)

       {

              scanf("%d",&s[i].b);

              scanf("%s",&s[i].a);

              scanf("%f",&s[i].one);

              scanf("%f",&s[i].two);

              scanf("%f",&s[i].three);

              scanf("%f",&s[i].four);

              scanf("%f",&s[i].five);

        sum[i]=s[i].one+s[i].two+s[i].three+s[i].four+s[i].five;

             

       }

       int max=sum[0];

       int n=0;

       for(int j=1;j<20;j++)

       {

              if(sum[j]>max)

              {

                     max=sum[j];

                     n=j;

        }

              printf("%-20d %s %f %f %f %f %f\n",s[j].b,s[j].a,s[j].one,s[j].two,s[j].three,s[j].four,s[j].five);

       }

      

       printf("总分最高的学生:%s 成绩为:%f",s[n].a,sum[n]);

       return 0;

}

2、编写一程序,运用插入结点的方法,将键盘输入的n个整数(输入0结束)插入到链表中,建立一个从小到大的有序链表。

#include <stdio.h>

#include <stdlib.h>

struct Node {

    int data;

    struct Node* next;

};

struct Node* createNode(int value);

void insertSorted(struct Node** head, int value);

void displayList(struct Node* head);

void freeList(struct Node* head);

int main() {

    struct Node* head = NULL;

    int input;

    printf("请输入整数,以0结束输入:\n");

    do {

        scanf("%d", &input);

        if (input != 0) {

            insertSorted(&head, input);

        }

    } while (input != 0);

    printf("有序链表的内容:\n");

    displayList(head);

    freeList(head);

    return 0;

}

struct Node* createNode(int value) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    if (newNode == NULL) {

        printf("内存分配失败\n");

        exit(1);

    }

    newNode->data = value;

    newNode->next = NULL;

    return newNode;

}

void insertSorted(struct Node** head, int value) {

    struct Node* newNode = createNode(value);

    if (*head == NULL || value < (*head)->data) {

        newNode->next = *head;

        *head = newNode;

    } else {

        struct Node* current = *head;

        while (current->next != NULL && current->next->data < value) {

            current = current->next;

        }

        newNode->next = current->next;

        current->next = newNode;

    }

}

void displayList(struct Node* head) {

    struct Node* current = head;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

    printf("\n");

}

void freeList(struct Node* head) {

    struct Node* current = head;

    struct Node* next;

    while (current != NULL) {

        next = current->next;

        free(current);

        current = next;

    }

}

相关推荐

  1. C语言结构

    2023-12-29 23:30:01       29 阅读
  2. c语言-结构

    2023-12-29 23:30:01       41 阅读
  3. 结构(C语言)

    2023-12-29 23:30:01       34 阅读
  4. C语言结构

    2023-12-29 23:30:01       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 23:30:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 23:30:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 23:30:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 23:30:01       20 阅读

热门阅读

  1. Red Hat系列Docker安装与移除

    2023-12-29 23:30:01       30 阅读
  2. uniapp-H5项目的坑

    2023-12-29 23:30:01       37 阅读
  3. Python爬虫实战演练之爬去VIP电影

    2023-12-29 23:30:01       38 阅读
  4. 大模型系列课程学习

    2023-12-29 23:30:01       26 阅读
  5. MySQL 设置商品乐观锁号示例

    2023-12-29 23:30:01       37 阅读
  6. 力扣:435. 无重叠区间(贪心)

    2023-12-29 23:30:01       36 阅读
  7. Leetcode的AC指南 —— 哈希法:454. 四数相加 II

    2023-12-29 23:30:01       44 阅读