编写一个程序,要求用户输入一个两位数,然后输出显示该数的英文单词。提示:把数分解为两个数字。用一个switch语句显示第一位数字对应的单词(“twenty”、“thirty”等),用第二个switch语句显示第二位数字对应的单词。不要忘记11~19需要特殊处理。用c语言
时间: 2023-05-23 10:07:12 浏览: 491
C语言入门习题:水仙花数
5星 · 资源好评率100%
实现如下:
```
#include <stdio.h>
int main() {
int num, first, second;
const char *digits[] = {"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
const char *tens[] = {"", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
printf("Please input a two-digit number:\n");
scanf("%d", &num);
if (num < 10 || num > 99) { // 判断输入是否为两位数
printf("Invalid input!\n");
return 1;
}
first = num / 10; // 获取十位数字
second = num % 10; // 获取个位数字
printf("The English word for %d is: ", num);
switch (first) {
case 1:
printf("%s\n", digits[num]);
return 0;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
printf("%s ", tens[first]);
break;
}
switch (second) {
case 0:
if (first == 0) {
printf("%s\n", digits[second]);
}
break;
case 1:
printf("%s\n", digits[10 + second]);
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
printf("%s\n", digits[second]);
break;
}
return 0;
}
```
样例输入:
```
55
```
样例输出:
```
The English word for 55 is: fifty five
```
阅读全文