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. Use stacks to convert an expression in infix notation into postfix notation
c. Print out the postfix notation
iii. 6*w/5+z/8+9-x$
//###################### PGM START ###############################
#include<iostream>
#include<stack>
using namespace std;
//get the precedence of the character
int getPrecedence(char c){
if(c=='*' || c=='/')
return 2;
else if(c=='+' || c=='-'){
return 1;
}
}
//function to convert an infix expression to postfix
void convertInfixToPostfix(string s){
stack<char> stk;
stk.push('$');
int n=s.length();
string result;
//repeat untill length of the string
for(int i=0;i<n;i++){
//if the character is alphabet or
digit add it to final result
if(
isalpha(s[i])||(isdigit(s[i])))
result+=s[i];
//if the character is operator
check the predecne of the operator
else if(s[i]=='+' ||s[i]=='-' ||
s[i]=='*' || s[i]=='/'){
//if the
precedence of operator is less than that of one in stack then pop
operator in stack
while(stk.top()
!= '$' && getPrecedence(s[i]) <=
getPrecedence(stk.top()))
{
char ch = stk.top();
stk.pop();
result += ch;
}
//fincally push the current oprator to stack
stk.push(s[i]);
}
//if the input character is $ then
print the result
else if(s[i]=='$'){
break;
}
//for all other characters print
ivlaid
else{
cout<<"Invalid input\n";
return;
}
}
while(stk.top() != '$')
{
char ch =
stk.top();
stk.pop();
result += ch;
}
cout<<"Postfix:
"<<result<<"\n";
}
int main(){
//string exp = "3+4*5/6$";
//string exp="A+B+C+D$";
string exp;
cout<<"Enter the infix expression: ";
getline(cin,exp);
convertInfixToPostfix(exp);
return 0;
}
//######################## PGM END ###################################
OUTPUT
#########
Write a program in c++ to convert an expression written in infix notation to be converted...
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...
You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2) One alphabetic character - "x" (representing a value to be supplied later). (3) Binary operators (+, -, *, / and % (modulo)). (4) Parentheses You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...
Write a Java program that will implement a stack object to convert from either infix notation to postfix notation or postfix notation to infix notation. The program will also implement a link list data structure to track all the conversions done. The Program should have a menu like the following as its output: "Please select what type of conversion you would like to do: Infix to postfix Postfix to infix Print Equations Exit"
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 program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...
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...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...
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...