|
发表于 2020-7-8 15:19:56
|
显示全部楼层
你这个程序我看的都一愣一愣的,全程用一个str数组存储所有结果,不出错才怪
应该声明一个int数组,读取一个数字就调用atoi函数将字符串转换为int并存入数组中,然后将str清空
最后直接比较int数组中的值
参考以下代码(纯手撸,没经过调试)
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- using namespace std;
- int main()
- {
- char str[256] = {0};
- int k = 0;
- int nums[5] = {0};
- for (int i=0; i<5; i++)
- {
- printf("请输入数字:");
- cin>>str;
- nums[k] = atoi(str);
- printf("%d\n", nums[k]);
- k++;
- memset(str, '\0', sizeof(str));
- }
-
- for (int act1=0; act1 < 5; act1++)
- {
- for (int act2=0; act2 < act1; act2++)
- {
- if (nums[act2] > nums[act2 + 1])
- {
- int temp = nums[act2];
- nums[act2] = nums[act2 + 1];
- nums[act2 + 1] = temp;
- }
- }
- }
-
- printf("冒泡排序:")
- for (int i=0; i<5; i++)
- {
- printf("%d ", nums[i]);
- }
- return 0;
- }
复制代码 |
|