fork download
  1. #include<math.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<ctype.h>
  6. #define MAX 300
  7.  
  8. struct stack {
  9. char alpha;
  10. struct stack *nxtPTR;
  11.  
  12. };
  13.  
  14. typedef struct stack Stack;
  15. typedef Stack *node;
  16. void message(void);
  17. char out_print(char word[]);
  18. int precedence_power(int res_1, int res_2);
  19. int pop(node *topPtr);
  20. void push_stack(node *topPTR, char value);
  21. int pop(node *topPTR);
  22. char check_stack(node data);
  23. int isOperator(char c);
  24. int precedence(char data_1, char data_2, int(intro_precedence_power)(int res_1, int res_2));
  25. void converting(char *in, char *out, node *PTR, char (checking_stack)(node), void (push)(node *topPTR, char value), int (pop)(node *fix), int (isOper)(char c), int (precedence_intro)(char data_1, char data_2, int(intro_precedence_power)(int res_1, int res_2)), int(intro_precedence_power)(int res_1, int res_2));
  26. void please_enter(void );
  27.  
  28. int main(void) {
  29.  
  30. char infix[MAX];
  31. char postfix[MAX];
  32. node topPTR = NULL;
  33.  
  34. fgets(infix, sizeof(infix), stdin);
  35. int m = strlen(infix);
  36.  
  37. infix[m] = ')';
  38.  
  39. memset(postfix, 0, MAX);
  40.  
  41.  
  42.  
  43.  
  44. converting( infix, postfix, &topPTR, check_stack, push_stack, pop, isOperator, precedence, precedence_power);
  45. out_print(postfix);
  46.  
  47. puts(" ");
  48.  
  49. return 0;
  50. }
  51.  
  52. char out_print(char word[]) {
  53.  
  54. if( word[0] != '\0' ){
  55. printf( "%c " , word[0] ) ;
  56. return out_print(word + 1 ) ;
  57. }
  58.  
  59. }
  60.  
  61. void push_stack(node *topPTR, char value) {
  62. node newPTR = malloc(sizeof(Stack));
  63.  
  64. if (newPTR != NULL) {
  65.  
  66. newPTR->alpha = value;
  67. newPTR->nxtPTR = *topPTR;
  68.  
  69. *topPTR = newPTR;
  70. }
  71.  
  72. else {
  73.  
  74. puts("error");
  75.  
  76. }
  77. }
  78.  
  79. int pop(node *fix) {
  80.  
  81. int value = (*fix)->alpha;
  82.  
  83. node temp = *fix;
  84. *fix = (*fix)->nxtPTR;
  85. free(temp);
  86.  
  87. return value;
  88. }
  89.  
  90. char check_stack(node data) {
  91. return data->alpha;
  92. }
  93.  
  94. int isOperator(char c) {
  95. return c == '/' || c == '*' || c == '-' || c == '+' || c == '^' ;
  96. }
  97.  
  98. void converting(char *in, char *out, node *PTR, char (checking_stack)(node), void (push)(node *topPTR, char value), int (pop)(node *fix), int (isOper)(char c), int (precedence_intro)(char data_1, char data_2, int(intro_precedence_power)(int res_1, int res_2)), int(intro_precedence_power)(int res_1, int res_2)) {
  99.  
  100. int k = 0, j = 0, d = 0;
  101.  
  102. push(PTR, '(');
  103.  
  104. for (k = 0; checking_stack((node)PTR) != 0; k++) {
  105.  
  106. if (isdigit(in[k])) {
  107.  
  108. out[j++] = in[k];
  109.  
  110. }
  111.  
  112. if (in[k] == '(') {
  113.  
  114. push(PTR, in[k]);
  115.  
  116. }
  117.  
  118. if (isOper(in[k]) == 1) {
  119.  
  120. while (precedence_intro((*PTR)->alpha, in[k], intro_precedence_power) != -1) {
  121.  
  122. out[j++] = pop(PTR);
  123. }
  124.  
  125. push(PTR, in[k]);
  126. }
  127.  
  128. if (in[k] == ')') {
  129.  
  130. d = pop(PTR);
  131. for (; d != '('; d = pop(PTR)) {
  132. out[j++] = d;
  133. }
  134. }
  135. }
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142. int precedence(char data_1, char data_2, int(intro_precedence_power)(int res_1, int res_2)) {
  143. char collection[] = "+1-1*2/2^3";
  144.  
  145. char buf_1 = (char)strcspn( collection , &data_1) + 1;
  146. char buf_2 = (char)strcspn(collection, &data_2) + 1;
  147.  
  148. return intro_precedence_power(atoi(&collection[buf_1]), atoi(&collection[buf_2]));
  149. }
  150.  
  151. int precedence_power(int res_1, int res_2) {
  152. if (res_1 < res_2) {
  153. return -1;
  154. }
  155. else if (res_1 == res_2) {
  156. return 0;
  157. }
  158. else if (res_1 > res_2) {
  159. return 1;
  160. }
  161. return 0;
  162. }
Success #stdin #stdout 0s 9432KB
stdin
13+7*/3
stdout
1 3 7 + * 3 /