c++,string,用户输入两个数字(0到20的实数)的英文单词,计算出两数字的乘积,并输出其英文单词
时间: 2023-06-19 11:06:02 浏览: 222
以下是一个示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string num1, num2;
string ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string tens[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
cout << "Enter the first number (between 0 and 20): ";
cin >> num1;
cout << "Enter the second number (between 0 and 20): ";
cin >> num2;
int n1 = 0, n2 = 0;
for (int i = 0; i < 20; i++) {
if (num1 == ones[i]) {
n1 = i;
break;
}
}
for (int i = 2; i < 10; i++) {
if (num1 == tens[i]) {
n1 = i * 10;
break;
}
}
for (int i = 0; i < 20; i++) {
if (num2 == ones[i]) {
n2 = i;
break;
}
}
for (int i = 2; i < 10; i++) {
if (num2 == tens[i]) {
n2 = i * 10;
break;
}
}
int result = n1 * n2;
string result_str;
if (result < 20) {
result_str = ones[result];
} else {
result_str = tens[result / 10];
if (result % 10 != 0) {
result_str += "-" + ones[result % 10];
}
}
cout << "The product is: " << result_str << endl;
return 0;
}
```
该代码将用户输入的两个数字转换为整数,计算它们的乘积,然后将结果转换为英文单词。在转换过程中,我们使用了两个字符串数组,一个用于处理个位数,一个用于处理十位数。
阅读全文