#include<stdio.h>
#include<conio.h>
void main()
{
char q[30],s[30],p[30];
int i,top=0,a=0;
clrscr();
printf("\n enter your expretion :");
scanf("%s",q);
strcat(q,")");
s[top]='(';
for(i=0;q[i];i++)
{
switch(q[i])
{
case '(':
top++;
s[top]=q[i];
break;
case '+':
case '-':
while(s[top]=='+'||s[top]=='-'||s[top]=='*'||s[top]=='/')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '*':
case '/':
while(s[top]=='*'||s[top]=='/'||s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '^':
while(s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case ')':
while(s[top]!='(')
{
p[a]=s[top];
top--;
a++;
}
break;
default:
p[a]=q[i];
a++;
}
}
for(i=0;i<a;i++)
printf("%c",p[i]);
getch();
}
#include<conio.h>
void main()
{
char q[30],s[30],p[30];
int i,top=0,a=0;
clrscr();
printf("\n enter your expretion :");
scanf("%s",q);
strcat(q,")");
s[top]='(';
for(i=0;q[i];i++)
{
switch(q[i])
{
case '(':
top++;
s[top]=q[i];
break;
case '+':
case '-':
while(s[top]=='+'||s[top]=='-'||s[top]=='*'||s[top]=='/')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '*':
case '/':
while(s[top]=='*'||s[top]=='/'||s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '^':
while(s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case ')':
while(s[top]!='(')
{
p[a]=s[top];
top--;
a++;
}
break;
default:
p[a]=q[i];
a++;
}
}
for(i=0;i<a;i++)
printf("%c",p[i]);
getch();
}
No comments:
Post a Comment