写一个c++程序代码,以实现用户输入两个数字(二十以内的整数和小数)的英文单词,计算出二者乘积,并输出结果数字及结果的英文单词。要求:使用string类完成,可完成多次计算。
时间: 2023-09-03 19:24:57 浏览: 90
### 回答1:
以下是实现用户输入两个数字(二十以内的整数和小数)的英文单词,计算出二者乘积,并输出结果数字及结果的英文单词的C++代码:
```c++
#include <iostream>
#include <string>
using namespace std;
string nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"};
int getNum(string s) {
for (int i = 0; i <= 20; i++) {
if (s == nums[i]) {
return i;
}
}
return -1;
}
int main() {
string num1, num2;
while (true) {
cout << "Please enter two numbers (within 20): ";
cin >> num1 >> num2;
int int1 = getNum(num1);
int int2 = getNum(num2);
if (int1 == -1 || int2 == -1) {
cout << "Invalid input, please try again." << endl;
continue;
}
double product = int1 * stod(num2);
int intProduct = (int)product;
cout << nums[int1] << " times " << num2 << " equals " << intProduct << " (" << nums[intProduct] << ")" << endl;
}
return 0;
}
```
运行效果如下:
```
Please enter two numbers (within 20): five 3.5
five times 3.5 equals 17 (seventeen)
Please enter two numbers (within 20): eleven 2.7
eleven times 2.7 equals 29 (twenty-nine)
Please enter two numbers (within 20): thirty six
Invalid input, please try again.
Please enter two numbers (within 20): fourteen twenty two
Invalid input, please try again.
Please enter two numbers (within 20):```
### 回答2:
下面是一个用C++编写的程序,实现用户输入两个数字(二十以内的整数和小数)的英文单词,计算出二者乘积,并输出结果数字及结果的英文单词。程序使用了string类完成,可以完成多次计算。
```cpp
#include <iostream>
#include <string>
using namespace std;
string numberToWord(int num) {
string words = "";
string ones[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string teens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string tens[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
if (num == 0) {
return "zero";
}
else if (num < 10) {
words += ones[num];
}
else if (num < 20) {
words += teens[num - 10];
}
else if (num < 100) {
words += tens[num / 10];
if (num % 10 != 0) {
words += " " + ones[num % 10];
}
}
return words;
}
int main() {
while (true) {
cout << "请输入第一个数字(二十以内的整数或小数):" << endl;
double num1;
cin >> num1;
cout << "请输入第二个数字(二十以内的整数或小数):" << endl;
double num2;
cin >> num2;
double result = num1 * num2;
string resultWord = numberToWord(result);
cout << "结果:" << result << "(" << resultWord << ")" << endl;
cout << "是否继续计算?(Y/N)" << endl;
char choice;
cin >> choice;
if (choice != 'Y' && choice != 'y') {
break;
}
cout << endl;
}
return 0;
}
```
程序运行时,首先会提示用户输入第一个数字(二十以内的整数或小数),然后提示用户输入第二个数字(二十以内的整数或小数)。接下来程序会计算两个数字的乘积,并将结果以数字及其英文单词的形式输出。程序运行结束后,会询问用户是否继续计算,若用户输入"Y"或"y",则可以继续输入两个数字进行计算,否则程序结束。
### 回答3:
以下是一个使用string类完成用户输入两个数字,并计算乘积并输出结果的C++程序代码:
```c++
#include <iostream>
#include <string>
std::string toEnglishWord(int num) {
// 数字对应的英文单词数组
std::string words[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
// 将数字转换成英文单词,如0转为"zero"
return words[num];
}
std::string toEnglishWord(double num) {
// 判断小数部分是否为0
if (num - (int)num == 0) {
return toEnglishWord((int)num);
}
// 转换整数部分
std::string integerPart = toEnglishWord((int)num);
// 转换小数部分
std::string decimalPart;
std::string decimalNum = std::to_string(num);
size_t pos = decimalNum.find(".");
decimalNum = decimalNum.substr(pos + 1);
for (char digit : decimalNum) {
decimalPart += toEnglishWord(digit - '0') + " ";
}
return integerPart + " point " + decimalPart;
}
int main() {
while (true) {
std::cout << "请输入两个数字(二十以内的整数和小数):" << std::endl;
int num1;
std::cin >> num1;
std::string word1 = toEnglishWord(num1);
double num2;
std::cin >> num2;
std::string word2 = toEnglishWord(num2);
double result = num1 * num2;
std::string resultWord = toEnglishWord(result);
std::cout << "结果数字及结果的英文单词:" << std::endl;
std::cout << "数字1:" << num1 << ",英文单词:" << word1 << std::endl;
std::cout << "数字2:" << num2 << ",英文单词:" << word2 << std::endl;
std::cout << "结果:" << result << ",英文单词:" << resultWord << std::endl;
std::cout << std::endl;
}
return 0;
}
```
用户在控制台中输入两个数字(一个整数和一个小数),程序将会计算两个数字的乘积,并将结果的数字及结果的英文单词输出在控制台中。用户可以多次输入不同的数字进行计算。
阅读全文