Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program write a program called \" infix.cpp \", that uses a stack to con

ID: 3544651 • Letter: W

Question

Write a program write a program  called "infix.cpp", that uses a stack  to convert a postfix expression to the corresponding fully-parenthesized infix  expression.  Consider the following examples:

Your program should ask the user for a postfix  expression as input, and it should output the corresponding fully-parenthesized  infix expression. The program should also ask the user if he/she would like to  do another conversion.  If so, the user should be able to enter another  posfix expression; otherwise the program should terminate. Also, the stack must be implemented using a  singly-linked list.  Your driver, infix.cpp, should include the definition  and declaration files for the class STACK, stack.cpp and stack.h, respectively.

**Using CodePad is helpful to understand your code.

Otherwise just submit your code neatly (understandable)

THANK YOU!

Explanation / Answer

Please check this code.
In the input don't give space between the characters.Give entire thing as a string.It is basically c code converted into c++


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef struct node
{
struct node * left;
char data;
char dumbo;
struct node * right;
}node;
int pref(char c)
{
if(c=='-' || c=='+')
return 1;
else if(c=='*')
return 2;
else if(c=='/')
return 3;
else if(c=='(')
return 0;

return 0;
}
int cmp(char c)
{
if(c=='-')
return 2;
else if(c=='+')
return 1;
else if(c=='*')
return 3;
else if(c=='/')
return 4;

return 0;

}
node * tree(char c[]);
void print(node * a);
int top=-1;
int main()
{
char c='';
while(1)
{
char t[100000]={''};
scanf("%s",t);
node * root;
top=strlen(t);
root=tree(t);
printf("( ");
print(root);
printf(") ");
printf(" ");
if(c==EOF)
return 0;
}
return 0;
}
node * tree(char c[])
{
node * k[5000]={NULL};
node * temp=NULL;
int pot=-1,i;
for(i=0;i<top;i++)
{
if(c[i]=='-'||c[i]=='+'||c[i]=='*'||c[i]=='/')
{
temp=(node *)malloc(sizeof(node));
temp->left=k[pot-1];
temp->right=k[pot];
temp->data=c[i];
temp->dumbo='a';
pot--;
k[pot]=temp;
}
else
{
temp=(node *)malloc(sizeof(node));
temp->left=temp->right=NULL;
temp->data=c[i];
temp->dumbo='a';
pot++;
k[pot]=temp;

}
}
return k[0];
}
void print(node * a)
{
if(a->left==NULL && a->right==NULL)
{
printf("%c ",a->data);
return;
}
if(a->left->data=='-'||a->left->data=='+'||a->left->data=='*'||a->left->data=='/')
{
if(pref(a->data)>pref(a->left->data))
{
a->left->dumbo='(';
}
if((cmp(a->data)==cmp(a->left->data)) && (a->left->data=='-' || a->left->data=='/'))
{
a->left->dumbo='(';
}
}
if(a->right->data=='-'||a->right->data=='+'||a->right->data=='*'||a->right->data=='/')
{
if(cmp(a->data)>cmp(a->right->data))
{
a->right->dumbo='(';
}
if((cmp(a->data)==cmp(a->right->data)) && (a->right->data=='-' || a->right->data=='/'))
{ a->right->dumbo='(';
}

}
if(a->dumbo=='(')
printf("( ");
print(a->left);
printf("%c ",a->data);
print(a->right);
if(a->dumbo!='a')
printf(") ");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote