Sie sind auf Seite 1von 3

#include<stdio.

h>
#include<conio.h>
#include<string.h>
int i=-1,outcnt=-1;
char p[100],x;
char out[100];
void push(char ch)
{
i++;
p[i]=ch;
}
char pop()
{
char a;
a=p[i];
i--;
return a;
}
int isp(char x)
{
int sp;
switch(x)
{
case '+':
case '-':
sp=2;
break;
case '*':
case '/':
sp=4;
break;
case '^':
sp=5;
break;
case '(':
sp=0;
break;
case ')':
sp=-1;
break;
default:
sp=8;
}
return sp;
}

int icp(char item)


{
int cp;
switch(item)
{
case '+':
case '-':
cp=1;
break;
case '*':
case '/':
cp=3;
break;
case '^':
cp=6;
break;
case '(':
cp=9;
break;
case ')':
cp=0;
break;
default:
cp=7;
}
return cp;
}

void main()
{
char item,str[100];
int strcnt,sp,cp;
clrscr();
printf("\nENter the char: ");
fflush(stdin);
gets(str);
printf("\nString is %d",strlen(str));
if(str[0]!='(')
{
printf("\nInvalid string\n");
getch();
exit(0);
}
item=str[0];
push(item);
strcnt=1;
while(strcnt<strlen(str))
{
item=str[strcnt];
if((item>='a' && item<='z') || (item>='A' && item<='0') || (item
>='a' && item<='9'))
{
outcnt++;
out[outcnt]=item;
}
else if(item==')')
{
while(p[i]!='(')
{
x=pop();
outcnt++;
out[outcnt]=x;
printf("\n%c",x);
}
x=pop();
}
else
{
x=pop();
sp=isp(x);
cp=icp(item);
if(sp<cp)
{
push(x);
push(item);
}
else
{
outcnt++;
out[outcnt]=x;
x=pop();
sp=isp(x);
while(sp>=cp)
{
outcnt++;
out[outcnt]=x;
x=pop();
sp=isp(x);
}
if(sp<cp)
{
push(x);
push(item);
}
}
}
strcnt++;
}
printf("\nString is %s",out);
getch();
}

Das könnte Ihnen auch gefallen