Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1.  
  2. #include "expeval.h"
  3.  
  4. void InfixToPostfix(char *infixstr, char *postfixstr) {
  5. stack *top = NULL;
  6. char *p = infixstr, *p2 = postfixstr, temp;
  7. strcat(infixstr,")");
  8. push(&top, '(');
  9.  
  10. while(*p != '\0'){
  11. if(*p == '('){
  12. push(&top, *p);
  13. } else if (isdigit(*p) || isalpha(*p)){
  14. *p2 = *p;
  15. p2++;
  16. } else if (*p == ')'){
  17. temp = peek(top);
  18. pop(&top);
  19. while(temp != '('){
  20. *p2 = temp;
  21. p2++;
  22. temp = peek(top);
  23. pop(&top);
  24. }
  25. } else if (isoper(*p)){
  26. temp = peek(top);
  27. pop(&top);
  28. while(isoper(temp) && getPriority(temp) > getPriority(*p)){
  29. *p2 = temp;
  30. p2++;
  31. temp = peek(top);
  32. pop(&top);
  33. }
  34. push(&top, temp);
  35. push(&top, *p);
  36. }
  37. p++;
  38. }
  39.  
  40. while(top != NULL){
  41. temp = peek(top);
  42. pop(&top);
  43. *p2 = temp;
  44. p2++;
  45. }
  46. }
  47.  
  48. int evaluatePostfix(char *exp) {
  49. stack *top = NULL;
  50. char *p = exp;
  51. int a, b;
  52. strcat(exp,")");
  53.  
  54. while(*p != '\0'){
  55. if(isdigit(*p) || isalpha(*p)) push(&top, *p - '0');
  56. else if (isoper(*p)){
  57. a = peek(top);
  58. pop(&top);
  59. b = peek(top);
  60. pop(&top);
  61. if(*p == '^' ){
  62. push(&top, pow(b,a));
  63. } else if (*p == '*'){
  64. push(&top, b * a);
  65. } else if (*p == '/'){
  66. push(&top, b / a);
  67. } else if (*p == '+') {
  68. push(&top, b + a);
  69. } else if (*p == '-') {
  70. push(&top, b - a);
  71. }
  72. }
  73. p++;
  74. }
  75. return peek(top);
  76. }
  77.  
  78. int getPriority(char op) {
  79. if (op == '/' || op == '*' || op == '%') return 1;
  80. else if (op == '+' || op == '-') return 0;
  81. return 0;
  82. }
  83.  
  84. //A function to act like isdigit() or isalpha() to tell is a character is an operator
  85. int isoper(char c){
  86. if(c == '^' || c == '*' || c == '/' || c == '+' || c == '-') return 1;
  87. else return 0;
  88. }
  89.  
  90. -------------------------------------------------------------------------------
  91.  
  92. #include "stack.h"
  93.  
  94. void push(stack **topp, int val) {
  95. stack *ptr = (stack *)malloc(sizeof(stack));
  96. ptr-> data = val;
  97.  
  98. if (*topp == NULL) {
  99. ptr->next = NULL;
  100. *topp = ptr;
  101. } else {
  102. ptr -> next = *topp;
  103. *topp = ptr;
  104. }
  105. }
  106.  
  107. void pop(stack **topp) {
  108. if (*topp == NULL) {
  109. printf("STACK UNDERFLOW \n");
  110. } else {
  111. stack *temp = *topp;
  112. *topp = temp->next;
  113. free(temp);
  114. }
  115. }
  116.  
  117. int peek(stack *top) {
  118. if (top == NULL) {
  119. printf("STACK IS EMPTY \n");
  120. return -1;
  121. }
  122. else {
  123. return top->data;
  124. }
  125. }
  126.  
  127. void clean(stack **topp) {
  128. node *temp, *np = *topp;
  129. while (np != NULL) {
  130. temp = np;
  131. np = np->next;
  132. free(temp);
  133. }
  134. *topp = NULL;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement