感谢Joachim,但我无法进行编译,我认为这可能是我设置它的方式,所以我像你说的那样删除了for循环,并将函数合并到我的calculator.cpp文件中。 所以当我编译程序时,会导致以下错误:
错误:`ParseNumberWord'未声明(首先使用此函数)
Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator {
// float a, b;
public:
int add(int, int);
int subtract(int, int);
int multiply(int, int);
int divide(int, int);
int ParseNumberDigit();
int ParseNumberWord();
private:
int n1;
int n2;
};
#endif /* CALCULATOR_H */
Calculator.cpp
#include "Calculator.h"
int Calculator::add(int n1, int n2) {
return (n1 + n2);
}
int Calculator::subtract(int n1, int n2) {
return (n1 - n2);
}
int Calculator::divide(int n1, int n2) {
return (n1 / n2);
}
int Calculator::multiply(int n1, int n2) {
return (n1 * n2);
}
int Calculator::ParseNumberDigit(std::string &number)
{
int n = 0;
for (int i = 9; i >= 0 && !number.empty(); i--) {
if (number.find(Hund[i]) == 0) {
n = i * 100;
number = number.substr(Hund[i].length());
}
else if (number.find(Ten[i]) == 0) {
n = i * 10;
number = number.substr(Ten[i].length());
}
else if (number.find(Teen[i]) == 0) {
n = i + 10;
number = number.substr(Teen[i].length());
}
else if (number.find(One[i]) == 0) {
n = i;
number = number.substr(One[i].length());
}
if (n != 0)
break;
}
return n;
}
int Calculator::ParseNumberWord(const std::string &word)
{
std::string number = word;
std::transform(number.begin(), number.end(), number.begin(), [](char c) { return std::tolower(c); });
// If the above line doesn't work, you have to do it the old way:
// for (int x = 0; x < number.length(); x++) { //input to lower case
// number[x] = std::tolower(number[x]);
// }
int n = 0;
while (!number.empty()) {
// Parse the next "digit"
n += ParseNumberDigit(number);
// The '_' is used to bind together digits
if (number[0] == '_') {
number = number.substr(1);
}
}
return n;
}
main.cpp
#include <iostream>
#include <sstream>
#include "Calculator.h"
#include <algorithm>
using namespace std;
string Calualtor();
int main() {
int length, result, OnesR, TensR;
Calculator calc;
string word1a, word2a;
string word1, word2;
// int result, OnesR, TensR;
char arithmetic;
std::string One[10] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
std::string Teen[10] = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
std::string Ten[10] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
std::string Hund[10] = {"", "one_hundred", "two_hundred", "three_hundred",
"four_hundred", "five_hundred", "six_hundred", "seven_hundred", "eight_hundred",
"nine_hundred"};
while (cin >> word1 >> arithmetic >> word2) {
int n1 = ParseNumberWord(word1);
int n2 = ParseNumberWord(word2);
switch (arithmetic) { //determines which arithmetic operation to perform
case '+':
result = calc.add(n1, n2);
break;
case '-':
result = calc.subtract(n1, n2);
break;
case '*':
result = calc.multiply(n1, n2);
break;
case '/':
result = calc.divide(n1, n2);
break;
default:
cout << 0 << endl;
}
// for the teen array
if (result <= 19 && result >= 11 || result <= 119 && result > 111) {
result = result % 10;
cout << Teen[result] << endl;
} else {
for (int i = 1; i <= 2; i++) //Save individual digits to individual variables.
{
switch (i) {
case 1:
OnesR = result % 10;
result = result / 10;
break;
case 2:
TensR = result % 10;
result = result / 10;
break;
}
}
if ((OnesR || TensR) < 0) { //To print a negative result
OnesR = OnesR * -1;
TensR = TensR * -1;
cout << "negative " + Ten[TensR] << One[OnesR] << endl;
} else {
cout << Ten[TensR] << One[OnesR] << endl;
}
}
}
}
答案 0 :(得分:0)
首先是一个小提示:用于获取第一个和第二个数字的代码是相同的,将此代码转换为函数是个好主意。
至于你的问题,我看到其中有两个:第一个是无论你在循环中匹配什么“数字”字,你总是从One
数组中删除,即这个行:
word1.erase(0, One[i].length());
应位于相应的if
- 体内,并且也应使用正确的数组。
第二个问题是,在删除当前的数字后,实际上并没有检查到应该将数字绑定在一起的'_'
字符。
修改强>
您需要做的第一件事是使数字字数组全局化。然后只需创建一个带字符串的函数,并返回一个数字:
#include <algorithm>
std::string One[10] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
std::string Teen[10] = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"};
std::string Ten[10] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eighty", "ninety"};
std::string Hund[10] = {"", "one_hundred", "two_hundred", "three_hundred",
"four_hundred", "five_hundred", "six_hundred", "seven_hundred", "eight_hundred",
"nine_hundred"};
int ParseNumberDigit(std::string &number)
{
int n = 0;
for (int i = 9; i >= 0 && !number.empty(); i--) {
if (number.find(Hund[i]) == 0) {
n = i * 100;
number = number.substr(Hund[i].length());
}
else if (number.find(Ten[i]) == 0) {
n = i * 10;
number = number.substr(Ten[i].length());
}
else if (number.find(Teen[i]) == 0) {
n = i + 10;
number = number.substr(Teen[i].length());
}
else if (number.find(One[i]) == 0) {
n = i;
number = number.substr(One[i].length());
}
if (n != 0)
break;
}
return n;
}
int ParseNumberWord(const std::string &word)
{
std::string number = word;
std::transform(number.begin(), number.end(), number.begin(), [](char c) { return std::tolower(c); });
// If the above line doesn't work, you have to do it the old way:
// for (int x = 0; x < number.length(); x++) { //input to lower case
// number[x] = std::tolower(number[x]);
// }
int n = 0;
while (!number.empty()) {
// Parse the next "digit"
n += ParseNumberDigit(number);
// The '_' is used to bind together digits
if (number[0] == '_') {
number = number.substr(1);
}
}
return n;
}
在获得号码的main
中,而不是for
- 循环,只需使用:
int n1 = ParseNumberWord(word1);
int n2 = ParseNumberWord(word2);
注意:上述功能经过测试可用。