Aniket_Goku

Evo of prefix expression

Sep 3rd, 2020 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. //evolution of prefix expression
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<MATH.h>
  5. #include<string.h>
  6. char prefix[50],tp[50];
  7. long int stack[50],top= (-1),ans,a,b;
  8. void alert()
  9. {
  10. printf("\nþûþ notice :==\n \"If you Enter NUMBER then Enter the space after the number is complete\" \n LIKe : \"+3 33\" ");
  11. }
  12. void push(long int ch)
  13. {
  14. top++;
  15. stack[top]=ch;
  16. }
  17. long int pop()
  18. {
  19. long int ch;
  20. ch=stack[top];
  21. top--;
  22. return ch;
  23. }
  24. int main()
  25. {
  26. long int i,j,ans,a,b,amt,l;
  27. char c[20];
  28. clrscr();
  29. alert();
  30. printf("\n ENter the prefix Expression: ");
  31. gets(prefix);
  32. strrev(prefix);
  33. for(i=0;i<strlen(prefix);i++)
  34. {
  35. j=0;
  36. while(prefix[i]>=48 && prefix[i]<=57)
  37. {
  38. c[j]=prefix[i];
  39. i++;
  40. j++;
  41.  
  42. }
  43. c[j]=NULL;
  44. strrev(c);
  45.  
  46. amt=atoi(c);
  47.  
  48. if(amt!=0)
  49. {
  50. printf("\nNO ==> %ld",amt);
  51. push(amt);
  52. }
  53.  
  54. switch(prefix[i])
  55. {
  56. case ' ':
  57. break;
  58.  
  59. case '^':
  60. a=stack[top];
  61. top--;
  62. b=stack[top];
  63. ans=pow(a,b);
  64. stack[top]=ans;
  65. break;
  66.  
  67. case '*':
  68. a=stack[top];
  69. top--;
  70. b=stack[top];
  71. ans=a*b;
  72. stack[top]=ans;
  73. break;
  74.  
  75. case '/':
  76. a=stack[top];
  77. top--;
  78. b=stack[top];
  79. ans=a/b;
  80. stack[top]=ans;
  81. break;
  82.  
  83. case '+':
  84. a=stack[top];
  85. top--;
  86. b=stack[top];
  87. ans=a+b;
  88. stack[top]=ans;
  89. break;
  90.  
  91. case '-':
  92. a=stack[top];
  93. top--;
  94. b=stack[top];
  95. ans=a-b;
  96. stack[top]=ans;
  97. break;
  98.  
  99. }
  100.  
  101. }
  102. printf("\nEVO of PREFIX expression ==> %ld",stack[top]);
  103. getch();
  104.  
  105. return 0;
  106. }
Add Comment
Please, Sign In to add comment