在英文整数上显示每个数字的程序不适用于以“0”开头的整数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在英文整数上显示每个数字的程序不适用于以“0”开头的整数相关的知识,希望对你有一定的参考价值。
我有一个任务,我必须写一个程序,从终端键入一个整数,并提取并显示整数的英文数字。我无法使用数组或递归,我们只是从编程开始。
例如:“123”返回“一二三”
我的程序运行良好(大部分),但问题是当你在终端输入类似“0123”的东西时,程序返回“八三”...... WTH ??
这是我的代码:
// Program that takes an integer and displays each digit in English
#include <stdio.h>
int main (void)
{
int num, digit;
int reversed = 0, backupZero = 0;
printf("Please enter an integer:\n");
scanf("%i", &num);
if (num == 0) // In case the input is just "0"
{
printf("zero");
}
while (num > 0) // Loop to reverse the integer
{
digit = num % 10;
reversed = (reversed * 10) + digit;
if ((reversed == 0) && (digit == 0)) // If the integer finishes in zero
{
++backupZero; // Use this to add extra zeroes later
}
num /= 10;
}
while (reversed > 0)
{
digit = reversed % 10;
reversed /= 10;
switch (digit)
{
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
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:
printf("zero ");
break;
}
}
for (int counter = 0; counter < backupZero; ++counter) // Prints the extra zeroes at the end
{
printf("zero ");
--backupZero;
}
printf("\n");
return 0;
}
可能是数学上的东西,我承认我不擅长它。
答案
当你读到数字时
scanf("%i", &num);
你让scanf
推断数字的基数。以0
开头的数字后跟其他数字被解释为八进制。所以0123
和123
不一样。事实上,它是83
。
0100 = 64
020 = 16
03 = 3
---------
0123 = 83
要将数字读作基数10,请使用
scanf("%d", &num);
另一答案
如果你想处理以'0'
开头的数字,那么我建议你将用户输入读作字符串(字符数组)而不是整数。
除此之外,您可以使用一个简单的数组,而不是在每个字符上“进行切换”,以便将正确的单词映射到每个数字。
以下是实现它的一种方法:
#include <stdio.h>
#define MAX_INPUT_LEN 100
const char* digits[] = {"zero","one","two" ,"three","four",
"five","six","seven","eight","nine"};
int main()
{
int i;
char format[10];
char str[MAX_INPUT_LEN+1];
sprintf(format,"%c%us",'%',MAX_INPUT_LEN); // now format = "%100s"
scanf(format,str); // will write into str at most 100 characters
for (i=0; str[i]!=0; i++)
{
if ('0' <= str[i] && str[i] <= '9')
printf("%s ",digits[str[i]-'0']);
else
printf("invalid character ");
}
return 0;
}
另一答案
哦,哇我花了3到4个小时写下面的代码。我只在第一周进入c,所以请体谅。更新:添加工作减去+一些评论。
#include <stdio.h>
#include <math.h>
int main(void)
{
int num, count, user, out;
count = 0;
printf("Type in any int: ");
scanf("%d", &num);
// adding minus to the beginning if int is negative
if (num < 0)
{
num = -num;
printf("minus ");
}
user = num;
// creating a power to the future number
while (num != 0)
{
num = num / 10;
count++;
}
int i2;
i2 = count;
// main calculations: dividing by (10 to the power of counter) and subtracting from the initial number
for (int i = 0; i < i2; i++)
{
out = user / pow(10, count - 1);
user = user - out * pow(10, count - 1);
count--;
switch (out)
{
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
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;
case 0:
printf("zero ");
break;
default:
break;
}
}
printf("\n");
return 0;
}
另一答案
有一些错误:
if ((reversed == 0) && (digit == 0)) (incorrect)
if ((reversed == 0) || (digit == 0)) (correct)
在最后一个循环中你应该删除
--backupZero;
代码会更好地读取数字
以上是关于在英文整数上显示每个数字的程序不适用于以“0”开头的整数的主要内容,如果未能解决你的问题,请参考以下文章