定义结构体,通过动态数组和函数,实现对数据的输入、排序、输出

时间:2025-03-10 17:21:21
  • #include <>
  • #include <>
  • struct student
  • {
  • int age;
  • char name[20];
  • float score;
  • };
  • void Inputstudent(struct student * p,int a);
  • void Outputstudent(struct student * p,int b);
  • void paixu(struct student * p,int c);
  • int main(void)
  • {
  • int len;
  • printf("请输入学生个数:\n");
  • printf("len=");
  • scanf("%d",&len);
  • struct student * parr;
  • parr = (struct student *)malloc(len * sizeof(struct student));
  • Inputstudent(parr, len);
  • paixu(parr, len);
  • printf("\n\n\n");
  • Outputstudent(parr, len);
  • //paixu(parr, len);
  • return 0;
  • }
  • void Inputstudent(struct student * p,int a)
  • {
  • int i;
  • for(i=0; i<a; i++)
  • {
  • printf("请输入第%d个学生的信息\n",i+1);
  • printf("age=");
  • scanf("%d",&p[i].age);
  • printf("name=");
  • scanf("%s",p[i].name);
  • printf("score=");
  • scanf("%f",&p[i].score);
  • }
  • }
  • void Outputstudent(struct student * p,int b)
  • {
  • printf("按成绩由高到低排序为:\n");
  • int i;
  • for(i=0; i<b; i++)
  • {
  • printf("第%d名学生的信息是:\n",i+1);
  • printf("%d\n",p[i].age);
  • printf("%s\n",p[i].name);
  • printf("%3.2f\n",p[i].score);
  • }
  • }
  • void paixu(struct student *p, int c)
  • {
  • int i,j;
  • struct student t;
  • for(i=0; i<c-1; i++)
  • {
  • for(j=0; j<c-i-1; j++)
  • {
  • if(p[i].score < p[i+1].score)
  • {
  • t = p[i];
  • p[i] = p[i+1];
  • p[i+1] = t;
  • }
  • }
  • }
  • }
  • 相关文章