Please solve the errors.
The code:
#include "stdafx.h"
# include
# include
# include
# include
using namespace std;
int top=-1;
char Stack[25][100]={NULL};
void push(const char *);
const char* pop( );
void postfix_to_infix(constchar *);
int main( )
{
clrscr( );
char Postfix_expression[100]={NULL};
cout<<"\n\n\t Enter the Postfix Expression : ";
cin.getline(Postfix_expression,80);
postfix_to_infix(Postfix_expression);
getch( );
return 0;
}
/*************************************************************************///----------------------------- push(const char) ----------------------///*************************************************************************/void push(constchar *Symbol)
{
if(top==24)
cout<<"Error : Stack is full."<
else
{
top++;
strcpy(Stack[top],Symbol);
}
}
/*************************************************************************///-------------------------------- pop( ) -----------------------------///*************************************************************************/constchar* pop( )
{
char Symbol[100]={NULL};
if(top==-1)
cout<<"Error : Stack is empty."<
else
{
strcpy(Symbol,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Symbol;
}
/*************************************************************************///--------------------- postfix_to_infix(const char *) ----------------///*************************************************************************/void postfix_to_infix(constchar *Postfix)
{
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<<"\n\n\t Infix Expression is : "< }
# include
# include
# include
# include
using namespace std;
int top=-1;
char Stack[25][100]={NULL};
void push(const char *);
const char* pop( );
void postfix_to_infix(constchar *);
int main( )
{
clrscr( );
char Postfix_expression[100]={NULL};
cout<<"\n\n\t Enter the Postfix Expression : ";
cin.getline(Postfix_expression,80);
postfix_to_infix(Postfix_expression);
getch( );
return 0;
}
/*************************************************************************///----------------------------- push(const char) ----------------------///*************************************************************************/void push(constchar *Symbol)
{
if(top==24)
cout<<"Error : Stack is full."<
else
{
top++;
strcpy(Stack[top],Symbol);
}
}
/*************************************************************************///-------------------------------- pop( ) -----------------------------///*************************************************************************/constchar* pop( )
{
char Symbol[100]={NULL};
if(top==-1)
cout<<"Error : Stack is empty."<
else
{
strcpy(Symbol,Stack[top]);
strset(Stack[top],NULL);
top--;
}
return Symbol;
}
/*************************************************************************///--------------------- postfix_to_infix(const char *) ----------------///*************************************************************************/void postfix_to_infix(constchar *Postfix)
{
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<<"\n\n\t Infix Expression is : "<


Sam HobbsRank #107 · 20.5K reputationPosted Oct 15, 2014, 11:41 AM EDT
Joe WilsonRank #631 · 3.8K reputationPosted Oct 16, 2014, 1:50 AM EDT
For Example:
The post fix expression : c a b - * c d / +
The infix expression with full parenthesis: ( ( ( c * ( a + b ) + ( c / d ) )
The Infix expression with less parenthesis ( by paying attention to the mathematical operator priorities ) :
( c * ( a + b ) ) + c / d
Wim SturkenboomRank #2,527 · 554 reputationPosted Oct 16, 2014, 2:50 AM EDT
I have always been told (learned at primary school or so) that multiplying goes before addition. If I'm right and the above is the output of your code, your code is wrong
No idea how to fix it though.
Joe WilsonRank #631 · 3.8K reputationPosted Oct 16, 2014, 3:12 AM EDT
( c * (a + b) ) + c / d
Joe WilsonRank #631 · 3.8K reputationPosted Oct 17, 2014, 12:40 AM EDT