【发布时间】:2015-12-06 09:58:36
【问题描述】:
我有一个问题,我需要将收到的数字转换为字符串,然后返回它。问题是我不知道我如何尝试添加 use (char)(number + 48) 但它不起作用我尝试使用 itoa 函数但同样的事情。但是该函数需要是递归的。 感谢您的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int itoc (int number, int base) {
if(number == 0) return number;
return (number % base) + 10 * itoc(number / base, base);
}
int main()
{ int a,b;
scanf("%d%d", &number ,&base);
printf("%d",itoc(number,base));
return 0;
}
上面的版本是返回整数的版本。
【问题讨论】:
-
递归调用是强制的吗?
-
只要转换不溢出且基数不大于 10,您的程序就可以工作。您发现哪些输入值不工作?
标签: c string recursion type-conversion