Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*построить дерево бинарного разбора выражения (f+1)*(6/(1+g)+4) */
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string.h>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- struct Btree
- {
- char data[10];
- Btree* left;
- Btree* right;
- };
- int priority(char c) //функция определяет приоритет операции или возвращает -1 если это число
- {
- switch (c)
- {
- case'+':
- case'-':
- return 1;
- case'*':
- case'/':
- return 2;
- }
- return 100;
- }
- void PrintTree(Btree* tree, int space)
- {
- const int COUNT = 5;
- if (tree != NULL)
- {
- space += COUNT;
- PrintTree(tree->right, space);
- for (int i = COUNT; i < space; i++)
- printf("%c", ' ');
- printf_s("%s\n", tree->data);
- PrintTree(tree->left, space);
- }
- }
- int its_a_number(Btree* tree)
- {
- int i = 0;
- if (!tree) return 0;
- while (tree->data[i])
- if (!strchr("-0123456789", tree->data[i++]))
- return 0;
- return 1;
- }
- void calculate(Btree* tree)
- {
- while (tree->left->left != NULL && tree->left != NULL)
- calculate(tree->left);
- while (tree->right->right != NULL && tree->right != NULL)
- calculate(tree->right);
- int a, b, c = 0;
- if (!tree || !its_a_number(tree->left) || !its_a_number(tree->right)) return;
- a = atoi(tree->left->data);
- b = atoi(tree->right->data);
- switch (tree->data[0])
- {
- case'+':c = a + b; break;
- case'-':c = a - b; break;
- case'*':c = a * b; break;
- case'/':c = a / b; break;
- }
- free(tree->left);
- free(tree->right);
- sprintf(tree->data, "%d", c);
- cout << c << endl;
- tree->left = NULL;
- tree->right = NULL;
- }
- Btree* let_it_tree(char term[], int first, int last)
- {
- Btree* tree = (Btree*)malloc(sizeof(Btree));
- char c;
- int min, i, k, prt;
- int nest = 0;
- if (first == last || first == (last - 1))
- {
- tree->data[0] = term[first];
- tree->data[1] = '\0';
- tree->left = NULL;
- tree->right = NULL;
- return tree;
- }
- min = 100;
- for (i = first; i < last; i++)
- {
- c = term[i];
- if (c == '(') { nest++; continue; }
- if (c == ')') { nest--; continue; }
- if (nest > 0) continue;
- prt = priority(c);
- if (prt <= min)
- {
- min = prt;
- k = i;
- }
- }
- if (min == 100 && term[first] == '(' && (term[last] == ')' || term[last] == '\0'))
- return let_it_tree(term, first + 1, last - 1);
- else
- {
- tree->data[0] = term[k];
- tree->data[1] = '\0';
- tree->left = let_it_tree(term, first, k - 1);
- tree->right = let_it_tree(term, k + 1, last);
- return tree;
- }
- }
- void genrand(int* x)
- {
- for (int i = 0; i < 10; i++)
- {
- x[i] = rand() % 15;
- cout.width(5);
- cout<< x[i];
- }
- cout<< endl;
- }
- void Tree2(Btree* r, int n, int m)//обратный
- {
- if (r != NULL)
- {
- Tree2(r->left, n, m);
- if (r->data[0] == 'a')
- {
- sprintf(r->data, "%d", n);
- }
- if (r->data[0] == 'b')
- {
- sprintf(r->data, "%d", m);
- }
- Tree2(r->right, n, m);
- }
- }
- int main()
- {
- srand(time(NULL));
- int* n = (int*)malloc(10 * sizeof(int));
- int* m = (int*)malloc(10 * sizeof(int));
- Btree* tree = (Btree*)malloc(sizeof(Btree));
- char* term = (char*)malloc(30 * sizeof(char));
- cout << "Input your fucntion" << endl;
- cin>> term;
- tree = let_it_tree(term, 0, strlen(term));
- PrintTree(tree, 10);
- cout<< "n is ";
- genrand(n);
- cout<< "m is ";
- n[0] = 7;
- genrand(m);
- Tree2(tree, n[0], m[0]);
- PrintTree(tree, 10);
- cout<< "result is "<< endl;
- calculate(tree);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement