#include <stdio.h> #include <stdlib.h> #include <string.h>

typedef struct Student { int sno; char name[10]; char sex; int age; } Student;

typedef struct Table { Student stu[1000]; int num; } Table;

int main() { Table HTable; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%s %c%d", &HTable.stu[i].sno, HTable.stu[i].name, &HTable.stu[i].sex, &HTable.stu[i].age); } scanf("%d%s %c%d", &HTable.stu[n].sno, HTable.stu[n].name, &HTable.stu[n].sex, &HTable.stu[n].age); HTable.num = n + 1; int insert_pos = n; for (int i = n - 1; i >= 0; i--) { if (HTable.stu[i].sno > HTable.stu[n].sno) { HTable.stu[i + 1] = HTable.stu[i]; insert_pos = i; } } HTable.stu[insert_pos] = HTable.stu[n]; for (int i = 0; i < HTable.num; i++) { printf("%d %s %c %d\n", HTable.stu[i].sno, HTable.stu[i].name, HTable.stu[i].sex, HTable.stu[i].age); } return 0; }

C语言实现学生信息排序插入 - 保持升序排列

原文地址: https://www.cveoy.top/t/topic/ojLP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录