Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include "rpn.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "stack.h"
  5. #include "queue.h"
  6.  
  7. int comp_prec(char op_one, char op_two){
  8. if(op_one == '+' || op_one == '-'){
  9. if(op_two == '('){
  10. return 1;
  11. }
  12. else{
  13. return -1;
  14. }
  15. }
  16. else if(op_one == '*' || op_one == '/'){
  17. if(op_two == '^' || op_two == '*' || op_two == '/'){
  18. return -1;
  19. }
  20. else{
  21. return 1;
  22. }
  23.  
  24. }
  25. else if(op_one == '^'){
  26. return 1;
  27.  
  28. }
  29. else{
  30. return 0;
  31. }
  32. }
  33.  
  34. int check_op(char op){
  35. if(op == '+' || op == '-' || op == '*' || op == '/' || op == '^'){
  36. return 1;
  37. }
  38. else{
  39. return 0;
  40. }
  41. }
  42. int check_num(char num){
  43. if(num == '0' || num == '1' || num == '2' || num == '3' || num == '4' || num == '5' || num == '6' || num == '7' || num == '8' || num == '9'){
  44. return 1;
  45. }
  46. else{
  47. return 0;
  48. }
  49. }
  50. void removeSpaces(char* source){
  51. char* n = source;
  52. char* k = source;
  53. while(*k != 0){
  54. *n = *k++;
  55. if(*n != ' '){
  56. n++;
  57. }
  58. }
  59. *n = 0;
  60. }
  61.  
  62.  
  63. /* convert an infix expression to postfix (rpn) using the */
  64. /* shunting yard algorithm. */
  65. /* return a queue containing the postfix expression. */
  66. /* if an error occurs during evaluation, return silently with NULL. */
  67. Queue *infix_to_postfix(char *expr)
  68. {
  69. int l = 0;
  70. int r = 0;
  71. int err = 0;
  72. int i = 0;
  73. int length;
  74. int j = 0;
  75. int space;
  76. Stack *shuntStack;
  77. Queue *final;
  78. shuntStack = initialise_stack();
  79. final = initialise_queue();
  80. removeSpaces(expr);
  81. length = strlen(expr);
  82.  
  83. for(i = 0; i < length; i++){
  84. if(expr[i] == '('){
  85. l++;
  86. }
  87. else if(expr[i] == ')'){
  88. r++;
  89. }
  90. else if(check_num(expr[i])==0 && expr[i] != ')' && expr[i] != '(' && check_op(expr[i])==0 && expr[i]!='.'){
  91. err = 1;
  92. }
  93. }
  94. if(l!=r){
  95. err = 1;
  96. }
  97.  
  98. if(err == 0){
  99. for(i = 0; i < length; i++) {
  100. if(expr[i] == '('){
  101. push_stack(shuntStack, (void *) &expr[i], 1);
  102. }
  103. else if( (check_op(expr[i])==1) && ((check_num(expr[i-1])==1) || (expr[i-1]==')')) ){
  104. if(shuntStack->head){
  105. if( comp_prec(expr[i],*((char *)peek_stack(shuntStack))) > 0){
  106. push_stack(shuntStack, (void *) &expr[i], 1);
  107. }
  108. else{
  109. while( comp_prec(expr[i], *((char *)peek_stack(shuntStack))) < 0){
  110. push_queue(final,pop_stack(shuntStack),1);
  111. if(!shuntStack->head){
  112. break;
  113. }
  114. }
  115. push_stack(shuntStack, (void *) &expr[i], 1);
  116. }
  117. }
  118. else{
  119. push_stack(shuntStack, (void *) &expr[i], 1);
  120. }
  121. }
  122. else if(expr[i] == ')'){
  123. while(*((char *)peek_stack(shuntStack)) != '('){
  124. push_queue(final, pop_stack(shuntStack),1);
  125. }
  126. pop_stack(shuntStack);
  127. }
  128. else if(expr[i] == ' '){
  129. continue;
  130. }
  131. else{
  132. j=i;
  133. space=1;
  134. if(expr[j+1]== '.' || expr[j] == '+' || expr[j] == '-' || check_num(expr[j+1])==1){
  135. while(1){
  136. space++;
  137. j++;
  138. if(!expr[j+1] || check_op(expr[j+1]) == 1 || expr[j+1] == ')' || expr[j+1] == '('){
  139. break;
  140. }
  141. }
  142. }
  143. push_queue(final,(void *) &expr[i],space);
  144. i+=(space-1);
  145. }
  146. }
  147. while(shuntStack->head){
  148. push_queue(final,pop_stack(shuntStack),1);
  149. if(!shuntStack->head){
  150. break;
  151. }
  152. }
  153. free_stack(shuntStack);
  154. return final;
  155. }
  156. else{
  157. free_stack(shuntStack);
  158. free_queue(final);
  159. return NULL;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement