MCPcopy Create free account
hub / github.com/Aloshi/EmulationStation / eval

Method eval

src/MathExp.cpp:36–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34}
35
36float MathExp::eval()
37{
38 unsigned int start = 0;
39 for(unsigned int i = 0; i < mExpression.length(); i++)
40 {
41 if(isOperator(mExpression.at(i)))
42 {
43 //the string from start to i is an operand, and i is an operator
44 if(start != i) //if we actually do have an operand
45 mOperands.push(strToVal(mExpression.substr(start, i - start)));
46 else
47 std::cout << "skipping operand, start == i\n";
48
49 //now we must decide what to do with the operator
50 const char op = mExpression.at(i);
51
52 if(op != *"(")
53 {
54 while(mOperators.size() && getPrecedence(mOperators.top()) >= getPrecedence(op))
55 {
56 doNextOperation();
57 }
58 }
59
60 mOperators.push(op);
61
62 start = i + 1;
63 }else{
64 if(isRParen(mExpression.at(i)))
65 {
66 while(mOperators.top() != *"(")
67 {
68 doNextOperation();
69 }
70
71 mOperators.pop();
72 }
73 }
74 }
75
76 mOperands.push(strToVal(mExpression.substr(start, mExpression.length() - start)));
77
78
79 while(mOperators.size() > 0)
80 doNextOperation();
81
82
83 if(mOperands.size() != 1)
84 {
85 std::cout << "Error - mOperands.size() = " << mOperands.size() << " at the end of evaluation!\n";
86 return 0;
87 }
88
89 float final = mOperands.top();
90 mOperands.pop();
91
92 return final;
93}

Callers 1

resolveExpMethod · 0.80

Calls 2

pushMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected