Question: this code converts infix to postfix #include #include #include #include #include #include #include stack.cpp #define OPERATORS 8 using namespace std; char precedence[OPERATORS][2] = {{'(', 0},

this code converts infix to postfix

#include

#include

#include

#include

#include

#include

#include "stack.cpp"

#define OPERATORS 8

using namespace std;

char precedence[OPERATORS][2] = {{'(', 0}, {'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'%', 2}, {'^', 3}, {')', 4}};

int getIndex(int data)

{

int i;

for (i = 0; i < OPERATORS; i++)

{

if (data == precedence[i][0])

return i;

}

return -1;

}

string convertToPostfix(string infix)

{

Stack *expr = new Stack();

string postfix = "";

int i;

char data;

int index1, index2;

for (i = 0; i < (int)infix.length(); i++)

{

if (infix[i] == ' ')

continue;

if (infix[i] >= '0' && infix[i] <= '9')

{

postfix += infix[i];

if (infix[i + 1] <= '0' || infix[i + 1] >= '9' )

postfix += ' ';

}

else if (infix[i] == ')')

{

data = expr->pop();

while (data != '(' && data != -1)

{

postfix += data;

postfix += ' ';

data = expr->pop();

}

}

else if (infix[i] == '(')

{

expr->push(infix[i]);

}

else

{

data = expr->pop();

if (data == -1)

{

expr->push(infix[i]);

continue;

}

else if (data == '(')

{

expr->push(data);

expr->push(infix[i]);

continue;

}

index1 = getIndex(data);

index2 = getIndex(infix[i]);

while (precedence[index1][1] >= precedence[index2][1])

{

postfix += data;

postfix += ' ';

data = expr->pop();

if (data == -1)

{

expr->push(infix[i]);

break;

}

else if (data == '(')

{

expr->push(data);

expr->push(infix[i]);

data = -1;

break;

}

index1 = getIndex(data);

}

if (data != -1)

{

expr->push(data);

expr->push(infix[i]);

}

}

}

while (1)

{

if ((data = expr->pop()) == -1)

break;

postfix += data;

postfix += ' ';

}

return postfix;

}

int main()

{

string output, equation;

ifstream inInfo;

inInfo.open("input.txt");

while(getline(inInfo, equation)){

cout<< equation <

output = convertToPostfix(equation);

cout << output << endl<

}

return 0;

}

where do i add this algorithim?

st = postfix_string for (i=0; i < length(st); i++) { token = st[i] switch (token) { digit : push token break operator : pop opn2 pop opn1 result = evaluate(opn1, token, opn2) push result break } } int evaluate (int opn1, char token, int opn2) { switch (token) { case '+' : result = opn1 + opn2; case '-' : result = opn1 - opn2; case '*' : result = opn1 * opn2; case '/' : result = opn1 / opn2; case '^' : result = opn1 ** opn2; (Fortran exponentiation) } return result; } 

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!