If you run my program choose infix to postfix. It evaluates ((2+((10*4)/5))-(9-3
ID: 3805229 • Letter: I
Question
If you run my program choose infix to postfix. It evaluates ((2+((10*4)/5))-(9-3)) to -5 because it counts 10 as 1, 0
I need help solving this to make it accept 10 as a whole number. It should evaluate to 4.
#include <iostream>
#include <stack>
#include <string>
#include <algorithm>
#define flag '#'
#include <cstring>
using namespace std;
class Expression{
public:
string inToPost(string);//done
string postToIn(string);//done
string inToPre(string);
string preToIn(string);
string convertThis; // Expression that we want converted
int evaluate(string); // Evaluate numeric expression
string evaluated;
int evaluation;
Expression(string input, int direction); //constructor
bool isOperator(char character);
bool isOperand(char character);
int isHigherWeight(char character);
bool isHigherPrecedence(char op1, char op2);
void printResult(int choice);
private:
string infix;
string postfix;
string prefix;
};
//Constructor function
Expression::Expression(string input, int direction){
convertThis = input;
switch (direction){
case 1: infix = input;
case 2: postfix = input;
case 3: prefix = input;
}
}
//Operator Function checks to see if character is a legal symbol
bool Expression::isOperator(char character){
if((character == '*')||(character == '+')||(character == '-')||(character == '/'))
return true;
else
return false;
}
//Operand Function checks to see if character is a legal character
bool Expression::isOperand(char character){
if(character >= 'a' && character <= 'z')
return true;
if(character >= 'A' && character <= 'Z')
return true;
if(character >= '0' && character <= '9')
return true;
else
return false;
}
//Function determines the weight of Operator.
int Expression::isHigherWeight(char character){
int weight = -1; //
switch(character){
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
}
return weight;
}
//Function that compares weights of two different Operators.
bool Expression::isHigherPrecedence(char oper1, char oper2){
int op1Weight = isHigherWeight(oper1);
int op2Weight = isHigherWeight(oper2);
// If operators have equal precedence, return true
// return false
if (op1Weight == op2Weight){
return true;
}
return op1Weight > op2Weight ? true: false;{
}
}
//*************EVALUATE FUNCTIONS****************
int doOperation(int operation, int operand1,int operand2)
{
if(operation == '+') return operand1 + operand2;
else if(operation == '-') return operand1 - operand2;
else if(operation == '*') return operand1 * operand2;
else if(operation == '/') return operand1 / operand2;
else cout<<"Something went wrong ";
return -1;
}
bool isANumber(char character)
{
if(character >= '0' && character <= '9')
return true;
return false;
}
string Expression::inToPost(string myExpression){
// TEST FULLY PARENTHESIZED: ((2+((10*4)/5))-(9-3))
stack<char> Stack;
string postfix = ""; // Initialize postfix as empty string.
for(int i = 0;i< myExpression.length();i++){ //go through array of string convertThis
if (myExpression[i] == ' ' || myExpression[i] == ',') continue;
else if(isOperator(myExpression[i])){
while(!Stack.empty() && Stack.top() != '(' && isHigherPrecedence(Stack.top(),myExpression[i])){
postfix += Stack.top();
//appending stack char to postfix string
Stack.pop();
}
Stack.push(myExpression[i]);
}
else if(isOperand(myExpression[i])){
postfix += myExpression[i]; //appending expression at pos[i] to postfix string
postfix += " ";
}
else if(myExpression[i] == '('){
Stack.push(myExpression[i]);
}
else if (myExpression[i] == ')'){
while(!Stack.empty() && Stack.top() != '('){
postfix += Stack.top(); //appending stack char to postfix string
// postfix += " ";
Stack.pop();
}
Stack.pop();
}
}
while(!Stack.empty()){
postfix += Stack.top(); //appending stack char to postfix string
// postfix += " ";
Stack.pop();
}
evaluated = postfix;
evaluation = evaluate(evaluated);
cout << evaluation;
return postfix;
}
string Expression::inToPre(string myExpression){
stack<char> Stack;
string prefix = ""; // Initialize postfix as empty string.
int length;
length = myExpression.length();
for(int i = length -1; i >=0 ;i--){ //go through array of string convertThis
if (myExpression[i] == ' ' || myExpression[i] == ',') continue;
else if(isOperator(myExpression[i])){
while(!Stack.empty() && Stack.top() != ')' && isHigherPrecedence(Stack.top(),myExpression[i])){
prefix += Stack.top(); //appending stack char to postfix string
Stack.pop();
}
Stack.push(myExpression[i]);
}
else if(isOperand(myExpression[i])){
prefix += myExpression[i]; //appending expression at pos[i] to postfix string
}
else if(myExpression[i] == ')'){
Stack.push(myExpression[i]);
}
else if (myExpression[i] == '('){
while(!Stack.empty() && Stack.top() != ')'){
prefix += Stack.top(); //appending stack char to postfix string
Stack.pop();
}
Stack.pop();
}
}
while(!Stack.empty()){
prefix += Stack.top(); //appending stack char to postfix string
Stack.pop();
}
reverse(prefix.begin(), prefix.end()); //reverse complete string to form prefix
// evaluation = evaluate(myExpression);
// cout << evaluation;
return prefix;
}
string Expression::postToIn(string myExpression){
//THIS FUNCTION WORKS.
stack<string> Stack;
string infix = ""; // Initialize postfix as empty string.
string leftParenthesis = "(";
string rightParenthesis = ")";
string leftValue;
string rightValue;
string myOperator;
string currentinfix;
bool leftDone = true; // leftDone if false means left value needs to be initialised.
bool rightDone = false; // rightDone true means rightDone already has a value hence leftvalue should go to stack , right value to
//leftvalue and new value to right value in case of operand.
leftValue = myExpression[0];
for(int i = 1;i< myExpression.length();i++){
if (isOperand(myExpression[i])){
if(leftDone){
if(rightDone){
Stack.push(leftValue);
leftValue = rightValue;
rightValue = myExpression[i];
}else{
rightValue = myExpression[i];
rightDone = true;
}
}else{
leftDone = myExpression[i];
leftDone = true;
}
}else{
if(rightDone){
leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis;
rightDone = false;
}
else{
rightValue = leftValue;
leftValue = Stack.top();
Stack.pop();
leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis;
}
}
}
return leftValue;
}
string Expression::preToIn(string myExpression){
// first convert prefix to postfix then postfix to infix
stack<string> Stack;
string infix = ""; // Initialize postfix as empty string.
string leftParenthesis = "(";
string rightParenthesis = ")";
string leftValue;
string rightValue;
string myOperator;
string currentinfix;
bool leftDone = true; // leftDone if false means left value needs to be initialised.
bool rightDone = false; // rightDone true means rightDone already has a value hence leftvalue should go to stack , right value to
int length; //leftvalue and new value to right value in case of operand.
leftValue = myExpression[0];
length = myExpression.length();
for(int i = length -1; i >=0 ;i--){
if (isOperand(myExpression[i])){
if(leftDone){
if(rightDone){
Stack.push(leftValue);
leftValue = rightValue;
rightValue = myExpression[i];
}else{
rightValue = myExpression[i];
rightDone = true;
}
}else{
leftDone = myExpression[i];
leftDone = true;
}
}else{
if(rightDone){
leftValue = rightParenthesis + leftValue + myExpression[i] + rightValue + leftParenthesis;
rightDone = false;
}
else{
rightValue = leftValue;
leftValue = Stack.top();
Stack.pop();
leftValue = rightParenthesis + leftValue + myExpression[i] + rightValue + leftParenthesis;
}
}
}
reverse(leftValue.begin(), leftValue.end());
return leftValue;
}
int Expression::evaluate(string myExpression){
//postfix evaluate MUST BE NUMERIC DIGIT TO EVALUATE AND SPACES BETWEEN OPERANDS
stack<int> Stack; //declaring stack of int, as we are only dealing with numbers
for(int i = 0;i< myExpression.length();i++) {
//if theres a space or comma.. keep going.
if(myExpression[i] == ' ' || myExpression[i] == ',') continue;
else if(isOperator(myExpression[i])) { //Expression is postfix so if we run into Operator pop two operands and perform operation
// Pop two operands.
int operand2 = Stack.top(); Stack.pop();
int operand1 = Stack.top(); Stack.pop();
// Perform operation
int result = doOperation(myExpression[i],operand1,operand2);
//Push back result of operation on stack.
Stack.push(result);
}
else if(isANumber(myExpression[i])){
// Extract the numeric operand from the string
// Keep incrementing i as long as you are getting a numeric digit.
int operand = 0;
while(i<myExpression.length() && isANumber(myExpression[i])) {
// For a number with more than one digits, as we are scanning from left to right.
// Everytime , we get a digit towards right, we can multiply current total in operand by 10
// and add the new digit.
operand = (operand*10) + (myExpression[i] - '0');
i++;
}
// Finally, you will come out of while loop with i set to a non-numeric character or end of string
// decrement i because it will be incremented in increment section of loop once again.
// We do not want to skip the non-numeric character by incrementing i twice.
i--;
// Push operand on stack.
Stack.push(operand);
}
}
// If expression is in correct format, Stack will finally have one element. This will be the output.
return Stack.top();
}
void Expression::printResult(int choice){
cout << "What is your choice?" << endl;
cin >> choice;
cout << "******************************************************************************" << endl;
if (choice == 1)
cout << "This expression Evaluates to: " << evaluation << endl << "The conversion from Infix to Postfix is: " << inToPost(convertThis) << endl << "******************************************************************************" << endl;
else if (choice == 2)
cout << "The conversion from Infix to Prefix is: " << inToPre(convertThis) << endl << endl << "******************************************************************************" << endl;
else if (choice == 3)
cout << "The conversion from Postfix to Infix is: " << postToIn(convertThis) << endl << endl << "******************************************************************************" << endl;
else if (choice == 4)
cout << "The conversion from Prefix to Infix is: " << preToIn(convertThis) << endl << endl << "******************************************************************************" << endl;
else if (choice == 5)
cout << "The Evaluation of your expression equals: " << evaluate(convertThis) << endl <<endl<< "******************************************************************************" << endl;
}
int main(){
string convertThis;
int choice;
string str;
cout << " Conversion Choice Menu " << endl;
cout << "______________________________________________" << endl << endl;
cout << "[1]Infix to Postfix? -> No spaces or parenthesis needed!" << endl;
cout << "[2]Infix to Prefix? -> No spaces or parenthesis needed!"<< endl;
cout << "[3]Postfix to Infix? -> No spaces! " << endl;
cout << "[4]Prefix to Infix? -> No spaces! " << endl;
cout << "[5]Evaluate an Expression? -> #'s only! " <<endl;
cout << "______________________________________________" << endl << endl;
//int choice;
//cin.ignore();
cout << "Enter the expression you want to convert: ";
getline(cin,str);
Expression myexp(str, 1);
myexp.printResult(choice);
}
Explanation / Answer
This is beccause the push and pop considers one character i.e Expression[i] is readig i=0;i<=Expression.lenght();
This actionn be handled in ur stack push operation.
we push the operand if the symbol is <9 and the previous element of the stack is also anumber ,then append the operand with previous symbol in the stack and place in the same place..
void Stack :: push(int symbol)
{
if(top>MAXSIZE)
{
cout<<"Stack overflow ";
exit(1);
}
if (stack.empty() && stack[top] >= 0 && stack[top] <= 9) {
if(stack[top-1] >=0 && stack[top-1] <= 9){
top=top-1;
stack[top] *= 10;
stack[top] += symbol;
}
else {
stack[++top]=symbol;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.