鱼C论坛

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

自己写的itoa函数最后打印不出来,找了半天也没找到问题..

[复制链接]
发表于 2016-11-1 09:26:34 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #define  SWAP(a,b) \
  3. do\
  4. {\
  5.         a=(a)+(b);\
  6.         b=(a)-(b);\
  7.         b=(a)-(b);\
  8. }while(0)

  9. void itoa(int num,char *ch)
  10. {
  11.         int rest;
  12.         int i =0;
  13.         int j=0;
  14.        
  15.         do
  16.         {
  17.         rest=num%10;
  18.         ch[i++]=rest;
  19.         num/=10;
  20.         }while(num!=0);
  21.        
  22.         ch[i]='\0';
  23.        
  24.         printf("ch=%s\n",ch);
  25.        
  26.         for(j=0;j<i/2;j++)
  27.         {
  28.                 SWAP(ch[j],ch[i-1-j]);
  29.         }
  30.        
  31.         printf("ch=%s\n",ch);
  32. }
  33. int main(void)
  34. {
  35.         char buff[16];
  36.        
  37.         itoa(200,buff);
  38.        
  39.         puts(buff);
  40.    return 0;
  41. }
复制代码

麻烦帮忙看一下哪里出了问题~感谢~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-1 12:34:12 | 显示全部楼层
本帖最后由 人造人 于 2016-11-1 12:36 编辑

SWAP 宏 有问题
24行 有问题
  1. #include<stdio.h>

  2. // 注意这里
  3. #define  SWAP(a,b) \
  4. do\
  5. {\
  6.         a=(a)+(b);\
  7.         b=(a)-(b);\
  8.         a=(a)-(b);\
  9. }while(0)

  10. void itoa(int num, char *ch)
  11. {
  12.         int rest;
  13.         int i = 0;
  14.         int j = 0;

  15.         do
  16.         {
  17.                 rest = num % 10;
  18.                 ch[i++] = rest + '0'; // 注意这里
  19.                 num /= 10;
  20.         } while(num != 0);

  21.         ch[i] = '\0';

  22.         //printf("ch=%s\n", ch);

  23.         for(j = 0; j<i / 2; j++)
  24.         {
  25.                 SWAP(ch[j], ch[i - 1 - j]);
  26.         }

  27.         //printf("ch=%s\n", ch);
  28. }
  29. int main(void)
  30. {
  31.         char buff[16];

  32.         itoa(12345, buff);

  33.         puts(buff);
  34.         return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 15:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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