鱼C论坛

 找回密码
 立即注册
查看: 720|回复: 1

[已解决]显示结构体未定义,107行

[复制链接]
发表于 2020-12-19 11:54:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<winuser.inl>
  2. #include<stdio.h>
  3. #include <cstdlib>
  4. #include<string.h>
  5. struct Student getInput(struct Student stu);
  6. void printinformation(struct Student stu);

  7. //用比较计数法对结构体的num字段进行排序
  8. struct Student
  9. {
  10.         int num;
  11.         char name[20];
  12.         float score;
  13. }stu[3];

  14. struct Student getInput(struct Student stu)
  15. {
  16.         printf("enter the number of student:");
  17.         scanf_s("%d", &stu.num);

  18.         printf("enter the name of student:");
  19.         scanf_s("%s", stu.name);

  20.         printf("enter the score of student:");
  21.         scanf_s("%f", &stu.score);

  22.         return stu;
  23. }

  24. void printinformation(struct Student stu)
  25. {
  26.         printf("title: %d\n", stu.num);
  27.         printf("author: %s\n", stu.name);
  28.         printf("price: %f\n", stu.score);
  29. }

  30. //************************************计数排序******************************************
  31. int numbers_of_people = 3;
  32. //找出待排序的数组中最大(最小)的元素
  33. //统计数组中每个值为 i 的元素出现的次数,存入数组 C 的第 i 项
  34. //对所有的计数累加(从 C 中的第一个元素开始,每一项和前一项相加)
  35. //反向填充目标数组:将每个元素 i 放在新数组的第 C(i) 项,每放一个元素就将 C(i) 减去 1
  36. int* sort(int array[])
  37. {

  38.         int min_num, max_num;
  39.         min_num = array[0];
  40.         max_num = array[0];
  41.         //找出待排序的数组中最大(最小)的元素
  42.         //找最小最大
  43.         for (int j = 1; j < numbers_of_people; j++)
  44.         {
  45.                 if (min_num > array[j])
  46.                 {
  47.                         min_num = array[j];
  48.                 }

  49.                 if (max_num < array[j])
  50.                 {
  51.                         max_num = array[j];
  52.                 }
  53.         }

  54.         //统计数组中每个值为 i 的元素出现的次数,存入数组 C 的第 i 项
  55.         int* counter;
  56.         int counter_length = max_num - min_num + 1;
  57.         counter = new int[max_num + 1];

  58.         int count_min = min_num;
  59.         for (int i = 0; i <= max_num; i++)  //记录counter的位置
  60.         {
  61.                 counter[i] = 0;
  62.                 for (int j = 0; j < numbers_of_people; j++)
  63.                 {
  64.                         if (i == array[j])
  65.                         {
  66.                                 counter[i] ++;
  67.                         }
  68.                 }
  69.                 count_min++;
  70.         }

  71.         //对所有的计数累加(从 C 中的第一个元素开始,每一项和前一项相加),这里是用于判断有几个数?
  72.         //反向填充目标数组:将每个元素 i 放在新数组的第 C(i) 项,每放一个元素就将 C(i) 减去 1
  73.         //新数组的长度肯定是已经确定的,计数数组逐个读取
  74.         int* new_array;
  75.         new_array = new int[numbers_of_people];
  76.         count_min = min_num;
  77.         int i = 0, j = 0, array_position = 0;
  78.         for (int i = 0; i <= max_num; i++)  //记录counter的位置
  79.         {
  80.                 if (counter[i] != 0)
  81.                 {
  82.                         do
  83.                         {
  84.                                 new_array[array_position] = i; //count第i个位置存放的是array中值 == i的个数
  85.                                 counter[i] --;
  86.                                 array_position++;
  87.                         } while (counter[i] != 0);
  88.                 }
  89.         }
  90.         return new_array;
  91. }

  92. int main()
  93. {
  94.         struct Student b1, b2, b3;  //给 Book结构体定义两个结构体变量
  95.         int array[3];   //获取三个人的Num字段,并将其存入一个数组,对数组进行排序,而后按照排序后的结果进行输出
  96.         int i = 0;

  97.         printf("enter the information of the first student...\n");
  98.         b1 = getInput(b1); //定义了 getInput函数接收输入,将获得的值赋值给空的结构体变量b1
  99.         array[i] = b1.num;
  100.         printf("\n");

  101.         printf("enter the information of the second student...\n");
  102.         b2 = getInput(b2); //定义了 getInput函数接收输入,将获得的值赋值给空的结构体变量b1
  103.         i++;
  104.         array[i] = b2.num;
  105.         printf("\n");

  106.         printf("enter the information of the third student...\n");
  107.         b3 = getInput(b3); //定义了 getInput函数接收输入,将获得的值赋值给空的结构体变量b1
  108.         i++;
  109.         array[i] = b3.num;
  110.         printf("\n");

  111.         printf("\n\n录入完毕,现在开始打印。。。");
  112.         printf("打印第一个人的信息。。。");
  113.         printinformation(b1);
  114.         printf("\n");
  115.         printf("打印第二个人的信息。。。");
  116.         printinformation(b2);
  117.         printf("\n");
  118.         printf("打印第三个人的信息。。。");
  119.         printinformation(b3);

  120.         int* sort_array;
  121.         sort_array = sort(array);
  122.         printf("三个人num的是:");
  123.         for (int i = 0; i < numbers_of_people; i++)  //记录counter的位置
  124.         {
  125.                 printf("%d ", array[i]);
  126.         }
  127.         printf("\n");

  128.         printf("三个人num按照大小排序是:");
  129.         for (int i = 0; i < numbers_of_people; i++)  //记录counter的位置
  130.         {
  131.                 printf("%d ", sort_array[i]);
  132.         }

  133.         return 0;
  134. }
复制代码
最佳答案
2020-12-19 12:09:48
本帖最后由 jackz007 于 2020-12-19 12:19 编辑

        第一行
  1. #include<winuser.inl>
复制代码

        这都不报错?
        编译器给出的错误信息是 b1、b2 、b3 未初始化,并不是变量未定义,意思是在赋值前访问了变量。
        解决方法,必须修改函数 getInput() 的定义
  1. void getInput(struct Student * stu)
  2. {
  3.         printf("enter the number of student:");
  4.         scanf_s("%d", & stu -> num)           ;

  5.         printf("enter the name of student:")  ;
  6.         scanf_s("%s", stu -> name , 20)       ;

  7.         printf("enter the score of student:") ;
  8.         scanf_s("%f", & stu -> score)         ;
  9. }
复制代码

     调用:
  1.         getInput(& b1)                        ;
复制代码
2.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-19 12:09:48 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-12-19 12:19 编辑

        第一行
  1. #include<winuser.inl>
复制代码

        这都不报错?
        编译器给出的错误信息是 b1、b2 、b3 未初始化,并不是变量未定义,意思是在赋值前访问了变量。
        解决方法,必须修改函数 getInput() 的定义
  1. void getInput(struct Student * stu)
  2. {
  3.         printf("enter the number of student:");
  4.         scanf_s("%d", & stu -> num)           ;

  5.         printf("enter the name of student:")  ;
  6.         scanf_s("%s", stu -> name , 20)       ;

  7.         printf("enter the score of student:") ;
  8.         scanf_s("%f", & stu -> score)         ;
  9. }
复制代码

     调用:
  1.         getInput(& b1)                        ;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-7 12:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表