Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE HELP!! getting weird output when i try to evaluate expression for example: 2 + 3 = 33 #include using namespace std; class myStack {

PLEASE HELP!!

getting weird output when i try to evaluate expression

for example: 2 + 3 = 33

#include

using namespace std;

class myStack

{

public:

int val;

myStack* next;

};

class stack{

public:

int size;

myStack* a;

stack(){

size = 0;

a = NULL;

}

void push (int);

char pop();

char topS();

void show();

bool isEmpty();

};

void stack::push(int b){

myStack *temp;

temp = new myStack;

temp->next = a;

temp->val= b;

a = temp;

size++;

}

char stack::topS(){

if(size == 0)

return '\0';

else

return a->val;

}

bool stack::isEmpty(){

if (size == 0){

return true;

}

return false;

}

void stack::show(){

cout << "print stack"<< endl;

int s = size;

for(int i = 0; i

cout << pop() << endl;

}

char stack::pop(){

char c;

if(isEmpty()){

//cout << "Stack is empty" << endl;

return '!';

}

else{

myStack *temp = a;

a = a->next;

c = temp->val;

delete temp;

size--;

}

return c;

}

class tokenReader{

public:

char infix[300];

char postfix[300];

char pFix;

stack s; // operations will be perform through this stack

tokenReader(){ // constructor

pFix = 0;

}

bool getOp(char a){ // check the operator appears

if ( a == '+' || a == '-' || a == '/' || a == '*')

return true;

else

return false;

}

bool checkBal(char a, char b){

if ( a == '+' || a == '-')

return true;

else if ( a== '*' || a == '/'){

if (b == '+' || b == '-'){

return false;

}

else if (b== '*' || b == '/'){

return true;

}

}

return false;

}

void PostFix(){

int i = 0;

cout << "Starting Expression Evaluation Program" << endl;

cout << "Enter Expression: ";

cin >> infix;

for (int a = 0 ; infix[a]!= '\0'; a++){

i++;

}

for (int a = 0; a

if (infix[a] == '(')

s.push(infix[a]);

else if (getOp(infix[a])){

while (getOp(s.topS()) && checkBal( infix[a],s.topS())){

postfix[pFix] = s.pop();

pFix++;

}

s.push(infix[a]);

}

else if (infix[a] == ')'){

while (s.topS()!= '('){

postfix[pFix] = s.pop();

pFix++;

}

s.pop();

}

else {

postfix[pFix]= infix[a];

}

}

while (!(s.isEmpty())){

postfix[pFix] = s.pop();

pFix++;

}

postfix[pFix]= '\0';

}

int calculate (int op1, int op2, char op3)

{

if(op3 == '+'){

return op1 +op2;

}

else if(op3 == '-'){

return op1 - op2;

}

else if( op3 == '*'){

return op1 * op2;

}

else if (op3 == '/'){

return op1 / op2;

}

return 0;

}

void evalPostFix(){

pFix = 0;

while (postfix[pFix]!= '\0'){

if (getOp(postfix[pFix])){

s.push(postfix[pFix]);

pFix++;

}

else if (getOp(postfix[pFix])){

int i = s.pop()- 48;

int j = s.pop()- 48;

cout << "OP: " << i << "OP: " << j << endl;

int x = calculate (i, j, postfix[pFix]);

s.push(x);

pFix++;

}

}

int result = s.pop();

cout << result << endl;

}

};

int main(){

tokenReader a1;

a1.PostFix();

a1.evalPostFix();

getchar();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial Databases 2nd Symposium Ssd 91 Zurich Switzerland August 1991 Proceedings Lncs 525

Authors: Oliver Gunther ,Hans-Jorg Schek

1st Edition

3540544143, 978-3540544142

More Books

Students also viewed these Databases questions