public double getResult() { Operation.Operation op = new Operation.Operation(); double a; double b; double ans = 0; List <string> opList = new List <string>(new string[] { "+", "-", "*", "/" }); if (!opList.Contains(tokens[1])) { valid = false; } if (!Double.TryParse(tokens[0], out a)) { valid = false; } if (!Double.TryParse(tokens[2], out b)) { valid = false; } switch (tokens[1]) { case "+": ans = op.plus(a, b); break; case "-": ans = op.minus(a, b); break; case "*": ans = op.time(a, b); break; case "/": if (b != 0) { ans = op.divide(a, b); } else { valid = false; } break; default: valid = false; break; } return(ans); }
public double getResult() { Operation.Operation op = new Operation.Operation(); double a; double b; a = 0; b = 0; while (tokens.Count() > 1) { double current; while (tokens.Contains("*") || tokens.Contains("/")) { for (int i = 0; i < tokens.Count(); i++) { if (tokens[i] == "*" && i >= 1) { if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b)) { current = op.time(a, b); tokens[i - 1] = Convert.ToString(current); //Delete both at i and i+1 tokens.RemoveAt(i); tokens.RemoveAt(i); break; } } if (tokens[i] == "/" && i >= 1) { if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b)) { current = op.divide(a, b); tokens[i - 1] = Convert.ToString(current); //Delete both at i and i+1 tokens.RemoveAt(i); tokens.RemoveAt(i); break; } } } } while (tokens.Contains("+") || tokens.Contains("-")) { for (int i = 0; i < tokens.Count(); i++) { if (tokens[i] == "+" && i >= 1) { if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b)) { current = op.plus(a, b); tokens[i - 1] = Convert.ToString(current); //Delete both at i and i+1 tokens.RemoveAt(i); tokens.RemoveAt(i); break; } } if (tokens[i] == "-" && i >= 1) { if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b)) { current = op.minus(a, b); tokens[i - 1] = Convert.ToString(current); //Delete both at i and i+1 tokens.RemoveAt(i); tokens.RemoveAt(i); break; } } } } } return(Convert.ToDouble(tokens[0])); }