Joachimに感謝しますが、コンパイルできません。設定方法に問題があるのではないかと思うので、あなたが言ったようにforループを削除し、関数をcalculator.cppファイルに組み込みました。そのため、プログラムをコンパイルすると、次のエラーが発生します。
エラー: `ParseNumberWord' が宣言されていません (最初にこの関数を使用してください)
電卓.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 */
電卓.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;
}
}
}
}