Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- struct var {
- string name;
- string n;
- };
- struct node {
- var info;
- node* next;
- };
- void add(node*& head, var n) {
- node* q = new node;
- q->next = NULL;
- q->info = n;
- if (head == NULL) {
- head = q;
- }
- else {
- node* p = head;
- while (p->next != NULL) {
- p = p->next;
- }
- p->next = q;
- }
- }
- void ch(node*& head, var n) {
- node* p = head;
- bool b = false;
- while (p && !b) {
- if (p->info.name == n.name) {
- p->info.n = n.n;
- b = true;
- }
- p = p->next;
- }
- if (!b)
- add(head, n);
- }
- string find(string& name, node*& head) {
- node* p = head;
- while (p) {
- if (p->info.name == name) {
- return p->info.n;
- }
- }
- return "-";
- }
- int defop(char g) {
- switch (g) {
- case '|': // диз
- return 0;
- break;
- case '&': // кон
- return 1;
- break;
- case '+': // слож
- return 2;
- break;
- case '-': // имп
- return 3;
- break;
- case '*': // шеф
- return 4;
- break;
- case ';':
- return 5;
- break;
- case ' ':
- return 5;
- break;
- default:
- return -1;
- }
- }
- bool f(node*& vars) {
- ifstream in("in.txt");
- int condition = 0;
- int op = 0;
- char c;
- string name;
- int idxname = 0;
- string n;
- int idxn = 0;
- string n1;
- int idxn1 = 0;
- string name1;
- while (in >> c) {
- switch (condition) {
- case 0:
- if (isdigit(c) || isalpha(c)) {
- if (idxname == 0 && isdigit(c))
- condition = -1;
- else {
- name += c;
- }
- }
- else if (c == ':')
- condition = 1;
- else
- condition = -1;
- break;
- case 1:
- if (c == '=')
- condition = 1;
- else if (isalpha(c)) {
- condition = 2;
- name1 = c;
- }
- else if (c == '0' || c == '1') {
- condition = 3;
- idxn = 1;
- n = c;
- }
- else
- condition = -1;
- break;
- case 2:
- if (isalpha(c) || isdigit(c)) {
- name1 += c;
- }
- else if (c == ' ' || c == ';') {
- n = find(name1, vars);
- if (n == "-")
- condition = -1;
- else {
- var t;
- t.n = n;
- t.name = name;
- name = "";
- idxname = 0;
- ch(vars, t);
- condition = 0;
- }
- }
- else {
- op = defop(c);
- if (op == -1)
- condition = -1;
- else
- condition = 4;
- }
- break;
- case 3:
- if (idxn < 8) {
- if (c == '0' || c == '1') {
- idxn++;
- n += c;
- }
- else
- condition = -1;
- }
- else {
- if (c == '0' || c == '1')
- condition = -1;
- else {
- op = defop(c);
- if (op == -1)
- condition = -1;
- else if (op == 5) {
- var t;
- t.n = n;
- t.name = name;
- name = "";
- idxname = 0;
- ch(vars, t);
- condition = 0;
- }
- else
- condition = 4;
- }
- }
- break;
- case 4:
- if (isalpha(c)) {
- condition = 5;
- name1 = c;
- }
- else if (c == '0' || c == '1') {
- condition = 6;
- idxn1 = 1;
- n1 = c;
- }
- else
- condition = -1;
- break;
- case 5:
- if (isalpha(c) || isdigit(c)) {
- name1 += c;
- }
- else if (c == ' ' || c == ';') {
- n1 = find(name1, vars);
- if (n1 == "-")
- condition = -1;
- else {
- string ans = "00000000";
- for (int i = 0; i < 8; ++i) {
- int a = (n[i] - '0'), b = (n1[i] - '0');
- switch (op) {
- case 0:
- ans[i] = (a || b) + '0';
- break;
- case 1:
- ans[i] = (a && b) + '0';
- break;
- case 2:
- ans[i] = ((a + b) % 2) + '0';
- break;
- case 3:
- ans[i] = (!(a && !b)) + '0';
- break;
- case 4:
- ans[i] = (!(a && b)) + '0';
- break;
- }
- }
- var gh;
- gh.n = ans;
- gh.name = name;
- name = "";
- idxname = 0;
- ch(vars, gh);
- condition = 0;
- }
- }
- else
- condition = -1;
- break;
- case 6:
- if (idxn1 < 8) {
- if (c == '0' || c == '1') {
- idxn1++;
- n1 += c;
- }
- else
- condition = -1;
- }
- else {
- if (c == '0' || c == '1')
- condition = -1;
- else {
- string ans = "00000000";
- for (int i = 0; i < 8; ++i) {
- int a = (n[i] - '0'), b = (n1[i] - '0');
- switch (op) {
- case 0:
- ans[i] = (a || b) + '0';
- break;
- case 1:
- ans[i] = (a && b) + '0';
- break;
- case 2:
- ans[i] = ((a + b) % 2) + '0';
- break;
- case 3:
- ans[i] = (!(a && !b)) + '0';
- break;
- case 4:
- ans[i] = (!(a && b)) + '0';
- break;
- }
- }
- var gh;
- gh.n = ans;
- gh.name = name;
- name = "";
- idxname = 0;
- ch(vars, gh);
- condition = 0;
- }
- }
- }
- if (condition == -1)
- return false;
- }
- return true;
- }
- void pr(node* head) {
- while (head) {
- cout << head->info.name << '=' << head->info.n << '\n';
- head = head->next;
- }
- }
- int main()
- {
- node* vars = NULL;
- if (f(vars)) {
- pr(vars);
- }
- else {
- cout << "syntax error";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement