MathLib: introduce and use calculate() method.

No functional change.
This commit is contained in:
Slava Semushin 2009-08-02 19:22:39 +07:00
parent e5372de295
commit 35e35b38a7
3 changed files with 36 additions and 15 deletions

View File

@ -101,6 +101,39 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
return toString<double>(toDoubleNumber(first) * toDoubleNumber(second));
}
std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
{
std::string result("0");
switch (action)
{
case '+':
result = MathLib::add(first, second);
break;
case '-':
result = MathLib::subtract(first, second);
break;
case '*':
result = MathLib::multiply(first, second);
break;
case '/':
result = MathLib::divide(first, second);
break;
default:
std::cout << "##### If you see this, there is a bug: "
<< "MathLib::calculate() was called with unknown action '"
<< action
<< "' #####"
<< std::endl;
break;
}
return result;
}
std::string MathLib::sin(const std::string &tok)
{

View File

@ -41,6 +41,8 @@ public:
static std::string subtract(const std::string & first, const std::string & second);
static std::string multiply(const std::string & first, const std::string & second);
static std::string divide(const std::string & first, const std::string & second);
static std::string calculate(const std::string & first, const std::string & second, char action);
static std::string sin(const std::string & tok);
static std::string cos(const std::string & tok);
static std::string tan(const std::string & tok);

View File

@ -2946,21 +2946,7 @@ bool Tokenizer::simplifyCalculations()
if (Token::simpleMatch(tok->next(), "/ 0"))
continue;
switch (*(tok->strAt(1)))
{
case '+':
tok->str(MathLib::add(tok->str(), tok->strAt(2)));
break;
case '-':
tok->str(MathLib::subtract(tok->str(), tok->strAt(2)));
break;
case '*':
tok->str(MathLib::multiply(tok->str(), tok->strAt(2)));
break;
case '/':
tok->str(MathLib::divide(tok->str(), tok->strAt(2)));
break;
}
tok->str(MathLib::calculate(tok->str(), tok->tokAt(2)->str(), *(tok->strAt(1))));
tok->deleteNext();
tok->deleteNext();