|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<winuser.inl>
- #include<stdio.h>
- #include <cstdlib>
- #include<string.h>
- struct Student getInput(struct Student stu);
- void printinformation(struct Student stu);
- //用比较计数法对结构体的num字段进行排序
- struct Student
- {
- int num;
- char name[20];
- float score;
- }stu[3];
- struct Student getInput(struct Student stu)
- {
- printf("enter the number of student:");
- scanf_s("%d", &stu.num);
- printf("enter the name of student:");
- scanf_s("%s", stu.name);
- printf("enter the score of student:");
- scanf_s("%f", &stu.score);
- return stu;
- }
- void printinformation(struct Student stu)
- {
- printf("title: %d\n", stu.num);
- printf("author: %s\n", stu.name);
- printf("price: %f\n", stu.score);
- }
- //************************************计数排序******************************************
- int numbers_of_people = 3;
- //找出待排序的数组中最大(最小)的元素
- //统计数组中每个值为 i 的元素出现的次数,存入数组 C 的第 i 项
- //对所有的计数累加(从 C 中的第一个元素开始,每一项和前一项相加)
- //反向填充目标数组:将每个元素 i 放在新数组的第 C(i) 项,每放一个元素就将 C(i) 减去 1
- int* sort(int array[])
- {
- int min_num, max_num;
- min_num = array[0];
- max_num = array[0];
- //找出待排序的数组中最大(最小)的元素
- //找最小最大
- for (int j = 1; j < numbers_of_people; j++)
- {
- if (min_num > array[j])
- {
- min_num = array[j];
- }
- if (max_num < array[j])
- {
- max_num = array[j];
- }
- }
- //统计数组中每个值为 i 的元素出现的次数,存入数组 C 的第 i 项
- int* counter;
- int counter_length = max_num - min_num + 1;
- counter = new int[max_num + 1];
- int count_min = min_num;
- for (int i = 0; i <= max_num; i++) //记录counter的位置
- {
- counter[i] = 0;
- for (int j = 0; j < numbers_of_people; j++)
- {
- if (i == array[j])
- {
- counter[i] ++;
- }
- }
- count_min++;
- }
- //对所有的计数累加(从 C 中的第一个元素开始,每一项和前一项相加),这里是用于判断有几个数?
- //反向填充目标数组:将每个元素 i 放在新数组的第 C(i) 项,每放一个元素就将 C(i) 减去 1
- //新数组的长度肯定是已经确定的,计数数组逐个读取
- int* new_array;
- new_array = new int[numbers_of_people];
- count_min = min_num;
- int i = 0, j = 0, array_position = 0;
- for (int i = 0; i <= max_num; i++) //记录counter的位置
- {
- if (counter[i] != 0)
- {
- do
- {
- new_array[array_position] = i; //count第i个位置存放的是array中值 == i的个数
- counter[i] --;
- array_position++;
- } while (counter[i] != 0);
- }
- }
- return new_array;
- }
- int main()
- {
- struct Student b1, b2, b3; //给 Book结构体定义两个结构体变量
- int array[3]; //获取三个人的Num字段,并将其存入一个数组,对数组进行排序,而后按照排序后的结果进行输出
- int i = 0;
- printf("enter the information of the first student...\n");
- b1 = getInput(b1); //定义了 getInput函数接收输入,将获得的值赋值给空的结构体变量b1
- array[i] = b1.num;
- printf("\n");
- printf("enter the information of the second student...\n");
- b2 = getInput(b2); //定义了 getInput函数接收输入,将获得的值赋值给空的结构体变量b1
- i++;
- array[i] = b2.num;
- printf("\n");
- printf("enter the information of the third student...\n");
- b3 = getInput(b3); //定义了 getInput函数接收输入,将获得的值赋值给空的结构体变量b1
- i++;
- array[i] = b3.num;
- printf("\n");
- printf("\n\n录入完毕,现在开始打印。。。");
- printf("打印第一个人的信息。。。");
- printinformation(b1);
- printf("\n");
- printf("打印第二个人的信息。。。");
- printinformation(b2);
- printf("\n");
- printf("打印第三个人的信息。。。");
- printinformation(b3);
- int* sort_array;
- sort_array = sort(array);
- printf("三个人num的是:");
- for (int i = 0; i < numbers_of_people; i++) //记录counter的位置
- {
- printf("%d ", array[i]);
- }
- printf("\n");
- printf("三个人num按照大小排序是:");
- for (int i = 0; i < numbers_of_people; i++) //记录counter的位置
- {
- printf("%d ", sort_array[i]);
- }
- return 0;
- }
复制代码
本帖最后由 jackz007 于 2020-12-19 12:19 编辑
第一行
这都不报错?
编译器给出的错误信息是 b1、b2 、b3 未初始化,并不是变量未定义,意思是在赋值前访问了变量。
解决方法,必须修改函数 getInput() 的定义
- void getInput(struct Student * stu)
- {
- printf("enter the number of student:");
- scanf_s("%d", & stu -> num) ;
- printf("enter the name of student:") ;
- scanf_s("%s", stu -> name , 20) ;
- printf("enter the score of student:") ;
- scanf_s("%f", & stu -> score) ;
- }
复制代码
调用:
|
-
|