Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

n5n

Answered: 1 week ago