user-0 提问: 求助C代码改错

回复:
user: user-0
content: 请问怎么去掉空格和换行 输出 1_3_4_2_5_6_8_7_10_9_\n 输出 1_3_4_2_5_6_8_7_10_9

user: user-0
content: 非常感谢 完美解决问题

user: user-1
content: 留个脚印，这个问题我想搞明白

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <string.h>   typedef int (*pfunc)(int );  int isEven(int temp) {     if(temp % 2 == 0)         return 1;     return 0; }  int isOdd(int temp) {     if(temp % 2 == 1)         return 1;     return 0; }  void sort(int num, int temp[])//对temp中的num个数进行排序 {     int i, j, t;     int swap;      for(i = 0; i < num; i ++)     {         swap = 0;         for(j = 1; j < num - i; j ++)         {             if(temp[j - 1] > temp[j])             {                 t = temp[j - 1];                 temp[j - 1] = temp[j];                 temp[j] = t;                 swap = 1;             }         }         if(swap == 0)             break;     } } void sort_odd(int n,int arr[])//对T中的数进行要求的排序，偶数位置不变，奇数排序 {     int i;     int x =0, y = 0,z = 0,b = 0;     int change[100];     int position[100];      for(i = 0; i < n; i ++)     {         if(isEven(arr[i]) == 1)         {             position[x] = i;             x++;         }         else         {             change[y] = arr[i];             y++;         }     }      sort(y, change);//对奇数排序     for( i = 0; i < n; i ++)     {         if(position[z] == i && z != x)         {             z++;         }         else         {             arr[i] = change[b++];         }     } }  void sort_even(int n,int arr[])//对T中的数进行要求的排序，奇数位置不变，偶数排序 {     int i;     int x =0, y = 0,z =0, b = 0;     int change[100];     int position[100];      for(i = 0; i < n; i ++)     {         if(isOdd(arr[i]) == 1)         {             position[x] = i;             x++;         }         else         {             change[y] = arr[i];             y++;         }     }     sort(y, change);//对偶数排序     for( i = 0; i < n; i ++)     {         if(position[z] == i && z != x)         {             z++;         }         else         {             arr[i] = change[b++];         }     } }   void select_sort(int *arr, int num, pfunc chk_func) {     int i, j;     int min, tmp;      for (i = 0; i < num; i++) {         if (!chk_func(arr[i]))             continue;         min = i;         for (j = i+1; j < num; j++) {             if (!chk_func(arr[j]))                 continue;             if (arr[min] > arr[j])                 min = j;         }         if (min != i) {             tmp = arr[min];             arr[min] = arr[i];             arr[i] = tmp;         }     } }  void bubble_sort(int *arr, int num) {     int i, j;     int tmp;      for (i = 0; i < num; i++)         for (j = 0; j < num - i - 1; j++)             if (arr[j] > arr[j+1]) {                 tmp = arr[j];                 arr[j] = arr[j+1];                 arr[j+1] = tmp;             }  }  void print_arr(int *arr, int len) {     int i;      for (i = 0; i < len; i++)         printf("%d  ", arr[i]);     putchar(10); }  //#define BUBBLE_SORT  int main() {     int i, n, k, cnt;     char word[16];     int num[100], *arr = NULL;      while (scanf("%s", word) != EOF) {         printf("Please input the number of array: ");         scanf("%d", &n); #ifdef BUBBLE_SORT         arr = (int *)malloc(sizeof(int) * n);         if (!arr) {             fprintf(stderr, "malloc error!\n");             exit(1);         }         if(strcmp(word,"odd") == 0) {             printf("%s\n", word);              for (i = 0, cnt = 0; i < n; i++) {                 scanf("%d", &num[i]);                 if (isOdd(num[i]))                     arr[cnt++] = num[i];             }             bubble_sort(arr, cnt);             for (i = 0, k = 0; k < cnt && i < n; i++)                 if (isOdd(num[i]))                     num[i] = arr[k++];             free(arr);         } else if (strcmp(word, "even") == 0) {             printf("%s\n", word);              for (i = 0, cnt = 0; i < n; i++) {                 scanf("%d", &num[i]);                 if (isEven(num[i]))                     arr[cnt++] = num[i];             }              bubble_sort(arr, cnt);              for (i = 0, k = 0; k < cnt && i < n; i++)                 if (isEven(num[i]))                     num[i] = arr[k++];             free(arr);         } #else         if(strcmp(word, "odd") == 0) {             printf("%s\n", word);             for (i = 0; i < n; i++)                 scanf("%d", &num[i]);             select_sort(num, n, isOdd);         } else if (strcmp(word, "even") == 0) {             printf("%s\n", word);             for (i = 0; i < n; i++)                 scanf("%d", &num[i]);             select_sort(num, n, isEven);         } #endif         printf("Show all numbers: \n");         print_arr(num, n);     }     return 0; }  以上是我的demo，可以测试一下； 使用了两种方法：select_sort是选择排序，bubble_sort是冒泡排序；一个是以时间换空间，一个是以空间换时间； 使用到了函数指针； 另外，改了一下原有代码中出现的bug;s 比如输入scanf第二个参数应该是地址，&n;

user: user-3
content: int main() {     int i,n;     //char *word;     char word[10];

user: user-3
content: 弄个冒泡排序就好勒呀，两个循环均跳过那些数字就好，就当不存在