我正在尝试编写一个将采用整数输入的程序,然后将其转换为单词。例如:123,一二三。也是-3908,负三九零八。 我的代码在90%的时间内工作,这是我在整数结束时放置一个或多个零的唯一问题。例如。 70800将成为七零八。它完全错过了结束零。我理解为什么会这样,但有人知道是否有办法绕过它。 PS(作为此任务的一部分,我不允许将输入作为字符串接受并将其拆分为数组,因此如果答案基于此代码,则最好。)
int main(int argc, const char * argv[])
{
@autoreleasepool {
float abNumber;
int i = 0;
float number;
float result;
float firstNumber;
printf("type a number: ");
scanf("%f", &firstNumber);
abNumber = abs(firstNumber);
if (firstNumber < 0) {
printf("negative ");
}
number = abNumber;
while (number >= 10) {
number = number / 10;
i++;
}
do {
float countNumber = abNumber;
float power = powf(10, -i);
float powerNo2 = powf(10, i);
countNumber = countNumber * power;
result = floorf(countNumber);
if (result == 9){
printf("nine ");
}
if (result == 8){
printf("eight ");
}
if (result == 7){
printf("seven ");
}
if (result == 6){
printf("six ");
}
if (result == 5){
printf("five ");
}
if (result == 4){
printf("four ");
}
if (result == 3){
printf("three ");
}
if (result == 2){
printf("two ");
}
if (result == 1){
printf("one ");
}
if (result == 0){
printf("zero ");
}
while (abNumber > powerNo2) {
abNumber = abNumber - powerNo2;
}
i--;
} while (i >= 0);
}
return 0;
}
答案 0 :(得分:1)
主要错误似乎是
while (abNumber > powerNo2) {
应该是
while (abNumber >= powerNo2) {
但我建议不要使用浮点运算,以避免 可能的舍入错误。使用简单的整数运算可以实现相同的目的 (为简单起见,我省略了“否定案例”):
int number;
printf("type a number: ");
scanf("%d", &number);
// Determine highest power of 10 that is <= the given number:
int power = 1;
while (10 * power <= number) {
power *= 10;
}
// Extract each digit:
while (power > 0) {
int digit = (number / power) % 10;
/*
* Use switch/case to print 'digit' as a string ...
*/
power /= 10;
}
答案 1 :(得分:1)
我会选择递归解决方案,就像那样
int print(int num)
{
if( num )
{
int mod = num%10;
print(num/10);
switch(mod)
{
case 0:printf(" zero");break;
case 1:printf(" one");break;
case 2:printf(" two");break;
case 3:printf(" three");break;
}
}
return 0;
}
Recursivy将数字除以它后面的任何内容,在返回打印模式的路上。
答案 2 :(得分:0)
请考虑以下事项:
<强>代码强>
#include <stdio.h>
#include <stdlib.h>
const char *numbers[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
void printNum(int num);
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%u", &num);
printNum(num);
printf("\n");
return 0;
}
void printNum(int num)
{
int absNum = abs(num);
if(absNum > 9)
printNum(num / 10);
if((absNum < 10) && (num < 0))
printf("negative");
printf(" %s", numbers[absNum % 10]);
}
示例输出
Enter a number: 2582 two five eight two Enter a number: -943 negative nine four three Enter a number: 1000 one zero zero zero Enter a number: -1000 negative one zero zero zero
<强>逻辑强>
待办事项
答案 3 :(得分:0)
为什么不直接输入数字作为字符串然后遍历每个字符:
输入:
-12003200
输出:
negative one two zero zero tree two zero zero
代码:
#include <stdio.h>
int main(int argc, char *argv[])
{
char input[25];
scanf("%s", input);
int i = 0;
while (input[i] != '\0') {
switch(input[i]) {
case '-' :
printf("negative");
break;
case '0' :
printf("zero");
break;
case '1' :
printf("one");
break;
case '2' :
printf("two");
break;
case '3' :
printf("tree");
break;
case '4' :
printf("four");
break;
case '5' :
printf("five");
break;
case '6' :
printf("six");
break;
case '7' :
printf("seven");
break;
case '8' :
printf("eight");
break;
case '9' :
printf("nine");
break;
default :
break;
}
printf(" ");
i++;
}
return 0;
}