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;

}

}

相关推荐
AndrewHZ12 分钟前
【图像处理基石】暗光增强算法入门:从原理到实战(Python+OpenCV)
图像处理·python·opencv·算法·计算机视觉·cv·暗光增强
lifallen1 小时前
从Apache Doris 学习 HyperLogLog
java·大数据·数据仓库·算法·apache
智驱力人工智能1 小时前
使用手机检测的智能视觉分析技术与应用 加油站使用手机 玩手机检测
深度学习·算法·目标检测·智能手机·视觉检测·边缘计算
姚瑞南2 小时前
【AI 风向标】四种深度学习算法(CNN、RNN、GAN、RL)的通俗解释
人工智能·深度学习·算法
补三补四2 小时前
SMOTE 算法详解:解决不平衡数据问题的有效工具
人工智能·算法
RTC老炮2 小时前
webrtc弱网-RobustThroughputEstimator源码分析与算法原理
网络·算法·webrtc
努力努力再努力wz2 小时前
【C++进阶系列】:万字详解智能指针(附模拟实现的源码)
java·linux·c语言·开发语言·数据结构·c++·python
凤年徐2 小时前
【C++】string的模拟实现
c语言·开发语言·c++
听风吹等浪起3 小时前
分类算法-逻辑回归
人工智能·算法·机器学习
敲代码的嘎仔3 小时前
JavaWeb零基础学习Day2——JS & Vue
java·开发语言·前端·javascript·数据结构·学习·算法