现有一个学生表学生信息包活:学号、姓名、性别、年龄表中包含n个学生数据已经按学号从小到大排序现在输入一个新的学生信息然后将它插入到表中的对应位置保证表中数据仍然照升序排列打印插入以后的数表。结构体定义如下:typedef struct Student{int sno;char name10;char sex;int age;Student;typedef struct TableSludent s
#include<stdio.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 table; int n; scanf("%d",&n); table.num=n; for(int i=0;i<n;i++){ scanf("%d%s %c%d",&table.stu[i].sno,table.stu[i].name,&table.stu[i].sex,&table.stu[i].age); } Student newstu; scanf("%d%s %c%d",&newstu.sno,newstu.name,&newstu.sex,&newstu.age); int pos=0;//插入位置 while(pos<table.num&&table.stu[pos].sno<newstu.sno){ pos++; } for(int i=table.num;i>pos;i--){ table.stu[i]=table.stu[i-1]; } table.stu[pos]=newstu; table.num++; for(int i=0;i<table.num;i++){ for(int j=i+1;j<table.num;j++){ if(table.stu[i].sno>table.stu[j].sno){ Student temp=table.stu[i]; table.stu[i]=table.stu[j]; table.stu[j]=temp; } } } for(int i=0;i<table.num;i++){ printf("%d %s %c %d\n",table.stu[i].sno,table.stu[i].name,table.stu[i].sex,table.stu[i].age); } return 0;
原文地址: https://www.cveoy.top/t/topic/fDEM 著作权归作者所有。请勿转载和采集!