Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //evolution of prefix expression
- #include<stdio.h>
- #include<conio.h>
- #include<MATH.h>
- #include<string.h>
- char prefix[50],tp[50];
- long int stack[50],top= (-1),ans,a,b;
- void alert()
- {
- printf("\nþûþ notice :==\n \"If you Enter NUMBER then Enter the space after the number is complete\" \n LIKe : \"+3 33\" ");
- }
- void push(long int ch)
- {
- top++;
- stack[top]=ch;
- }
- long int pop()
- {
- long int ch;
- ch=stack[top];
- top--;
- return ch;
- }
- int main()
- {
- long int i,j,ans,a,b,amt,l;
- char c[20];
- clrscr();
- alert();
- printf("\n ENter the prefix Expression: ");
- gets(prefix);
- strrev(prefix);
- for(i=0;i<strlen(prefix);i++)
- {
- j=0;
- while(prefix[i]>=48 && prefix[i]<=57)
- {
- c[j]=prefix[i];
- i++;
- j++;
- }
- c[j]=NULL;
- strrev(c);
- amt=atoi(c);
- if(amt!=0)
- {
- printf("\nNO ==> %ld",amt);
- push(amt);
- }
- switch(prefix[i])
- {
- case ' ':
- break;
- case '^':
- a=stack[top];
- top--;
- b=stack[top];
- ans=pow(a,b);
- stack[top]=ans;
- break;
- case '*':
- a=stack[top];
- top--;
- b=stack[top];
- ans=a*b;
- stack[top]=ans;
- break;
- case '/':
- a=stack[top];
- top--;
- b=stack[top];
- ans=a/b;
- stack[top]=ans;
- break;
- case '+':
- a=stack[top];
- top--;
- b=stack[top];
- ans=a+b;
- stack[top]=ans;
- break;
- case '-':
- a=stack[top];
- top--;
- b=stack[top];
- ans=a-b;
- stack[top]=ans;
- break;
- }
- }
- printf("\nEVO of PREFIX expression ==> %ld",stack[top]);
- getch();
- return 0;
- }
Add Comment
Please, Sign In to add comment