Advertisement
Korotkodul

logic_main.cpp_final_edit

Mar 17th, 2025 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.53 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. #include <FL/Fl.H>
  13. #include <FL/Fl_Window.H>
  14. #include <FL/Fl_Button.H>
  15. #include <FL/Fl_Input.H>
  16. #include <FL/Fl_Output.H>
  17. #include <FL/Fl_Box.H>
  18.  
  19.  
  20.  
  21.  
  22. void show_scheme(std::string A, std::string B)
  23. {
  24.     bool Abit0, Abit1, Abit2, Abit3, Bbit0, Bbit1, Bbit2, Bbit3;
  25.     Abit0 = bool(A[0] - '0');
  26.     Abit1 = bool(A[1] - '0');
  27.     Abit2 = bool(A[2] - '0');
  28.     Abit3 = bool(A[3] - '0');
  29.     Bbit0 = bool(B[0] - '0');
  30.     Bbit1 = bool(B[1] - '0');
  31.     Bbit2 = bool(B[2] - '0');
  32.     Bbit3 = bool(B[3] - '0');
  33.  
  34.     int win_w = 600;
  35.     int win_h = 800;
  36.     Graph_lib::Point lt{ Graph_lib::x_max()/2 - win_w/2, Graph_lib::y_max()/2 - win_h/2 };
  37.     Simple_window win{ lt, win_w  , win_h , "Scheme of Logic Elements" };
  38.    
  39.     using namespace Logic;
  40.  
  41.     Source A0;
  42.     Source A1;
  43.     Source A2;
  44.     Source A3;
  45.     Source B0;
  46.     Source B1;
  47.     Source B2;
  48.     Source B3;
  49.  
  50.     // Заменим And на Xor
  51.     Xor xor0;
  52.     Xor xor1;
  53.     Xor xor2;
  54.     Xor xor3;
  55.  
  56.     // Подключение источников к XOR
  57.     A0 >> xor0;
  58.     B0 >> xor0;
  59.     A1 >> xor1;
  60.     B1 >> xor1;
  61.     A2 >> xor2;
  62.     B2 >> xor2;
  63.     A3 >> xor3;
  64.     B3 >> xor3;
  65.  
  66.     // Объединение XOR
  67.     Or nor{ Out_state::inverted };
  68.  
  69.     xor0 >> nor;
  70.     xor1 >> nor;
  71.     xor2 >> nor;
  72.     xor3 >> nor;
  73.  
  74.     // Создание схемы
  75.     SchemeShape scheme{ Graph_lib::Point{5, 5}, win_w - 80, win_h - 10 };
  76.     win.attach(scheme);
  77.  
  78.     auto column_x = [] (double c) -> int
  79.     {
  80.         return int(30 + 100 * c);
  81.     };
  82.  
  83.     auto line_y = [] (double l) -> int
  84.     {
  85.         return int(40 + 75 * l);
  86.     };
  87.  
  88.     // Создание отображений для источников
  89.     SourceShape A0_shape{ scheme, A0, "A0", Graph_lib::Point{ column_x(0), line_y(0) } };
  90.     SourceShape B0_shape{ scheme, B0, "B0", Graph_lib::Point{ column_x(0), line_y(1) } };
  91.     SourceShape A1_shape{ scheme, A1, "A1", Graph_lib::Point{ column_x(0), line_y(2) } };
  92.     SourceShape B1_shape{ scheme, B1, "B1", Graph_lib::Point{ column_x(0), line_y(3) } };
  93.     SourceShape A2_shape{ scheme, A2, "A2", Graph_lib::Point{ column_x(0), line_y(4) } };
  94.     SourceShape B2_shape{ scheme, B2, "B2", Graph_lib::Point{ column_x(0), line_y(5) } };
  95.     SourceShape A3_shape{ scheme, A3, "A3", Graph_lib::Point{ column_x(0), line_y(6) } };
  96.     SourceShape B3_shape{ scheme, B3, "B3", Graph_lib::Point{ column_x(0), line_y(7) } };
  97.  
  98.     Graph_lib::Text TA0{ Graph_lib::Point{ column_x(0) + 10, line_y(0) + 20}, "-1" };
  99.     Graph_lib::Text TB0{ Graph_lib::Point{ column_x(0) + 10, line_y(1) + 20}, "-1" };
  100.     Graph_lib::Text TA1{ Graph_lib::Point{ column_x(0) + 10, line_y(2) + 20}, "-1" };
  101.     Graph_lib::Text TB1{ Graph_lib::Point{ column_x(0) + 10, line_y(3) + 20}, "-1" };
  102.     Graph_lib::Text TA2{ Graph_lib::Point{ column_x(0) + 10, line_y(4) + 20}, "-1" };
  103.     Graph_lib::Text TB2{ Graph_lib::Point{ column_x(0) + 10, line_y(5) + 20}, "-1" };
  104.     Graph_lib::Text TA3{ Graph_lib::Point{ column_x(0) + 10, line_y(6) + 20}, "-1" };
  105.     Graph_lib::Text TB3{ Graph_lib::Point{ column_x(0) + 10, line_y(7) + 20}, "-1" };
  106.  
  107.     TA0.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  108.     TA0.set_font_size(20);  // Увеличиваем размер шрифта
  109.     TA1.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  110.     TA1.set_font_size(20);  // Увеличиваем размер шрифта
  111.     TA2.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  112.     TA2.set_font_size(20);  // Увеличиваем размер шрифта
  113.     TA3.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  114.     TA3.set_font_size(20);  // Увеличиваем размер шрифта
  115.     TB0.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  116.     TB0.set_font_size(20);  // Увеличиваем размер шрифта
  117.     TB1.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  118.     TB1.set_font_size(20);  // Увеличиваем размер шрифта
  119.     TB2.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  120.     TB2.set_font_size(20);  // Увеличиваем размер шрифта
  121.     TB3.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  122.     TB3.set_font_size(20);  // Увеличиваем размер шрифта
  123.  
  124.     win.attach(TA0);
  125.     win.attach(TA1);
  126.     win.attach(TB0);
  127.     win.attach(TB1);
  128.     win.attach(TA2);
  129.     win.attach(TA3);
  130.     win.attach(TB2);
  131.     win.attach(TB3);
  132.  
  133.     // Заменим AndShape на XorShape
  134.     XorShape xor0_shape{ scheme, xor0, "xor0", Graph_lib::Point{ column_x(1), line_y(1) } };
  135.     XorShape xor1_shape{ scheme, xor1, "xor1", Graph_lib::Point{ column_x(1), line_y(3) } };
  136.     XorShape xor2_shape{ scheme, xor2, "xor2", Graph_lib::Point{ column_x(1), line_y(5) } };
  137.     XorShape xor3_shape{ scheme, xor3, "xor3", Graph_lib::Point{ column_x(1), line_y(7) } };
  138.  
  139.     Graph_lib::Text Txor0{ Graph_lib::Point{ column_x(1) + 10, line_y(1) + 20}, "-1" };
  140.     Graph_lib::Text Txor1{ Graph_lib::Point{ column_x(1) + 10, line_y(3) + 20}, "-1" };
  141.     Graph_lib::Text Txor2{ Graph_lib::Point{ column_x(1) + 10, line_y(5) + 20}, "-1" };
  142.     Graph_lib::Text Txor3{ Graph_lib::Point{ column_x(1) + 10, line_y(7) + 20}, "-1" };
  143.  
  144.     Txor0.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  145.     Txor0.set_font_size(20);  // Увеличиваем размер шрифта
  146.     Txor1.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  147.     Txor1.set_font_size(20);  // Увеличиваем размер шрифта
  148.     Txor2.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  149.     Txor2.set_font_size(20);  // Увеличиваем размер шрифта
  150.     Txor3.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  151.     Txor3.set_font_size(20);  // Увеличиваем размер шрифта
  152.  
  153.     win.attach(Txor0);
  154.     win.attach(Txor1);
  155.     win.attach(Txor2);
  156.     win.attach(Txor3);
  157.  
  158.     OrShape nor_shape{ scheme, nor, "nor", Graph_lib::Point{ column_x(2), line_y(4) } };
  159.     Graph_lib::Text Tnor{ Graph_lib::Point{ column_x(2) + 12, line_y(4) + 19 }, "-1" };
  160.     Tnor.set_font(Graph_lib::Font::helvetica_bold);  // Жирный шрифт
  161.     Tnor.set_font_size(20);  // Увеличиваем размер шрифта
  162.     win.attach(Tnor);
  163.     // Обновляем соединения схемы
  164.     scheme.update_connections();
  165.    
  166.     bool x0, x1, x2, x3;
  167.    
  168.     TA0.set_label(std::to_string(Abit0));
  169.     TA1.set_label(std::to_string(Abit1));
  170.     TA2.set_label(std::to_string(Abit2));
  171.     TA3.set_label(std::to_string(Abit3));
  172.     TB0.set_label(std::to_string(Bbit0));
  173.     TB1.set_label(std::to_string(Bbit1));
  174.     TB2.set_label(std::to_string(Bbit2));
  175.     TB3.set_label(std::to_string(Bbit3));
  176.     x0 = Abit0 ^ Bbit0;
  177.     x1 = Abit1 ^ Bbit1;
  178.     x2 = Abit2 ^ Bbit2;
  179.     x3 = Abit3 ^ Bbit3;
  180.     Txor0.set_label(std::to_string(x0));
  181.     Txor1.set_label(std::to_string(x1));
  182.     Txor2.set_label(std::to_string(x2));
  183.     Txor3.set_label(std::to_string(x3));
  184.     bool res = !(x0 || x1 || x2 || x3);
  185.     Tnor.set_label(std::to_string(res));
  186.     // Ожидание нажатия кнопки
  187.     win.wait_for_button();
  188. }
  189.  
  190. // Функция сравнения двух 4-битных чисел
  191. void compare_numbers(Fl_Widget* w, void* data) {
  192.     Fl_Input* inputA = (Fl_Input*)(((Fl_Widget**)data)[0]);
  193.     Fl_Input* inputB = (Fl_Input*)(((Fl_Widget**)data)[1]);
  194.     Fl_Output* output = (Fl_Output*)(((Fl_Widget**)data)[2]);
  195.  
  196.     std::string A = inputA->value();
  197.     std::string B = inputB->value();
  198.  
  199.     if (A == B)
  200.         output->value("equal");
  201.     else
  202.         output->value("not equal");
  203.     show_scheme(A, B);
  204. }
  205.  
  206. void exit_callback(Fl_Widget* w, void* data) {
  207.     exit(0);  // Завершение программы
  208. }
  209.  
  210. int main() {
  211.     Fl_Window* window = new Fl_Window(300, 250, "4-bit Comparator");
  212.  
  213.     // Надпись и поле для ввода числа A
  214.     Fl_Box* labelA = new Fl_Box(FL_NO_BOX, 50, 10, 200, 20, "4-bit number A");
  215.     Fl_Input* inputA = new Fl_Input(50, 30, 200, 25);
  216.     inputA->maximum_size(4); // Ограничение ввода до 4 символов
  217.  
  218.     // Надпись и поле для ввода числа B
  219.     Fl_Box* labelB = new Fl_Box(FL_NO_BOX, 50, 60, 200, 20, "4-bit number B");
  220.     Fl_Input* inputB = new Fl_Input(50, 80, 200, 25);
  221.     inputB->maximum_size(4); // Ограничение ввода до 4 символов
  222.  
  223.     // Кнопка "Count Result. Show Scheme"
  224.     Fl_Button* compareButton = new Fl_Button(50, 120, 200, 30, "Count Result. Show Scheme");
  225.  
  226.     // Надпись "Result: A and B are"
  227.     Fl_Box* labelResult = new Fl_Box(FL_NO_BOX, 50, 160, 200, 20, "Result: A and B are");
  228.  
  229.     // Поле для вывода результата
  230.     Fl_Output* output = new Fl_Output(50, 180, 200, 25);
  231.  
  232.     // Кнопка "Exit"
  233.     Fl_Button* exitButton = new Fl_Button(50, 210, 200, 30, "Exit");
  234.  
  235.     // Привязка функций к кнопкам
  236.     Fl_Widget* widgets[] = { inputA, inputB, output };
  237.     compareButton->callback(compare_numbers, widgets);
  238.     exitButton->callback(exit_callback);
  239.  
  240.     window->end();
  241.     window->show();
  242.    
  243.     return Fl::run();
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement