Guest User

Untitled

a guest
Oct 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. #define SIZE 50 /* Size of Stack */
  7.  
  8. double s[SIZE];
  9. int peak=-1; /* Global declarations */
  10. char pofx[50];
  11.  
  12. double pop()
  13. { /* Function for POP operation */
  14. return(s[peak--]);
  15. }
  16.  
  17. double push(double elem) {
  18. if (peak + 1 >= SIZE) {
  19. printf("Stack overflown");
  20. return 0;
  21. }
  22. s[++peak] = elem;
  23. }
  24.  
  25. void main()
  26. { /* Main Program */
  27. printf("Enter the Postfix Expression:");
  28. // fgets(pofx,100,stdin); // 100??
  29. fgets(pofx, sizeof pofx, stdin); // better
  30. postfixtoresult();
  31. printf("Result: %lfn",s[peak]);
  32. }
  33.  
  34. void postfixtoresult()
  35. {
  36. int i=0;
  37. const char *st = pofx;
  38.  
  39. while (*st) {
  40. char *end; //location to store end of FP parsing
  41. double value = strtod(st, &end);
  42. if (end > st) {
  43. push(value);
  44. st = end;
  45. } else if (isspace((unsigned char) *st)) {
  46. st++;
  47. } else {
  48. switch (*st) {
  49. case '+':push(pop() + pop());break; // pop order irrelevant
  50. case '-':{ double t = pop(); push(pop() - t);break; } // pop order relevant
  51. case '*':push(pop() * pop());break; // pop order irrelevant
  52. case '/':{ double u = pop(); push(pop() / u);break; } // pop order relevant
  53. case '^':{ double v = pop(); push(pow(pop(),v));break; } // pop order relevant
  54.  
  55. default: {
  56. printf("Input invalid operator: character code %dn", *st);
  57. return 0;
  58. }
  59. } // end switch
  60. st++;
  61. }
  62. }
  63. }
Add Comment
Please, Sign In to add comment