Please help me because the first letter and the first operator of the operation
ID: 3638520 • Letter: P
Question
Please help me because the first letter and the first operator of the operation you enter does not display in the output.Here is the code:
I use Borland C++ 5.0.2
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
char data;
node *next;
};
node *T=NULL;
node *B=NULL;
node *E;
node *L;
node *SL;
void push(char);
const char pop( );
void infix_to_postfix(char *Infix);
int main( )
{
clrscr( );
char Infix[50]={NULL};
cout<<" Infix Expression : ";
cin>>Infix;
infix_to_postfix(Infix);
getch( );
return 5;
}
void push(char Operator)
{
E=new node;
if(B==NULL)
{
E->data=Operator;
E->next=NULL;
B=E;
T=E;
}
if(B!=NULL)
{
E->data=Operator;
E->next=NULL;
T->next=E;
T=E;
}
}
const char pop( )
{
char Operator=NULL;
if(B==NULL)
cout<<" Stack is empty..."<<endl;
else
{
for(L=B;L->next!=NULL;L=L->next){
SL=L;}
if(T==B)
B=NULL;
Operator=T->data;
delete T;
T=SL;
T->next=NULL;
}
return Operator;
}
void infix_to_postfix(char *Infix)
{
char Postfix[50]={NULL};
strcpy(Infix,"(");
strcat(Infix,Infix);
strcat(Infix,")");
char Operator[5]={NULL};
char Temp[5]={NULL};
int count=0;
do
{
Operator[0]=Infix[count];
if(Operator[0]=='(')
push(Operator[0]);
else if(Operator[0]==')')
{
Operator[0]=pop( );
while(Operator[0]!='(')
{
strcat(Postfix,Operator);
Operator[0]=pop( );
}
}
else if(Operator[0]=='^' || Operator[0]=='*' || Operator[0]=='/'|| Operator[0]=='+' || Operator[0]=='-')
{
if(Operator[0]=='*' || Operator[0]=='/')
{
Temp[0]=pop( );
while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/')
{
strcat(Postfix,Temp);
Temp[0]=pop( );
}
push(Temp[0]);
}
else if(Operator[0]=='+' || Operator[0]=='-')
{
Temp[0]=pop( );
while(Temp[0]!='(')
{
strcat(Postfix,Temp);
Temp[0]=pop( );
}
push(Temp[0]);
}
push(Operator[0]);
}
else
strcat(Postfix,Operator);
count++;
}while(count<strlen(Infix));
cout<<" Postfix Expression : "<<Postfix;
}
Thank you for your help. It would mean a lot to me :D
Explanation / Answer
Infix, Postfix and Prefix Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands. Infix notation: X + Y Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer." Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction. (see CS2121 lecture). Postfix notation (also known as "Reverse Polish notation"): X Y + Operators are written after their operands. The infix expression given above is equivalent to A B C + * D / The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the "+" is to the left of the "*" in the example above, the addition must be performed before the multiplication. Operators act on values immediately to the left of them. For example, the "+" above uses the "B" and "C". We can add (totally unnecessary) brackets to make this explicit: ( (A (B C +) *) D /) Thus, the "*" uses the two values immediately preceding: "A", and the result of the addition. Similarly, the "/" uses the result of the multiplication and the "D". Prefix notation (also known as "Polish notation"): + X Y Operators are written before their operands. The expressions given above are equivalent to / * A + B C D As for Postfix, operators are evaluated left-to-right and brackets are superfluous. Operators act on the two nearest values on the right. I have again added (totally unnecessary) brackets to make this clear: (/ (* A (+ B C) ) D)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.