C语言之结构体

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

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

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

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

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

#include<stdio.h>

struct stu

{

int b;

char a20;

float one;

float two;

float three;

float four;

float five;

};

int main()

{

struct stu s20;

float sum20;

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

{

scanf("%d",&si.b);

scanf("%s",&si.a);

scanf("%f",&si.one);

scanf("%f",&si.two);

scanf("%f",&si.three);

scanf("%f",&si.four);

scanf("%f",&si.five);

sumi=si.one+si.two+si.three+si.four+si.five;

}

int max=sum0;

int n=0;

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

{

if(sumj>max)

{

max=sumj;

n=j;

}

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

}

printf("总分最高的学生:%s 成绩为:%f",sn.a,sumn);

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;

}

}

相关推荐
Omics Pro5 分钟前
~30,000+引用!理论2005,R包2008,多组学集成AI增强
开发语言·数据库·人工智能·算法·机器学习·自然语言处理·r语言
梦帮科技25 分钟前
【3.0修订版】创世与节点工程:genesis.json 与 node.toml 解剖
数据结构·算法·json·去中心化·区块链·智能合约·信任链
影视飓风TIM33 分钟前
Linux 进程虚拟地址空间详解
linux·算法
如何原谅奋力过但无声37 分钟前
【灵神高频面试题合集21-25】动态规划(下)
算法·动态规划
释厄6231 小时前
学术引用本体论——WorkBuddy 学术科学文化三违反
网络·人工智能·算法
梦帮科技1 小时前
【3.0修订版】 RNS 代币架构:ERC20 五件套扩展与六钱包分配
数据结构·后端·算法·架构·node.js·区块链·php
白山编程大哥1 小时前
C语言篇:可变参数函数
c语言·开发语言
水饺编程1 小时前
第1章:开发环境搭建,在 Windows 中安装 VS 2019
linux·c语言·汇编·visual studio
薛定e的猫咪1 小时前
(AISTATS 2023)BaCaDI:未知干预下的贝叶斯因果发现
人工智能·深度学习·算法·机器学习
ebiobiz1 小时前
Zig 嵌入式底层库选择与 build.zig 组织指南
c语言·c++·嵌入式硬件·mcu