编写一个程序,要求用户输入一个两位数,然后输出显示该数的英文单词。提示:把数分解为两个数字。用一个switch语句显示第一位数字对应的单词(“twenty”、“thirty”等),用第二个switch
时间: 2023-05-31 15:18:21 浏览: 392
### 回答1:
语句显示第二位数字对应的单词(“one”、“two”等),最后将两个单词拼接起来即可。
以下是示例代码:
```
#include <stdio.h>
int main() {
int num;
printf("请输入一个两位数:");
scanf("%d", &num);
int ten = num / 10; // 十位数字
int digit = num % 10; // 个位数字
char* ten_word; // 十位数字对应的单词
switch (ten) {
case 2:
ten_word = "twenty";
break;
case 3:
ten_word = "thirty";
break;
case 4:
ten_word = "forty";
break;
case 5:
ten_word = "fifty";
break;
case 6:
ten_word = "sixty";
break;
case 7:
ten_word = "seventy";
break;
case 8:
ten_word = "eighty";
break;
case 9:
ten_word = "ninety";
break;
default:
printf("输入有误!\n");
return 0;
}
char* digit_word; // 个位数字对应的单词
switch (digit) {
case 0:
digit_word = "";
break;
case 1:
digit_word = "one";
break;
case 2:
digit_word = "two";
break;
case 3:
digit_word = "three";
break;
case 4:
digit_word = "four";
break;
case 5:
digit_word = "five";
break;
case 6:
digit_word = "six";
break;
case 7:
digit_word = "seven";
break;
case 8:
digit_word = "eight";
break;
case 9:
digit_word = "nine";
break;
default:
printf("输入有误!\n");
return 0;
}
if (ten == 1) { // 如果十位数字是1,需要特殊处理
switch (digit) {
case 0:
printf("ten\n");
break;
case 1:
printf("eleven\n");
break;
case 2:
printf("twelve\n");
break;
case 3:
printf("thirteen\n");
break;
case 4:
printf("fourteen\n");
break;
case 5:
printf("fifteen\n");
break;
case 6:
printf("sixteen\n");
break;
case 7:
printf("seventeen\n");
break;
case 8:
printf("eighteen\n");
break;
case 9:
printf("nineteen\n");
break;
}
} else { // 否则直接拼接两个单词
printf("%s%s\n", ten_word, digit_word);
}
return 0;
}
```
### 回答2:
编写一个程序,要求用户输入一个两位数,并输出显示该数的英文单词。
首先,我们需要把输入的两位数分解为十位和个位数字。可以使用以下代码实现:
```cpp
int num; // 定义输入的两位数
cout << "请输入一个两位数:" << endl;
cin >> num;
int ten = num / 10; // 十位数字
int one = num % 10; // 个位数字
```
然后,我们可以使用一个switch语句来根据输入的十位数字输出对应的英文单词,如下所示:
```cpp
switch (ten) {
case 2:
cout << "twenty";
break;
case 3:
cout << "thirty";
break;
case 4:
cout << "forty";
break;
case 5:
cout << "fifty";
break;
case 6:
cout << "sixty";
break;
case 7:
cout << "seventy";
break;
case 8:
cout << "eighty";
break;
case 9:
cout << "ninety";
break;
default:
break;
}
```
接着,我们可以再使用一个switch语句根据个位数字输出对应的英文单词,如下所示:
```cpp
switch (one) {
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
case 5:
cout << "five";
break;
case 6:
cout << "six";
break;
case 7:
cout << "seven";
break;
case 8:
cout << "eight";
break;
case 9:
cout << "nine";
break;
default:
break;
}
```
最后,我们需要注意特殊情况,例如输入的数为10、11、12、13、14、15、16、17、18、19时,对应的英文单词不同。
完整的代码如下所示:
```cpp
#include <iostream>
using namespace std;
int main() {
int num; // 定义输入的两位数
cout << "请输入一个两位数:" << endl;
cin >> num;
int ten = num / 10; // 十位数字
int one = num % 10; // 个位数字
switch (ten) {
case 1:
switch (one) {
case 0:
cout << "ten";
break;
case 1:
cout << "eleven";
break;
case 2:
cout << "twelve";
break;
case 3:
cout << "thirteen";
break;
case 4:
cout << "fourteen";
break;
case 5:
cout << "fifteen";
break;
case 6:
cout << "sixteen";
break;
case 7:
cout << "seventeen";
break;
case 8:
cout << "eighteen";
break;
case 9:
cout << "nineteen";
break;
default:
break;
}
break;
case 2:
cout << "twenty";
break;
case 3:
cout << "thirty";
break;
case 4:
cout << "forty";
break;
case 5:
cout << "fifty";
break;
case 6:
cout << "sixty";
break;
case 7:
cout << "seventy";
break;
case 8:
cout << "eighty";
break;
case 9:
cout << "ninety";
break;
default:
break;
}
if (ten != 1) {
switch (one) {
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
case 5:
cout << "five";
break;
case 6:
cout << "six";
break;
case 7:
cout << "seven";
break;
case 8:
cout << "eight";
break;
case 9:
cout << "nine";
break;
default:
break;
}
}
cout << endl;
return 0;
}
```
### 回答3:
编写一个程序实现将输入的两位数转换成英文单词的功能,可以采用如下的思路:
首先,需要使用C语言中的scanf()函数让用户从键盘输入一个两位数。这可以通过以下语句实现:
```
int num;
scanf("%d", &num);
```
接着,我们可以使用除法和取模运算将两位数num分解成两个数字digit1和digit2,分别表示十位和个位。代码如下:
```
int digit1 = num / 10;
int digit2 = num % 10;
```
然后,我们需要使用switch语句将digit1对应的数字转换成英文单词。代码如下:
```
char* str1;
switch (digit1) {
case 2:
str1 = "twenty";
break;
case 3:
str1 = "thirty";
break;
// ...
default:
str1 = "";
}
```
最后,我们需要使用switch语句将digit2对应的数字转换成英文单词。代码如下:
```
char* str2;
switch (digit2) {
case 0:
str2 = "zero";
break;
case 1:
str2 = "one";
break;
// ...
default:
str2 = "";
}
```
最后,我们可以将str1和str2连接起来,形成最终的英文单词。代码如下:
```
char result[20];
if (str1[0] == '\0') {
strcpy(result, str2);
} else if (str2[0] == '\0') {
strcpy(result, str1);
} else {
sprintf(result, "%s-%s", str1, str2);
}
printf("%s\n", result);
```
以上便是一个将输入的两位数转换成英文单词的程序的基本实现方式,注意需按照英文单词的拼写格式进行输出。
阅读全文