C语言:输入学生信息(学号姓名成绩)并按成绩高低升序输出

题目:

定义学生结构体Student(含学号、姓名、成绩)和排序函数sort,该函数使用冒泡排序法按成绩升序排序。在主函数中输入5个学生的学号、姓名和成绩,调用sort函数对学生数据排序,最后输出排序后的学生全部信息。

主要考察的知识点:

  1. 结构体数组的元素输入输出。

  1. 结构体作为参数传递。

  1. 冒泡排序法。

解题思路:

  1. 定义学生信息结构体。

  1. 定义两数交换函数。

  1. 定义冒泡排序函数。

  1. 主函数中输入学生信息。

  1. 调用排序函数。

  1. 输出学生信息。

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#define N 5

typedef struct {//定义学生信息结构体
    int id;//学号
    char name[10];//姓名
    int score;//成绩
}student;

void swap(int* a, int* b)//定义两数交换函数
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void sort(student* a)//定义冒泡排序函数
{
    for (int j = 0; j < N - 1; j++)//冒泡排序法:两两依次比较,数大后移
        for (int i = 0; i < N - 1-j; i++)
            if (a[i].score > a[i + 1].score)
            {
                swap(&a[i].score, &a[i + 1].score);//调用两数交换函数
                swap(&a[i].id, &a[i + 1].id);
                char temp3[10] = { };
                strcpy(temp3, a[i].name);//字符串复制函数,头文件是string.h
                strcpy(a[i].name, a[i+1].name);
                strcpy(a[i + 1].name, temp3);
            }
}

int main()
{
    student a[N];
    for (int i = 0; i < N; i++)//输入学生信息
    {
        printf("请输入第%d个学生的学号、姓名、成绩:",i+1);
        scanf("%d%s%d", &a[i].id,&a[i].name,&a[i].score);
    }
    sort(a);//调用排序函数
    printf("\n\t学号\t姓名\t成绩\n");
    for (int i = 0; i < N; i++)//输出排序后的学生信息
    {
        printf("\t%d\t%s\t%d\n", a[i].id, a[i].name, a[i].score);
    }
    return 0;
}

运行结果:

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义学生结构体 typedef struct student { char name[20]; int id; float score; struct student* next; } Student; // 插入学生信息 void insert(Student** head, char* name, int id, float score) { // 创建新的学生节点 Student* new_student = (Student*)malloc(sizeof(Student)); strcpy(new_student->name, name); new_student->id = id; new_student->score = score; new_student->next = NULL; // 如果链表为空,直接将新节点作为头节点 if (*head == NULL) { *head = new_student; return; } // 找到链表尾部,将新节点插入尾部 Student* cur = *head; while (cur->next != NULL) { cur = cur->next; } cur->next = new_student; } // 按姓名查找学生信息 Student* find_by_name(Student* head, char* name) { Student* cur = head; while (cur != NULL) { if (strcmp(cur->name, name) == 0) { return cur; } cur = cur->next; } // 没有找到对应姓名学生 return NULL; } int main() { // 初始化链表 Student* head = NULL; // 插入学生信息 insert(&head, "Tom", 1001, 90.5); insert(&head, "Jerry", 1002, 85.0); insert(&head, "Alice", 1003, 95.0); insert(&head, "Bob", 1004, 88.5); // 按姓名查找学生信息输出 Student* found_student = find_by_name(head, "Alice"); if (found_student != NULL) { printf("Name: %s\n", found_student->name); printf("ID: %d\n", found_student->id); printf("Score: %.1f\n", found_student->score); } else { printf("Student not found.\n"); } return 0; } ``` 以上程序定义了一个 `Student` 结构体,包含学生姓名学号成绩和下一个节点的指针。使用 `insert` 函数将学生信息插入链表中,使用 `find_by_name` 函数按姓名查找学生信息。在 `main` 函数中初始化链表并插入学生信息,然后查找名为 "Alice" 的学生信息输出

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值