Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / Problem statement: Evaluate an infix algebraic expression by converting the infix expression to postfix expression and then evaluate the postfix expression using stack

Problem statement: Evaluate an infix algebraic expression by converting the infix expression to postfix expression and then evaluate the postfix expression using stack

Computer Science

Problem statement:
Evaluate an infix algebraic expression by converting the infix expression to postfix expression and
then evaluate the postfix expression using stack. A data file (“expr.dat”) contains the list of
expressions to be evaluated, one expression per line. You can assume that operands are single digit
positive numbers.
Motivation: Postfix notation is a way of writing algebraic expressions without the use of parentheses
or rules of operator precedence.
Requirements:
• Process infix expressions stored in a file read interactively (for example “expr.dat”). For each
expression in the file:
o Print the infix notation read from the file
o Print the corresponding postfix notation
o Print the result of the evaluation
Assumption: In our simplified examples,
• Each operand is represented by a single digit (0 - 9) and
• Each operator by a single character.
o These are called tokens. Things are more complicated in real life.
o Operands and some operators, like ** for exponentiation, require more than a single
character. However, we can make these simplifications without loss of generality.
o List of operators to be handled: +, -, *, /
• No unary operators are supported and
• An expression contains no embedded blank
• The input expressions are presumed to be valid.
Program requirements:
• Use the template as needed to solve this problem.
• Create functions as needed to make the program modular
Sample output:
Infixpostfixevaluation
1. 2+52 5+7
2. 9*39 3*27
3. 3+4+53 4 + 5+12
4. 3*4+53 4 * 5+17
5. 3+4*5+63 4 5 * + 6+29
6. (3+4)*53 4 + 5*35
7. (9-2)*39 2 – 3*21
8. ((2+3)*4)*52 3 + 4 * 5*100
9. 2-(3*(4+5))+62 3 4 5 + * - 6 + -19

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

 // Header file : my Stack.h #ifndef H_StackType #define H StackType #include #include using namespace std; template class stack Type { private : int maxStackSize; int stacktop; Type list; public: void initializeStack(); bool is Fullstack() const; bool is Emptystack() const; void push( const Type & ); void pop(); Type top() const; stackType( int = 20); -stack Type(); }; template void stackType:: initializestack() { stacktop = 0; } template bool stackType :: isFullstack() const return ( stacktop == maxstackSize ); } template bool stackType :: is Emptystack() const { return ( stack Top == 0); } template void stackType:: push( const Type& newItem> { if (!is Fullstack()) { listi stack Top] = newItem; stacktop++; } else cout << "\n\tcan not add to a full stack"; } template void stackType :: pop() { if (!isEmptystack()) stackTop--; else cout << "\n\tcan not remove from an empty stack"; } return the value of stack-top template Type stackType:: top() const { assert ( stackTop != 0); return list[ stacktop - 1l; 1 template stackType ::stack Type( int stack size) { if ( stack size <= 0 ) { cout << "Invalid size"; stacksize = 10; } else maxStackSize -stackSize; stackTop = 0; list = new Type: maxstackSize ); } template stackTypeType>::-stackType() { deletel list; } // #include #include "mystack.h" using std::string; //a utility function to find a given character is an operator or not bool is operator (const char c) { if (c == "+' ) 11(c == '-') IT (c == '*') II( c == '/') !! %')) return true; else return false; ) int precedence( const char opl, const char op 2 ) { int prel, pre2 if ( ( opl == 'A') || (op2 == 'A' ) ) cout << "Exponentiation was not considered" << "for precedence. In tThe program may" << "result in abnormal output of Postfix exp."; if ( ( opl == '+' ) 11 ( opl == '-')) prel = 0; else if ((op1 == '*') || (op1=='/') || (op1=='' pre = 1; if ( (op2 == '') || (op 2 == '-')) pre2 = 0; else if ((op2 == ***)||(op2=='/') || (op2== *8')) pre 2 = 1; if ( prel == pre2 ) return 0; else if ( prel > pre2 ) return 1; else return -1; } void infixToPostfix( string infix ) { stackType< char > mystack; string pfx = ""; cout << "The infix expression is : " <" ); // push the left parenthesis onto the stack mystack.push('('); unsigned int i = 0; do { if ( isoperator ( infix[i])) { if ( isoperator mystack.top()) if (precedence (infix[i], mystack.top() ==0) { pfx = pfx + mystack.top(); mystack.pop(); } else if (precedence (infix[i], mystack.top()) == 1 > { my stack.push( infix[i]); i++; else { pfx = pfx+ mystack.top(); myStack.pop } else mystack.push( infix[i]); i++; ) else { if ( infixt i l == '') { } while (mystack.top() !='() pfx = pfx + mystack.top(); mystack.pop(); } mystack.pop(); i++; else if ( infixt i l == '') { mystack.push( infix[i]); i++; } else { pfx - pfx + infixt i]; i++; } } while (i < infix.length()); // print the postfix expression cout << "\n\tThe postfix expression is : px; } " << int main() { cout <<"\n\t Program that converts an infix expression" << "\n\tinto an equivalent postfix expression"; string inFix = ""; cout << "\n\n\tEnter an input expression : cin >> inFix; infixToPostfix in Fix ); cout << "\n\n\t"; system ("pause"); return 0;

 

Attachment can be downloaded through below process.

Please use this google drive link to download the answer file.

https://drive.google.com/file/d/1CKpPz9aeNobMGZG9j8E36W9mTuSjuVMX/view?usp=sharing

Note: If you have any trouble in viewing/downloading the answer from the given link, please use this below guide to understand the whole process.

https://helpinhomework.org/blog/how-to-obtain-answer-through-google-drive-link