Exemple #1
0
int CSVExpression::calculateDouble(const string &szExpr, double &dResult)
{
    int nRet = eval_ok;
    string szRpn("");
    if((nRet = toRPN(szExpr, szRpn)) == eval_ok)
    {
        nRet = evaluateRPN(szRpn, dResult);
    }
    return nRet;
}
Exemple #2
0
 vector<string> basicCalculatorIV(string expression, vector<string>& evalvars, vector<int>& evalints) {
     unordered_map<string, string> m;
     for (int i = 0; i < evalvars.size(); ++i) {
         m[evalvars[i]] = to_string(evalints[i]);
     }
     auto tokens = tokenize(expression);
     tokens = inject(tokens, m);
     tokens = toRPN(tokens);
     tokens = evaluate(tokens);
     return tokens;
 }
double calculator::calculate(const char* expr,
    std::map<std::string, double>* vars) {

  // Convert to RPN with Dijkstra's Shunting-yard algorithm.
  TokenQueue_t rpn = toRPN(expr, vars);

  double ret = calculate(rpn);

  cleanRPN(rpn);

  return ret;
}
double calculator::calculate(const char* expr,
    std::map<std::string, double>* vars) {
  // 1. Create the operator precedence map.
  std::map<std::string, int> opPrecedence;
  opPrecedence["("] = -1;
  opPrecedence["<<"] = 1; opPrecedence[">>"] = 1;
  opPrecedence["+"]  = 2; opPrecedence["-"]  = 2;
  opPrecedence["*"]  = 3; opPrecedence["/"]  = 3;
  opPrecedence["^"] = 4;

  // 2. Convert to RPN with Dijkstra's Shunting-yard algorithm.
  TokenQueue_t rpn = toRPN(expr, vars, opPrecedence);

  // 3. Evaluate the expression in RPN form.
  std::stack<double> evaluation;
  while (!rpn.empty()) {
    TokenBase* base = rpn.front();
    rpn.pop();

    Token<std::string>* strTok = dynamic_cast<Token<std::string>*>(base);
    Token<double>* doubleTok = dynamic_cast<Token<double>*>(base);
    if (strTok) {
      std::string str = strTok->val;
      if (evaluation.size() < 2) {
        throw std::domain_error("Invalid equation.");
      }
      double right = evaluation.top(); evaluation.pop();
      double left  = evaluation.top(); evaluation.pop();
      if (!str.compare("+")) {
        evaluation.push(left + right);
      } else if (!str.compare("*")) {
        evaluation.push(left * right);
      } else if (!str.compare("-")) {
        evaluation.push(left - right);
      } else if (!str.compare("/")) {
        evaluation.push(left / right);
      } else if (!str.compare("<<")) {
        evaluation.push((int) left << (int) right);
      } else if (!str.compare("^")) {
        evaluation.push(pow(left, right));
      } else if (!str.compare(">>")) {
        evaluation.push((int) left >> (int) right);
      } else {
        throw std::domain_error("Unknown operator: '" + str + "'.");
      }
    } else if (doubleTok) {
Exemple #5
0
Fichier : cal.c Projet : Jmq14/XV6
int cal(char* src)
{
  char* res = caln(toRPN(split(src)));
  printf(2, "The answer is: %s\n", res);
  return chars_to_int(res);
}
ResultSet BoolQuery::query(string queryStr) {
	Expression infex = split(queryStr, ' ');
	Expression rpn = toRPN(infex);
	ResultSet result = calcRPN(rpn);
	return result;
}