Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- bool is_operator(char c) {
- return c == '+' || c == '-' || c == '*';
- }
- typedef struct Tree_Node Tree_Node;
- struct Tree_Node {
- char label;
- Tree_Node* left;
- Tree_Node* right;
- bool value;
- };
- Tree_Node* new_Tree_Node(char label) {
- Tree_Node* root = (Tree_Node*) malloc(sizeof(Tree_Node));
- root->label = label;
- root->left = nullptr;
- root->right = nullptr;
- root->value = (int) NULL;
- // if(!is_operator(label))
- // root->value = label-'0';
- root->value = false;
- return root;
- }
- void inorder(Tree_Node* root) {
- if(root == NULL)
- return;
- inorder(root->left);
- printf("%c ", root->label);
- inorder(root->right);
- }
- long long gcd(long long a, long long b) {
- if(a == b)
- return a;
- long long bigger = max(a,b);
- long long smaller = min(a,b);
- if(bigger % smaller == 0)
- return smaller;
- return gcd(smaller, bigger % smaller);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- bool arr_bool[27];
- for(int i = 65; i < 65+n; i++) {
- char c;
- cin >> c;
- arr_bool[i - 65] = c == 'T';
- }
- string postfix_str;
- getline(cin, postfix_str);
- getline(cin, postfix_str);
- // cout << postfix_str << endl;
- // cout <<" end";
- int post_fix_size = 0;
- int postfix_ind = 0;
- char postfix[250];
- // cout << "printing postfix_str:\n";
- // cout << postfix_str;
- // for(char c = po)
- for(char i : postfix_str) {
- if(i != ' ') {
- postfix[postfix_ind] = i;
- postfix_ind++;
- post_fix_size++;
- }
- }
- // cout << "printing postfix:\n";
- // for(int i = 0; i < post_fix_size; i++)
- // cout << postfix[i];
- stack<Tree_Node*> nodes;
- for(int i = 0; i < post_fix_size; i++) {
- char current_char = postfix[i];
- if(!is_operator(current_char)) {
- Tree_Node* t = new_Tree_Node(current_char);
- t->value = arr_bool[current_char - 65];
- nodes.push(t);
- }
- else {
- if(current_char != '-') {
- Tree_Node* t = new_Tree_Node(current_char);
- Tree_Node* t1 = nodes.top();
- nodes.pop();
- Tree_Node* t2 = nodes.top();
- nodes.pop();
- t->left = t2;
- t->right = t1;
- switch(current_char) {
- case '+': {
- bool res = t1->value | t2->value;
- t->value = res;
- break;
- }
- case '*': {
- bool res = t1->value & t2->value;
- t->value = res;
- break;
- }
- }
- nodes.push(t);
- }
- else {
- Tree_Node* t1 = nodes.top();
- t1->value = !(t1->value);
- // Tree_Node *t = new_Tree_Node(current_char); //new node with operator
- // Tree_Node *t1 = nodes.top(); //pop the last two values
- // nodes.pop();
- // t->value = !(t->value);
- // nodes.push(t);
- }
- }
- }
- // cout << " IDEMO ODMA \n";
- Tree_Node* t = nodes.top();
- // inorder(t);
- // cout << "endl\n";
- // cout << "label is " << t->label;
- if(!t->value) {
- cout <<"F";
- return 0;
- }
- cout << "T";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement