Advertisement
Korotkodul

logic_main.cpp6

Mar 17th, 2025 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <exception>
  4.  
  5. #include <Graph_lib/Graph.h>
  6. #include <Graph_lib/Window.h>
  7. #include <Graph_lib/Simple_window.h>
  8.  
  9. #include "logic.h"
  10. #include "logic_shapes.h"
  11.  
  12.  
  13.  
  14.  
  15. void  Func()
  16. {
  17.     std::string A, B;
  18.     std::cin >> A >> B;
  19.     bool Abit0, Abit1, Abit2, Abit3, Bbit0, Bbit1, Bbit2, Bbit3;
  20.     Abit0 = bool(A[0] - '0');
  21.     Abit1 = bool(A[1] - '0');
  22.     Abit2 = bool(A[2] - '0');
  23.     Abit3 = bool(A[3] - '0');
  24.     Bbit0 = bool(B[0] - '0');
  25.     Bbit1 = bool(B[1] - '0');
  26.     Bbit2 = bool(B[2] - '0');
  27.     Bbit3 = bool(B[3] - '0');
  28.    
  29.     //std::cin >> Abit0 >> Abit1 >> Abit2 >> Abit3 >> Bbit0 >> Bbit1 >> Bbit2 >> Bbit3;
  30.  
  31.     int win_w = 600;
  32.     int win_h = 400;
  33.     Graph_lib::Point lt{ Graph_lib::x_max()/2 - win_w/2, Graph_lib::y_max()/2 - win_h/2 };
  34.     Simple_window win{ lt, win_w  , win_h * 2, "Scheme of Logic Elements" };
  35.    
  36.     using namespace Logic;
  37.  
  38.     Source A0;
  39.     Source A1;
  40.     Source A2;
  41.     Source A3;
  42.     Source B0;
  43.     Source B1;
  44.     Source B2;
  45.     Source B3;
  46.  
  47.     // Заменим And на Xor
  48.     Xor xor0;
  49.     Xor xor1;
  50.     Xor xor2;
  51.     Xor xor3;
  52.  
  53.     // Подключение источников к XOR
  54.     A0 >> xor0;
  55.     B0 >> xor0;
  56.     A1 >> xor1;
  57.     B1 >> xor1;
  58.     A2 >> xor2;
  59.     B2 >> xor2;
  60.     A3 >> xor3;
  61.     B3 >> xor3;
  62.  
  63.     // Объединение XOR
  64.     Or nor{ Out_state::inverted };
  65.  
  66.     xor0 >> nor;
  67.     xor1 >> nor;
  68.     xor2 >> nor;
  69.     xor3 >> nor;
  70.  
  71.     // Создание схемы
  72.     SchemeShape scheme{ Graph_lib::Point{5, 5}, win_w - 80, win_h - 10 };
  73.     win.attach(scheme);
  74.  
  75.     auto column_x = [] (double c) -> int
  76.     {
  77.         return int(30 + 100 * c);
  78.     };
  79.  
  80.     auto line_y = [] (double l) -> int
  81.     {
  82.         return int(40 + 75 * l);
  83.     };
  84.  
  85.     // Создание отображений для источников
  86.     SourceShape A0_shape{ scheme, A0, "A0", Graph_lib::Point{ column_x(0), line_y(0) } };
  87.     SourceShape B0_shape{ scheme, B0, "B0", Graph_lib::Point{ column_x(0), line_y(1) } };
  88.     SourceShape A1_shape{ scheme, A1, "A1", Graph_lib::Point{ column_x(0), line_y(2) } };
  89.     SourceShape B1_shape{ scheme, B1, "B1", Graph_lib::Point{ column_x(0), line_y(3) } };
  90.     SourceShape A2_shape{ scheme, A2, "A2", Graph_lib::Point{ column_x(0), line_y(4) } };
  91.     SourceShape B2_shape{ scheme, B2, "B2", Graph_lib::Point{ column_x(0), line_y(5) } };
  92.     SourceShape A3_shape{ scheme, A3, "A3", Graph_lib::Point{ column_x(0), line_y(6) } };
  93.     SourceShape B3_shape{ scheme, B3, "B3", Graph_lib::Point{ column_x(0), line_y(7) } };
  94.  
  95.     Graph_lib::Text TA0{ Graph_lib::Point{ column_x(0) + 10, line_y(0) + 20}, "-1" };
  96.     Graph_lib::Text TB0{ Graph_lib::Point{ column_x(0) + 10, line_y(1) + 20}, "-1" };
  97.     Graph_lib::Text TA1{ Graph_lib::Point{ column_x(0) + 10, line_y(2) + 20}, "-1" };
  98.     Graph_lib::Text TB1{ Graph_lib::Point{ column_x(0) + 10, line_y(3) + 20}, "-1" };
  99.     Graph_lib::Text TA2{ Graph_lib::Point{ column_x(0) + 10, line_y(4) + 20}, "-1" };
  100.     Graph_lib::Text TB2{ Graph_lib::Point{ column_x(0) + 10, line_y(5) + 20}, "-1" };
  101.     Graph_lib::Text TA3{ Graph_lib::Point{ column_x(0) + 10, line_y(6) + 20}, "-1" };
  102.     Graph_lib::Text TB3{ Graph_lib::Point{ column_x(0) + 10, line_y(7) + 20}, "-1" };
  103.  
  104.     TA0.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  105.     TA0.set_font_size(20);  // Увеличиваем размер шрифта
  106.     TA1.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  107.     TA1.set_font_size(20);  // Увеличиваем размер шрифта
  108.     TA2.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  109.     TA2.set_font_size(20);  // Увеличиваем размер шрифта
  110.     TA3.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  111.     TA3.set_font_size(20);  // Увеличиваем размер шрифта
  112.     TB0.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  113.     TB0.set_font_size(20);  // Увеличиваем размер шрифта
  114.     TB1.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  115.     TB1.set_font_size(20);  // Увеличиваем размер шрифта
  116.     TB2.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  117.     TB2.set_font_size(20);  // Увеличиваем размер шрифта
  118.     TB3.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  119.     TB3.set_font_size(20);  // Увеличиваем размер шрифта
  120.  
  121.     win.attach(TA0);
  122.     win.attach(TA1);
  123.     win.attach(TB0);
  124.     win.attach(TB1);
  125.     win.attach(TA2);
  126.     win.attach(TA3);
  127.     win.attach(TB2);
  128.     win.attach(TB3);
  129.  
  130.     // Заменим AndShape на XorShape
  131.     XorShape xor0_shape{ scheme, xor0, "xor0", Graph_lib::Point{ column_x(1), line_y(1) } };
  132.     XorShape xor1_shape{ scheme, xor1, "xor1", Graph_lib::Point{ column_x(1), line_y(3) } };
  133.     XorShape xor2_shape{ scheme, xor2, "xor2", Graph_lib::Point{ column_x(1), line_y(5) } };
  134.     XorShape xor3_shape{ scheme, xor3, "xor3", Graph_lib::Point{ column_x(1), line_y(7) } };
  135.  
  136.     Graph_lib::Text Txor0{ Graph_lib::Point{ column_x(1) + 10, line_y(1) + 20}, "-1" };
  137.     Graph_lib::Text Txor1{ Graph_lib::Point{ column_x(1) + 10, line_y(3) + 20}, "-1" };
  138.     Graph_lib::Text Txor2{ Graph_lib::Point{ column_x(1) + 10, line_y(5) + 20}, "-1" };
  139.     Graph_lib::Text Txor3{ Graph_lib::Point{ column_x(1) + 10, line_y(7) + 20}, "-1" };
  140.  
  141.     Txor0.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  142.     Txor0.set_font_size(20);  // Увеличиваем размер шрифта
  143.     Txor1.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  144.     Txor1.set_font_size(20);  // Увеличиваем размер шрифта
  145.     Txor2.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  146.     Txor2.set_font_size(20);  // Увеличиваем размер шрифта
  147.     Txor3.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  148.     Txor3.set_font_size(20);  // Увеличиваем размер шрифта
  149.  
  150.     win.attach(Txor0);
  151.     win.attach(Txor1);
  152.     win.attach(Txor2);
  153.     win.attach(Txor3);
  154.  
  155.     OrShape nor_shape{ scheme, nor, "nor", Graph_lib::Point{ column_x(2), line_y(4) } };
  156.     Graph_lib::Text Tnor{ Graph_lib::Point{ column_x(2) + 12, line_y(4) + 19 }, "-1" };
  157.     Tnor.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  158.     Tnor.set_font_size(20);  // Увеличиваем размер шрифта
  159.     win.attach(Tnor);
  160.     // Обновляем соединения схемы
  161.     scheme.update_connections();
  162.    
  163.     bool x0, x1, x2, x3;
  164.    
  165.     TA0.set_label(std::to_string(Abit0));
  166.     TA1.set_label(std::to_string(Abit1));
  167.     TA2.set_label(std::to_string(Abit2));
  168.     TA3.set_label(std::to_string(Abit3));
  169.     TB0.set_label(std::to_string(Bbit0));
  170.     TB1.set_label(std::to_string(Bbit1));
  171.     TB2.set_label(std::to_string(Bbit2));
  172.     TB3.set_label(std::to_string(Bbit3));
  173.     x0 = Abit0 ^ Bbit0;
  174.     x1 = Abit1 ^ Bbit1;
  175.     x2 = Abit2 ^ Bbit2;
  176.     x3 = Abit3 ^ Bbit3;
  177.     Txor0.set_label(std::to_string(x0));
  178.     Txor1.set_label(std::to_string(x1));
  179.     Txor2.set_label(std::to_string(x2));
  180.     Txor3.set_label(std::to_string(x3));
  181.     bool res = !(x0 || x1 || x2 || x3);
  182.     Tnor.set_label(std::to_string(res));
  183.     // Ожидание нажатия кнопки
  184.     win.wait_for_button();
  185. }
  186.  
  187. int main() {
  188.    
  189.     Func();
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement