Mar 9, 2011 at 3:45pm UTC
#include<iostream.h>
#include<conio.h>
char ch;
int top=-1;
char st[100];
char stack[150];
void exp()
{
top=-1;
}
void input()
{
cout<<"enter expresion without space = ";
cin>>st;
}
void scan(void);
void push(void);
void pop(void);
int main()
{
exp() ;
input();
scan();
getch();
}
void push(void)
{
top=top+1;
stack [top]=ch;
}
void pop()
{
while(top!=-1)
{
if(ch==')')
{
cout<<stack[top]<<" ";
top--;
if(stack[top]=='(')
{
top--;
break;
}
}
else
{
cout<<stack[top]<<" ";
top--;
if(stack[top]=='(')break;
}
}
}
void scan(void)
{
for(int i=0;st[i]!=' ';i++)
{
ch=st[i];
if(ch=='('||ch=='*'||ch=='/'||ch=='+'||ch=='-'||ch==')')
{
if(ch=='(')
push();
else if(stack[top]=='(')
pop();
else if(ch==')')
pop();
else if((stack[top]=='+'||stack[top]=='-')&&(ch=='*'||ch=='/'))
push();
else if(top==-1)
push();
else
{
pop();
push();
}
}
else
cout<<ch<<" ";
}
pop();
}
Mar 9, 2011 at 4:01pm UTC
As a start, you could establish a relationship with the spacebar on your keyboard. Everything rammed together with only minimal spacing is impossible to read. Also, indention is important. And remove the global variables.
Mar 9, 2011 at 4:14pm UTC
I think the forum eats the spaces when you don't use [ code] [ /code] tags. :) Which one should arguably do.
Mar 9, 2011 at 4:18pm UTC
only at the start of the lines
:0(
else if((stack[top]=='+'||stack[top]=='-')&&(ch=='*'||ch=='/'))
:0|
else if ( (stack[top] == '+' || stack[top] == '-' ) && (ch == '*' || ch == '/' ) )
:0)
else if ( (stack[top] == '+' || stack[top] == '-' ) && (ch == '*' || ch == '/' ) )
Last edited on Mar 9, 2011 at 4:19pm UTC