Question: need help reading decimal values. im trying to write a postfix evaluation program in c++. the problem is i need to read decimals values from

need help reading decimal values. im trying to write a postfix evaluation program in c++. the problem is i need to read decimals values from a string but i cant figure out how to do that. i managed to figure out how to read multiple values to get 2 digit numbers pushed onto the stack but i cant figure out decimal values. here is the code, the part in bold is the part that i need help fixing

double evaluate(string input) { stack Stack;

for ( int n = 0; n < input.length(); n++ ) //start at the begining of the input and loop through each char until you run out of chars to check { if (input[n] == ' ') //if you hit a space, skip it continue;

else if ( isOperator(input[n])) { if (isUnaryOperator(input[n])) { double operand1 = Stack.top(); Stack.pop();

double postfixAnswer = checkOperator(input[n], operand1); Stack.push(postfixAnswer); }

else if (isKaryOperator(input[n])) { double kary = Stack.top(); Stack.pop(); double temp = 0; for (int i = 0; i < kary ; i++) {

temp += Stack.top(); Stack.pop(); } if (input[n] == '$') { Stack.push((temp/kary)); return (temp/kary); } else Stack.push(temp); return temp; } else { double operand1 = Stack.top(); Stack.pop(); double operand2 = Stack.top(); Stack.pop();

double postfixAnswer = checkOperator(input[n], operand2, operand1); Stack.push(postfixAnswer); }

}

else if (input[n] == '.') { double operand = 0; while ((n) < input.length() && isNumber((n+1))) { operand = (operand / 10) + input[n+1] - '0'; n++; } n--; Stack.push(operand); }

else if ( isNumber(input[n])) { double operand = 0; while (n < input.length() && isNumber(input[n])) { operand = (operand * 10) + ((input[n] - '0')); n++; // check if there are two numbers together (11... etc) , if there is just one number by itself (4... etc) it will multiply 0 by ten and add 4, just giving you the one number // if there is a second number it will have the number before it stored in the operand from the first iteration and then it will add the second number it reads; } n--; // the loop will run one extra time so you decrement one to account for it and not skip over any chars

Stack.push(operand);

} } return Stack.top(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!