#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int chartoint(char c)
{
return (c-48);
}
int
operation(int op1,int op2, char op)
{
switch(op)
{
case '+':
return op1+op2;
case '-':
return op1-op2;
case '*':
return op1*op2;
case '/':
return op1/op2;
case '^':
return pow(op1,op2);
}
return -1;
}
int main()
{
int op1,op2,st[20],i=-1,j=-1;
char op,exp[20];
clrscr();
printf("\nEnter prefix
expression\n");
scanf("%s",exp);
for(i=strlen(exp)-1;i>=0;i--)
{
if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/'||exp[i]=='^')
{
op=exp[i];
op1=st[j];
j--;
op2=st[j];
st[j]=operation(op1,op2,op);
}
else
{
j++;
st[j]=chartoint(exp[i]);
}
}
printf("\nThe answer is : %d\n",st[0]);
getch();
return 0;
}
Output:

No comments:
Post a Comment