Postfix to Infix Conversion. Get an postfix artithmetic expression such as 2 6 +
ID: 670302 • Letter: P
Question
Postfix to Infix Conversion. Get an postfix artithmetic expression such as 2 6 + 6 * 7 3 / - and convert to Infix. I already have a C code about it but need to converted into C++. Need to use stacks and classes and final result has to be shown with the corresponding parenthesis.
THANKSSSSS!!!
Here's the C code:
#include
#include
int top = 10;
struct node
{
char ch;
struct node *next;
struct node *prev;
} *stack[11];
typedef struct node node;
void push(node *str)
{
if (top <= 0)
printf("Stack is Full ");
else
{
stack[top] = str;
top--;
}
}
node *pop()
{
node *exp;
if (top >= 10)
printf("Stack is Empty ");
else
exp = stack[++top];
return exp;
}
void convert(char exp[])
{
node *op1, *op2;
node *temp;
int i;
for (i=0;exp[i]!='';i++)
if (exp[i] >= 'a'&& exp[i] <= 'z'|| exp[i] >= 'A' && exp[i] <= 'Z')
{
temp = (node*)malloc(sizeof(node));
temp->ch = exp[i];
temp->next = NULL;
temp->prev = NULL;
push(temp);
}
else if (exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/' ||
exp[i] == '^')
{
op1 = pop();
op2 = pop();
temp = (node*)malloc(sizeof(node));
temp->ch = exp[i];
temp->next = op1;
temp->prev = op2;
push(temp);
}
}
void display(node *temp)
{
if (temp != NULL)
{
display(temp->prev);
printf("%c", temp->ch);
display(temp->next);
}
}
void main()
{
char exp[50];
clrscr();
printf("Enter the postfix expression :");
scanf("%s", exp);
convert(exp);
printf(" The Equivalant Infix expression is:");
display(pop());
printf(" ");
getch();
}
The general algorithm would be:
1.While there are input symbol left
2. Read the next symbol from input.
3. If the symbol is an operand
Push it onto the stack.
4. Otherwise,
the symbol is an operator.
5. If there are fewer than 2 values on the stack
Show Error /* input not sufficient values in the expression */
6. Else
Pop the top 2 values from the stack.
Put the operator, with the values as arguments and form a string.
Encapsulate the resulted string with parenthesis.
Push the resulted string back to stack.
7. If there is only one value in the stack
That value in the stack is the desired infix string.
8. If there are more values in the stack
Show Error /* The user input has too many values */
Explanation / Answer
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
int top=-1;
char Stack[25][100]={NULL};
void push(constchar *);
constchar* pop( );
void postfix_to_infix(constchar *);
int main( )
{
clrscr( );
char Postfix_expression[100]={NULL};
cout<<" Enter the Postfix Expression : ";
cin.getline(Postfix_expression,80);
postfix_to_infix(Postfix_expression);
getch( );
return 0;
}
{
if(top==24)
cout<<"Error : Stack is full."<<endl;
else
{
top++;
strcpy(Stack[top],Symbol);
}
}
{
char Symbol[100]={NULL};
if(top==-1)
cout<<"Error : Stack is empty."<<endl;
else
{
strcpy(Symbol,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Symbol;
}
{
char Infix_expression[100]={NULL};
char Postfix_expression[100]={NULL};
strcpy(Postfix_expression,Postfix);
strcat(Postfix_expression,"=");
int count=0;
char Symbol_scanned[5]={NULL};
do
{
Symbol_scanned[0]=Postfix_expression[count];
if(Symbol_scanned[0]=='/' || Symbol_scanned[0]=='*' ||
Symbol_scanned[0]=='-' || Symbol_scanned[0]=='+' ||
Symbol_scanned[0]=='^' )
{
char Value_1[100]={NULL};
char Value_2[100]={NULL};
char Result[100]={NULL};
strcpy(Value_1,pop( ));
strcpy(Value_2,pop( ));
if(Infix_expression[(count+1)]!='=')
strcpy(Result,"(");
strcat(Result,Value_2);
strcat(Result,Symbol_scanned);
strcat(Result,Value_1);
if(Infix_expression[(count+1)]!='=')
strcat(Result,")");
push(Result);
}
else
push(Symbol_scanned);
count++;
}
while(Postfix_expression[count]!='=');
strset(Infix_expression,NULL);
strcpy(Infix_expression,pop( ));
cout<<" Infix Expression is : "<<Infix_expression;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.