c++算术字数转换,求指教
Posted
技术标签:
【中文标题】c++算术字数转换,求指教【英文标题】:c++ Arithmetic word number conversion, please need advice 【发布时间】:2012-04-18 02:01:45 【问题描述】:感谢 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;
【问题讨论】:
使用下划线是不自然的。为什么不留空间? 【参考方案1】:首先一个小提示:获取第一个和第二个数字的代码是一样的,最好把这段代码做成一个函数。
至于你的问题,我看到了其中两个:第一个是无论你在循环中匹配什么“数字”字,你总是从 One
数组中删除,即这一行:
word1.erase(0, One[i].length());
应该在相应的if
-body 内,并且使用正确的数组。
第二个问题是,在删除当前数字字之后,您实际上并没有检查应该将数字绑定在一起的 '_'
字符。
编辑:
您需要做的第一件事是使数字字数组全局化。然后只需创建一个接受字符串并返回数字的函数:
#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
-loop 只需使用:
int n1 = ParseNumberWord(word1);
int n2 = ParseNumberWord(word2);
注意:以上功能均经过测试。
【讨论】:
这可能是一个愚蠢的问题,但我将如何将一个函数用于两个不同的输入 @user1340113 为您创建了所需的功能。 仍然报同样的错误error: `ParseNumberWord' undeclared (先用这个函数) @user1340113 函数必须在调用之前放置,即在源文件中的main
之前。以上是关于c++算术字数转换,求指教的主要内容,如果未能解决你的问题,请参考以下文章
为啥在 C 和 C++ 中的算术运算之前必须将 short 转换为 int?