任務是使用scanf("%d")
然後用printf("%с")
不一樣的atoi標準功能再次打印得到int
,itoa。至於我明白我需要把所有的數字再加入\0
char和打印,但是我怎麼可以把它。我想到了分割number%10 + /0
和number/10
以減少1個字符的循環。 因此代碼看起來應該像平滑這個INT轉換爲char
#include <conio.h>
#include <stdio.h>
main(void)
{
int number,reserve ;
char Array[50];
scanf_s("%d",&number);
if (number > 0 || number == 0)
{
do
{
reserve = number % 10;
printf("%c", reserve + '/0');
number /= 10;
} while (number != 0);
}
else
{
number *= -1;
printf("-");
do
{
reserve = number % 10;
printf("%c", reserve + '/0');
number /= 10;
} while (number != 0);
}
_getch();
return 0;
}
同樣可以有負數,所以我需要一些if
語句來檢查它是否爲負,並且在情況下,它是循環應該避免它,我們不會得到類似-%10
所以我不知道如果循環是正確的(希望有人會解決它並解釋我應該如何)。等待你的建議。行
number = number % 10;
的
循環控制是錯誤的:例如,對於'42'你需要'2'循環不'42'。嘗試'do {...} while(number!= 0);'。並仔細考慮循環內容。請*運行*您的代碼! –
你應該看看[適當的C格式](// prohackr112.tk/r/properties-formatting)。或者學習如何[徹底模糊你的代碼](// prohackr112.tk/r/proper-c-obfuscation)。 –
'itoa'不是一個標準功能 –