Advertisement
w3ntz

Untitled

Jun 8th, 2025
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. struct var {
  6.     string name;
  7.     string n;
  8. };
  9. struct node {
  10.     var info;
  11.     node* next;
  12. };
  13. void add(node*& head, var n) {
  14.     node* q = new node;
  15.     q->next = NULL;
  16.     q->info = n;
  17.     if (head == NULL) {
  18.         head = q;
  19.     }
  20.     else {
  21.         node* p = head;
  22.         while (p->next != NULL) {
  23.             p = p->next;
  24.         }
  25.         p->next = q;
  26.     }
  27. }
  28. void ch(node*& head, var n) {
  29.     node* p = head;
  30.     bool b = false;
  31.     while (p && !b) {
  32.         if (p->info.name == n.name) {
  33.             p->info.n = n.n;
  34.             b = true;
  35.         }
  36.         p = p->next;
  37.     }
  38.     if (!b)
  39.         add(head, n);
  40. }
  41. string find(string& name, node*& head) {
  42.     node* p = head;
  43.     while (p) {
  44.         if (p->info.name == name) {
  45.             return p->info.n;
  46.         }
  47.     }
  48.     return "-";
  49. }
  50. int defop(char g) {
  51.     switch (g) {
  52.     case '|': // диз
  53.         return 0;
  54.         break;
  55.     case '&': // кон
  56.         return 1;
  57.         break;
  58.     case '+': // слож
  59.         return 2;
  60.         break;
  61.     case '-': // имп
  62.         return 3;
  63.         break;
  64.     case '*': // шеф
  65.         return 4;
  66.         break;
  67.     case ';':
  68.         return 5;
  69.         break;
  70.     case ' ':
  71.         return 5;
  72.         break;
  73.     default:
  74.         return -1;
  75.     }
  76. }
  77. bool f(node*& vars) {
  78.     ifstream in("in.txt");
  79.     int condition = 0;
  80.     int op = 0;
  81.     char c;
  82.     string name;
  83.     int idxname = 0;
  84.     string n;
  85.     int idxn = 0;
  86.     string n1;
  87.     int idxn1 = 0;
  88.     string name1;
  89.     while (in >> c) {
  90.         switch (condition) {
  91.         case 0:
  92.             if (isdigit(c) || isalpha(c)) {
  93.                 if (idxname == 0 && isdigit(c))
  94.                     condition = -1;
  95.                 else {
  96.                     name += c;
  97.                 }
  98.             }
  99.             else if (c == ':')
  100.                 condition = 1;
  101.             else
  102.                 condition = -1;
  103.             break;
  104.         case 1:
  105.             if (c == '=')
  106.                 condition = 1;
  107.             else if (isalpha(c)) {
  108.                 condition = 2;
  109.                 name1 = c;
  110.             }
  111.             else if (c == '0' || c == '1') {
  112.                 condition = 3;
  113.                 idxn = 1;
  114.                 n = c;
  115.             }
  116.             else
  117.                 condition = -1;
  118.             break;
  119.         case 2:
  120.             if (isalpha(c) || isdigit(c)) {
  121.                 name1 += c;
  122.             }
  123.             else if (c == ' ' || c == ';') {
  124.                 n = find(name1, vars);
  125.                 if (n == "-")
  126.                     condition = -1;
  127.                 else {
  128.                     var t;
  129.                     t.n = n;
  130.                     t.name = name;
  131.                     name = "";
  132.                     idxname = 0;
  133.                     ch(vars, t);
  134.                     condition = 0;
  135.                 }
  136.             }
  137.             else {
  138.                 op = defop(c);
  139.                 if (op == -1)
  140.                     condition = -1;
  141.                 else
  142.                     condition = 4;
  143.             }
  144.             break;
  145.         case 3:
  146.             if (idxn < 8) {
  147.                 if (c == '0' || c == '1') {
  148.                     idxn++;
  149.                     n += c;
  150.                 }
  151.                 else
  152.                     condition = -1;
  153.             }
  154.             else {
  155.                 if (c == '0' || c == '1')
  156.                     condition = -1;
  157.                 else {
  158.                     op = defop(c);
  159.                     if (op == -1)
  160.                         condition = -1;
  161.                     else if (op == 5) {
  162.                         var t;
  163.                         t.n = n;
  164.                         t.name = name;
  165.                         name = "";
  166.                         idxname = 0;
  167.                         ch(vars, t);
  168.                         condition = 0;
  169.                     }
  170.                     else
  171.                         condition = 4;
  172.                 }
  173.             }
  174.             break;
  175.         case 4:
  176.             if (isalpha(c)) {
  177.                 condition = 5;
  178.                 name1 = c;
  179.             }
  180.             else if (c == '0' || c == '1') {
  181.                 condition = 6;
  182.                 idxn1 = 1;
  183.                 n1 = c;
  184.             }
  185.             else
  186.                 condition = -1;
  187.             break;
  188.         case 5:
  189.             if (isalpha(c) || isdigit(c)) {
  190.                 name1 += c;
  191.             }
  192.             else if (c == ' ' || c == ';') {
  193.                 n1 = find(name1, vars);
  194.                 if (n1 == "-")
  195.                     condition = -1;
  196.                 else {
  197.                     string ans = "00000000";
  198.                     for (int i = 0; i < 8; ++i) {
  199.                         int a = (n[i] - '0'), b = (n1[i] - '0');
  200.                         switch (op) {
  201.                         case 0:
  202.                             ans[i] = (a || b) + '0';
  203.                             break;
  204.                         case 1:
  205.                             ans[i] = (a && b) + '0';
  206.                             break;
  207.                         case 2:
  208.                             ans[i] = ((a + b) % 2) + '0';
  209.                             break;
  210.                         case 3:
  211.                             ans[i] = (!(a && !b)) + '0';
  212.                             break;
  213.                         case 4:
  214.                             ans[i] = (!(a && b)) + '0';
  215.                             break;
  216.                         }
  217.                     }
  218.                     var gh;
  219.                     gh.n = ans;
  220.                     gh.name = name;
  221.                     name = "";
  222.                     idxname = 0;
  223.                     ch(vars, gh);
  224.                     condition = 0;
  225.                 }
  226.             }
  227.             else
  228.                 condition = -1;
  229.             break;
  230.         case 6:
  231.             if (idxn1 < 8) {
  232.                 if (c == '0' || c == '1') {
  233.                     idxn1++;
  234.                     n1 += c;
  235.                 }
  236.                 else
  237.                     condition = -1;
  238.             }
  239.             else {
  240.                 if (c == '0' || c == '1')
  241.                     condition = -1;
  242.                 else {
  243.                     string ans = "00000000";
  244.                     for (int i = 0; i < 8; ++i) {
  245.                         int a = (n[i] - '0'), b = (n1[i] - '0');
  246.                         switch (op) {
  247.                         case 0:
  248.                             ans[i] = (a || b) + '0';
  249.                             break;
  250.                         case 1:
  251.                             ans[i] = (a && b) + '0';
  252.                             break;
  253.                         case 2:
  254.                             ans[i] = ((a + b) % 2) + '0';
  255.                             break;
  256.                         case 3:
  257.                             ans[i] = (!(a && !b)) + '0';
  258.                             break;
  259.                         case 4:
  260.                             ans[i] = (!(a && b)) + '0';
  261.                             break;
  262.                         }
  263.                     }
  264.                     var gh;
  265.                     gh.n = ans;
  266.                     gh.name = name;
  267.                     name = "";
  268.                     idxname = 0;
  269.                     ch(vars, gh);
  270.                     condition = 0;
  271.                 }
  272.             }
  273.         }
  274.         if (condition == -1)
  275.             return false;
  276.     }
  277.     return true;
  278. }
  279. void pr(node* head) {
  280.     while (head) {
  281.         cout << head->info.name << '=' << head->info.n << '\n';
  282.         head = head->next;
  283.     }
  284. }
  285. int main()
  286. {
  287.     node* vars = NULL;
  288.     if (f(vars)) {
  289.         pr(vars);
  290.     }
  291.     else {
  292.         cout << "syntax error";
  293.     }
  294. }
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement