gpt4 book ai didi

C - 尝试将输入的未指定数量的数字转换为各自的单词

转载 作者:行者123 更新时间:2023-11-30 15:43:38 29 4
gpt4 key购买 nike

我知道这有点类似于使用“千”、“百”等将数字转换为单词的程序。但是,我希望采用任何大小的整数(例如 543210)并创建输出“五四三二一零”。我正在使用一个我认为我完全理解并且可以工作的 switch 语句。我坚持使用某种循环来挑出整数的每个数字并打印它的单词,然后重复下一个数字,然后再重复下一个数字。我对这一切都很陌生,所以任何帮助将不胜感激!谢谢。

这是我到目前为止所拥有的(不多,但我被困在循环的位置上):

#include <stdio.h>

int main(void)
{
int num;

printf("Please enter an integer: \n");
scanf("%d", & num);

while (num==0)
{
switch (num)
{
case (0):
printf("Zero ");
break;
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 ");
case (8):
printf("Eight ");
break;
case (9):
printf("Nine ");
break;
case (10):
printf("Ten ");
break;

}
}
return 0;

}

最佳答案

因此您可以尝试使用 fgets 读取字符串并循环直到字符串末尾(或者像我的示例代码中那样,直到数字末尾)并打印数字名称。

#include <stdio.h>

char *names[10] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", };
int main()
{
char buffer[500] = {0};
int i = 0;
fgets(buffer, sizeof(buffer), stdin);
while (isdigit(buffer[i]))
printf("%s ", names[buffer[i++] - '0']);

return 0;
}

http://ideone.com/Ba7Rs3

关于C - 尝试将输入的未指定数量的数字转换为各自的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19823702/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com