Write a program to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b. Use stacks to convert an expression in infix notation into postfix notation c. Print out the postfix notation d. Run your program on these strings to test it: i. a+b/c*d-y*x$ ii. 4*8-3+7/3*5-9$ iii. 6*w/5+z/8+9-x$
What to submit
• A header file containing the definition of the Stack class
• A C++ file containing the implementation of the class
• A C++ file containing the main function that converts an expression in infix notation into an expression in postfix notation
#include<iostream>
#include<string>
using namespace std;
// .h file
template <class X>
class Stack
{
X *stackData;
int currentIndex;
int size;
public:
Stack(int size = 10);
void push(X);
X pop();
X top();
int getSize();
bool isEmpty();
bool isFull();
void display();
};
// (1) .cpp file
template <class X>
Stack<X>::Stack(int size)
{
stackData = new X[size];
this->size = size;
currentIndex = -1;
}
template <class X>
void Stack<X>::push(X data)
{
if (!isFull())
{
stackData[++currentIndex] = data;
}
}
template <class X>
X Stack<X>::pop()
{
if (!isEmpty())
{
return stackData[currentIndex--];
}
}
template <class X>
X Stack<X>::top()
{
if (!isEmpty())
return stackData[currentIndex];
}
template <class X>
int Stack<X>::getSize()
{
return size;
}
template <class X>
bool Stack<X>::isEmpty()
{
return currentIndex == -1;
}
template <class X>
bool Stack<X>::isFull()
{
return currentIndex == size - 1;
}
template <class X>
void Stack<X>::display() {
if (currentIndex>-1)
{
cout << "Data : \n\t";
for (int i = 0; i <= currentIndex; i++)
cout << stackData[i] << " ";
cout << "\n";
}
else
{
cout << "Stack is Empty\n";
}
}
// (2) .cpp file
//--------------------------------(Functions)--------------------------------------
bool IsOperand(char character)
{
if (character >= '0' && character <= '9') {
return true;
}
if (character >= 'a' && character <= 'z') {
return true;
}
if (character >= 'A' && character <= 'Z') {
return true;
}
return false;
}
bool IsOperator(char character)
{
if (character == '+' || character == '-' || character == '*' ||
character == '/' || character == '%')
{
return true;
}
return false;
}
int GetoperatorPresidency(char op)
{
int presidency = 0;
switch (op)
{
case '+':
case '-':
presidency = 1;
break;
case '*':
case '/':
case '%':
presidency = 2;
break;
}
return presidency;
}
int HasHigherPrecedence(char op1, char op2)
{
int op1Weight = GetoperatorPresidency(op1);
int op2Weight = GetoperatorPresidency(op2);
if (op1Weight == op2Weight)
return true;
return op1Weight > op2Weight ? true : false;
}
string Intopostfix(string expression, Stack<char> stack)
{
string postfix = "";
for (int i = 0; i< expression.length()-1; i++) { // $ is the end
so we are checking before $ sign.
if (IsOperator(expression[i]))
{
while (!stack.isEmpty() && stack.top() !=
'(' && HasHigherPrecedence(stack.top(),
expression[i]))
{
postfix += stack.top();
stack.pop();
}
stack.push(expression[i]);
}
else if (IsOperand(expression[i]))
{
postfix += expression[i];
}
else if (expression[i] == '(')
{
stack.push(expression[i]);
}
else if (expression[i] == ')')
{
while (!stack.isEmpty() && stack.top() !=
'(') {
postfix += stack.top();
stack.pop();
}
stack.pop();
}
}
while (!stack.isEmpty()) {
postfix += stack.top();
stack.pop();
}
return postfix;
}
//-----------------------------------------------------------------------
void ConvertiontopostFix() {
string expression;
cout << "Enter Infix Expression : ";
getline(cin, expression);
Stack<char> stack(expression.length());
cout << "Postfix Expression is : " <<
(Intopostfix(expression, stack)) << endl;
cout << endl;
}
//---------------------Main----------------------------------------------------
int main() {
ConvertiontopostFix();
return 0;
}


Write a program to convert an expression written in infix notation to be converted to postfix...
Write a program in c++ to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b....
Complete the following java program in which infix expressions should be converted to postfix expressions /** * An algebraic expression class that has operations such as conversion from infix * expression to postfix expression, * and evaluation of a postfix expression */ public class Expression { /** * The infix of this expression */ private String infix; /** * Constructs an expression using an infix. */ public Expression(String infix){ this.infix = infix; } /** * Converts an infix expression into...
Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression try{ Scanner inf = new Scanner (System.in); // Read input from KB/ File while(inf.hasNext()){ // read next infix expression iexp = inf.next(); // Assume method name to convert infix...
In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is 62+5*84/- The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The...
Code should be written in java Write a program to convert an infix expression to postfix expression using stack. Algorithm: a) Create a stack b) For each character t in the input stream If(t is an operand) append t to the output. Else if (t is a right parenthesis) Pop and append to the output until a left parenthesis is popped (but do not append this parenthesis to the output). Else if(t is an operator or left parenthesis) Pop and...
Using Java- Write a program that takes an arithmetic expression in an infix form, converts it to a postfix form and then evaluates it. Use linked lists for the infix and postfix queues and the operator and value stacks. You must use sperate Stack and Queue classes with a driver class to run the program. example Input : 111++ Output : (1+ (1+ 1)) Answer: 3
C++ Write a program that takes an infix expression as an input and produces a postfix expression. Use stack to convert an infix expression into postfix expression. Include a function that evaluates a postfix expression.
C++ Stack Program Write a program that uses stacks to evaluate an arithmetic expression in infix notation. The program should NOT convert the infix to postfix and then evaluate the postfix. The program takes as input a numeric expression in infix notation, such as 3+4*2, and outputs the result. 1a) Operators are +, -, *, / 1b) Assume that the expression is formed correctly so that each operation has two arguments. 1c) The expression can have parenthesis, for example: 3*(4-2)+6...
We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...
Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...