用c语言解决问题现有一个学生表学生信息包括:学号、姓名、性别、年龄表中包含n个学生数据 已经按照学号从小到大排序现在输入- 新的学生信息然后将它插入到表中的对应位置保证表中数据仍然按照升序排列打印插入以后的数据表 结构体定义如下: typedef structStudent intsno; charname10 charsex intage; Student; typedef struct Tab
#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;
原文地址: https://www.cveoy.top/t/topic/fDE2 著作权归作者所有。请勿转载和采集!