bool Expression::Compute(mycomplex& com) { if(CheckParenthesis(m_strExp) == false) { m_strError = "括号不匹配"; return false; } if(CheckOperatorPos(m_strExp) == false) { m_strError = "操作符不对应"; return false; } if(CheckDotPos(m_strExp) == false) { m_strError = "小数点没有填写对"; return false; } m_strScanDouble = ConvertDouble(m_strExp); if(m_strScanDouble == "error") { m_strError = "数字输入错误"; return false; } m_strScanComplex = ConvertComplex(m_strScanDouble); if(CheckImageFlag(m_strScanComplex) == false) { m_strError = "输入复数格式错误"; return false; } m_strInfix = ConvertDoubleToComplex(m_strScanComplex); m_vecInfix = ConvertStringToInfix(m_strInfix); m_vecPostfix = ConvertInfixToPostfix(m_vecInfix); string str(""); com = Compute2(m_vecPostfix, str); if(str == "error") { m_strError = "输入表达式错误"; return false; } return true; }
long double Calculation::Resolve(const string& str) const throw(invalid_argument) { if (!CheckParenthesis(str)) throw invalid_argument("The number of left parenthesis is not equal with" " the number of right parenthesis"); /* * TODO * To test special cases. * 5 + * 2 * 5 + 2 * * * 5 + 2 * * 5 (2 + 3) * (2 + 3) 5 * * 5 2 * 3 * 5 sin(90) * * sin(90) cos(90) * sin 90 cos(45) * * To test if there are the functions that are called. */ Lexer l; list<Token*> tokens = l.Split(str); long double r; if (l.PrintErrors()) { throw invalid_argument("Invalid input"); } if (tokens.size() == 0) { throw invalid_argument("No tokens"); } AddZero(tokens); try { r = Resolve(tokens); for (auto it = tokens.begin(); it != tokens.end(); ++it) delete (*it); return r; } catch (invalid_argument& e) { for (auto it = tokens.begin(); it != tokens.end(); ++it) delete (*it); throw e; } }