Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct binary_tree bt;
- typedef struct stack stack;
- typedef struct node node;
- struct node {
- char sequence;
- node *next;
- };
- struct stack {
- node *top;
- };
- stack *create_stack() {
- stack *new_stack = (stack*) malloc(sizeof(stack));
- new_stack -> top = NULL;
- return new_stack;
- }
- void push(stack *stack, char sequence) {
- node *new_top = (node*) malloc(sizeof(node));
- new_top -> sequence = sequence;
- new_top -> next = stack -> top;
- stack -> top = new_top;
- return;
- }
- int pop(stack *stack) {
- if(stack -> top == NULL) {
- //printf("Stack is underflow.\n");
- return -1; // error code
- }
- else {
- //printf("%s\n", stack -> top -> sequence);
- stack -> top = stack -> top -> next;
- return 0; // success code
- }
- }
- struct binary_tree {
- int item;
- bt *right;
- bt *left;
- };
- bt *create_empty_binary_tree() {
- return NULL;
- }
- bt *create_binary_tree(int item, bt *left, bt *right) {
- bt *new_bt = (bt *) malloc(sizeof(bt));
- new_bt -> item = item;
- new_bt -> left = left;
- new_bt -> right = right;
- return new_bt;
- }
- void print_pre_order(bt *bt) {
- if(bt != NULL) {
- printf("%d\n", bt -> item);
- print_pre_order(bt -> left);
- print_pre_order(bt -> right);
- }
- }
- bt *read(bt *b_t, char tree[], int *i) {
- if(tree[*i] == ')') {
- if(tree[*i - 1] != '(') {
- return b_t;
- }
- return NULL;
- }
- else if(tree[*i] == '\n' || tree[*i] == '(' || tree[*i] == ' ') {
- *i += 1;
- read(b_t, tree, i);
- }
- else {
- int item = 0, multiplier = 1;
- while(tree[*i] != '\n' && tree[*i] != '(' && tree[*i] != ')' && tree[*i] != ' ') {
- item = (item * multiplier) + (tree[*i] - '0');
- multiplier = 10;
- *i += 1;
- }
- bt *new_bt = create_binary_tree(item, NULL, NULL);
- new_bt -> left = read(new_bt, tree, i);
- *i += 1;
- new_bt -> right = read(new_bt, tree, i);
- *i += 1;
- return new_bt;
- }
- }
- int path(bt *bt, int num, int sum) {
- if(bt == NULL) {
- if(num == sum) {
- return 1;
- }
- return 0;
- }
- else {
- int i = path(bt -> left, num, sum + bt -> item);
- int j = path(bt -> right, num, sum + bt -> item);
- if(i > j) {
- return i;
- }
- else {
- return j;
- }
- }
- }
- void multiples_entries() {
- int num;
- scanf("%d", &num);
- printf("%d ", num);
- if(num == -1000) {
- printf("\n");
- return;
- }
- else {
- int i = 0;
- char character, input[1000];
- stack *stack = create_stack();
- while(1) {
- scanf("%c", &character);
- if(character != '\0' && character != ' ') {
- input[i++] = character;
- if(character == '(') {
- push(stack, '(');
- }
- else if(character == ')') {
- pop(stack);
- if(stack -> top == NULL) {
- break;
- }
- }
- }
- }
- input[i++] = '\0';
- //int aux = 0;
- //int *j = &aux;
- //bt *bt = create_empty_binary_tree();
- //bt = read(bt, input, j);
- /*if(path(bt, num, 0)) {
- printf("sim\n");
- }
- else {
- printf("nao\n");
- }*/
- //print_pre_order(bt);
- printf("%s\n", input);
- multiples_entries();
- }
- }
- int main() {
- multiples_entries();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement