Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct binary_tree bt;
  7. typedef struct stack stack;
  8. typedef struct node node;
  9.  
  10. struct node {
  11. char sequence;
  12. node *next;
  13. };
  14.  
  15. struct stack {
  16. node *top;
  17. };
  18.  
  19. stack *create_stack() {
  20. stack *new_stack = (stack*) malloc(sizeof(stack));
  21. new_stack -> top = NULL;
  22. return new_stack;
  23. }
  24.  
  25. void push(stack *stack, char sequence) {
  26. node *new_top = (node*) malloc(sizeof(node));
  27. new_top -> sequence = sequence;
  28. new_top -> next = stack -> top;
  29. stack -> top = new_top;
  30. return;
  31. }
  32.  
  33. int pop(stack *stack) {
  34. if(stack -> top == NULL) {
  35. //printf("Stack is underflow.\n");
  36. return -1; // error code
  37. }
  38. else {
  39. //printf("%s\n", stack -> top -> sequence);
  40. stack -> top = stack -> top -> next;
  41. return 0; // success code
  42. }
  43. }
  44.  
  45. struct binary_tree {
  46. int item;
  47. bt *right;
  48. bt *left;
  49. };
  50.  
  51. bt *create_empty_binary_tree() {
  52. return NULL;
  53. }
  54.  
  55. bt *create_binary_tree(int item, bt *left, bt *right) {
  56. bt *new_bt = (bt *) malloc(sizeof(bt));
  57. new_bt -> item = item;
  58. new_bt -> left = left;
  59. new_bt -> right = right;
  60.  
  61. return new_bt;
  62. }
  63.  
  64. void print_pre_order(bt *bt) {
  65. if(bt != NULL) {
  66. printf("%d\n", bt -> item);
  67. print_pre_order(bt -> left);
  68. print_pre_order(bt -> right);
  69. }
  70. }
  71.  
  72. bt *read(bt *b_t, char tree[], int *i) {
  73. if(tree[*i] == ')') {
  74. if(tree[*i - 1] != '(') {
  75. return b_t;
  76. }
  77.  
  78. return NULL;
  79. }
  80. else if(tree[*i] == '\n' || tree[*i] == '(' || tree[*i] == ' ') {
  81. *i += 1;
  82. read(b_t, tree, i);
  83. }
  84. else {
  85. int item = 0, multiplier = 1;
  86.  
  87. while(tree[*i] != '\n' && tree[*i] != '(' && tree[*i] != ')' && tree[*i] != ' ') {
  88. item = (item * multiplier) + (tree[*i] - '0');
  89.  
  90. multiplier = 10;
  91. *i += 1;
  92. }
  93.  
  94. bt *new_bt = create_binary_tree(item, NULL, NULL);
  95.  
  96. new_bt -> left = read(new_bt, tree, i);
  97. *i += 1;
  98. new_bt -> right = read(new_bt, tree, i);
  99. *i += 1;
  100.  
  101. return new_bt;
  102. }
  103. }
  104.  
  105. int path(bt *bt, int num, int sum) {
  106. if(bt == NULL) {
  107. if(num == sum) {
  108. return 1;
  109. }
  110.  
  111. return 0;
  112. }
  113. else {
  114. int i = path(bt -> left, num, sum + bt -> item);
  115. int j = path(bt -> right, num, sum + bt -> item);
  116.  
  117. if(i > j) {
  118. return i;
  119. }
  120. else {
  121. return j;
  122. }
  123. }
  124. }
  125.  
  126. void multiples_entries() {
  127. int num;
  128.  
  129. scanf("%d", &num);
  130. printf("%d ", num);
  131.  
  132. if(num == -1000) {
  133. printf("\n");
  134. return;
  135. }
  136. else {
  137. int i = 0;
  138. char character, input[1000];
  139. stack *stack = create_stack();
  140. while(1) {
  141. scanf("%c", &character);
  142.  
  143. if(character != '\0' && character != ' ') {
  144. input[i++] = character;
  145.  
  146. if(character == '(') {
  147. push(stack, '(');
  148. }
  149. else if(character == ')') {
  150. pop(stack);
  151.  
  152. if(stack -> top == NULL) {
  153. break;
  154. }
  155. }
  156. }
  157. }
  158.  
  159. input[i++] = '\0';
  160.  
  161. //int aux = 0;
  162. //int *j = &aux;
  163.  
  164. //bt *bt = create_empty_binary_tree();
  165. //bt = read(bt, input, j);
  166.  
  167. /*if(path(bt, num, 0)) {
  168. printf("sim\n");
  169. }
  170. else {
  171. printf("nao\n");
  172. }*/
  173.  
  174. //print_pre_order(bt);
  175. printf("%s\n", input);
  176.  
  177. multiples_entries();
  178. }
  179. }
  180.  
  181. int main() {
  182. multiples_entries();
  183.  
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement