Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <exception>
- #include <Graph_lib/Graph.h>
- #include <Graph_lib/Window.h>
- #include <Graph_lib/Simple_window.h>
- #include "logic.h"
- #include "logic_shapes.h"
- #include <FL/Fl.H>
- #include <FL/Fl_Window.H>
- #include <FL/Fl_Button.H>
- #include <FL/Fl_Input.H>
- #include <FL/Fl_Output.H>
- #include <FL/Fl_Box.H>
- void show_scheme(std::string A, std::string B)
- {
- bool Abit0, Abit1, Abit2, Abit3, Bbit0, Bbit1, Bbit2, Bbit3;
- Abit0 = bool(A[0] - '0');
- Abit1 = bool(A[1] - '0');
- Abit2 = bool(A[2] - '0');
- Abit3 = bool(A[3] - '0');
- Bbit0 = bool(B[0] - '0');
- Bbit1 = bool(B[1] - '0');
- Bbit2 = bool(B[2] - '0');
- Bbit3 = bool(B[3] - '0');
- int win_w = 600;
- int win_h = 800;
- Graph_lib::Point lt{ Graph_lib::x_max()/2 - win_w/2, Graph_lib::y_max()/2 - win_h/2 };
- Simple_window win{ lt, win_w , win_h , "Scheme of Logic Elements" };
- using namespace Logic;
- Source A0;
- Source A1;
- Source A2;
- Source A3;
- Source B0;
- Source B1;
- Source B2;
- Source B3;
- // Заменим And на Xor
- Xor xor0;
- Xor xor1;
- Xor xor2;
- Xor xor3;
- // Подключение источников к XOR
- A0 >> xor0;
- B0 >> xor0;
- A1 >> xor1;
- B1 >> xor1;
- A2 >> xor2;
- B2 >> xor2;
- A3 >> xor3;
- B3 >> xor3;
- // Объединение XOR
- Or nor{ Out_state::inverted };
- xor0 >> nor;
- xor1 >> nor;
- xor2 >> nor;
- xor3 >> nor;
- // Создание схемы
- SchemeShape scheme{ Graph_lib::Point{5, 5}, win_w - 80, win_h - 10 };
- win.attach(scheme);
- auto column_x = [] (double c) -> int
- {
- return int(30 + 100 * c);
- };
- auto line_y = [] (double l) -> int
- {
- return int(40 + 75 * l);
- };
- // Создание отображений для источников
- SourceShape A0_shape{ scheme, A0, "A0", Graph_lib::Point{ column_x(0), line_y(0) } };
- SourceShape B0_shape{ scheme, B0, "B0", Graph_lib::Point{ column_x(0), line_y(1) } };
- SourceShape A1_shape{ scheme, A1, "A1", Graph_lib::Point{ column_x(0), line_y(2) } };
- SourceShape B1_shape{ scheme, B1, "B1", Graph_lib::Point{ column_x(0), line_y(3) } };
- SourceShape A2_shape{ scheme, A2, "A2", Graph_lib::Point{ column_x(0), line_y(4) } };
- SourceShape B2_shape{ scheme, B2, "B2", Graph_lib::Point{ column_x(0), line_y(5) } };
- SourceShape A3_shape{ scheme, A3, "A3", Graph_lib::Point{ column_x(0), line_y(6) } };
- SourceShape B3_shape{ scheme, B3, "B3", Graph_lib::Point{ column_x(0), line_y(7) } };
- Graph_lib::Text TA0{ Graph_lib::Point{ column_x(0) + 10, line_y(0) + 20}, "-1" };
- Graph_lib::Text TB0{ Graph_lib::Point{ column_x(0) + 10, line_y(1) + 20}, "-1" };
- Graph_lib::Text TA1{ Graph_lib::Point{ column_x(0) + 10, line_y(2) + 20}, "-1" };
- Graph_lib::Text TB1{ Graph_lib::Point{ column_x(0) + 10, line_y(3) + 20}, "-1" };
- Graph_lib::Text TA2{ Graph_lib::Point{ column_x(0) + 10, line_y(4) + 20}, "-1" };
- Graph_lib::Text TB2{ Graph_lib::Point{ column_x(0) + 10, line_y(5) + 20}, "-1" };
- Graph_lib::Text TA3{ Graph_lib::Point{ column_x(0) + 10, line_y(6) + 20}, "-1" };
- Graph_lib::Text TB3{ Graph_lib::Point{ column_x(0) + 10, line_y(7) + 20}, "-1" };
- TA0.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TA0.set_font_size(20); // Увеличиваем размер шрифта
- TA1.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TA1.set_font_size(20); // Увеличиваем размер шрифта
- TA2.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TA2.set_font_size(20); // Увеличиваем размер шрифта
- TA3.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TA3.set_font_size(20); // Увеличиваем размер шрифта
- TB0.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TB0.set_font_size(20); // Увеличиваем размер шрифта
- TB1.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TB1.set_font_size(20); // Увеличиваем размер шрифта
- TB2.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TB2.set_font_size(20); // Увеличиваем размер шрифта
- TB3.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- TB3.set_font_size(20); // Увеличиваем размер шрифта
- win.attach(TA0);
- win.attach(TA1);
- win.attach(TB0);
- win.attach(TB1);
- win.attach(TA2);
- win.attach(TA3);
- win.attach(TB2);
- win.attach(TB3);
- // Заменим AndShape на XorShape
- XorShape xor0_shape{ scheme, xor0, "xor0", Graph_lib::Point{ column_x(1), line_y(1) } };
- XorShape xor1_shape{ scheme, xor1, "xor1", Graph_lib::Point{ column_x(1), line_y(3) } };
- XorShape xor2_shape{ scheme, xor2, "xor2", Graph_lib::Point{ column_x(1), line_y(5) } };
- XorShape xor3_shape{ scheme, xor3, "xor3", Graph_lib::Point{ column_x(1), line_y(7) } };
- Graph_lib::Text Txor0{ Graph_lib::Point{ column_x(1) + 10, line_y(1) + 20}, "-1" };
- Graph_lib::Text Txor1{ Graph_lib::Point{ column_x(1) + 10, line_y(3) + 20}, "-1" };
- Graph_lib::Text Txor2{ Graph_lib::Point{ column_x(1) + 10, line_y(5) + 20}, "-1" };
- Graph_lib::Text Txor3{ Graph_lib::Point{ column_x(1) + 10, line_y(7) + 20}, "-1" };
- Txor0.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- Txor0.set_font_size(20); // Увеличиваем размер шрифта
- Txor1.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- Txor1.set_font_size(20); // Увеличиваем размер шрифта
- Txor2.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- Txor2.set_font_size(20); // Увеличиваем размер шрифта
- Txor3.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- Txor3.set_font_size(20); // Увеличиваем размер шрифта
- win.attach(Txor0);
- win.attach(Txor1);
- win.attach(Txor2);
- win.attach(Txor3);
- OrShape nor_shape{ scheme, nor, "nor", Graph_lib::Point{ column_x(2), line_y(4) } };
- Graph_lib::Text Tnor{ Graph_lib::Point{ column_x(2) + 12, line_y(4) + 19 }, "-1" };
- Tnor.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
- Tnor.set_font_size(20); // Увеличиваем размер шрифта
- win.attach(Tnor);
- // Обновляем соединения схемы
- scheme.update_connections();
- bool x0, x1, x2, x3;
- TA0.set_label(std::to_string(Abit0));
- TA1.set_label(std::to_string(Abit1));
- TA2.set_label(std::to_string(Abit2));
- TA3.set_label(std::to_string(Abit3));
- TB0.set_label(std::to_string(Bbit0));
- TB1.set_label(std::to_string(Bbit1));
- TB2.set_label(std::to_string(Bbit2));
- TB3.set_label(std::to_string(Bbit3));
- x0 = Abit0 ^ Bbit0;
- x1 = Abit1 ^ Bbit1;
- x2 = Abit2 ^ Bbit2;
- x3 = Abit3 ^ Bbit3;
- Txor0.set_label(std::to_string(x0));
- Txor1.set_label(std::to_string(x1));
- Txor2.set_label(std::to_string(x2));
- Txor3.set_label(std::to_string(x3));
- bool res = !(x0 || x1 || x2 || x3);
- Tnor.set_label(std::to_string(res));
- // Ожидание нажатия кнопки
- win.wait_for_button();
- }
- // Функция сравнения двух 4-битных чисел
- void compare_numbers(Fl_Widget* w, void* data) {
- Fl_Input* inputA = (Fl_Input*)(((Fl_Widget**)data)[0]);
- Fl_Input* inputB = (Fl_Input*)(((Fl_Widget**)data)[1]);
- Fl_Output* output = (Fl_Output*)(((Fl_Widget**)data)[2]);
- std::string A = inputA->value();
- std::string B = inputB->value();
- if (A == B)
- output->value("equal");
- else
- output->value("not equal");
- show_scheme(A, B);
- }
- void exit_callback(Fl_Widget* w, void* data) {
- exit(0); // Завершение программы
- }
- int main() {
- Fl_Window* window = new Fl_Window(300, 250, "4-bit Comparator");
- // Надпись и поле для ввода числа A
- Fl_Box* labelA = new Fl_Box(FL_NO_BOX, 50, 10, 200, 20, "4-bit number A");
- Fl_Input* inputA = new Fl_Input(50, 30, 200, 25);
- inputA->maximum_size(4); // Ограничение ввода до 4 символов
- // Надпись и поле для ввода числа B
- Fl_Box* labelB = new Fl_Box(FL_NO_BOX, 50, 60, 200, 20, "4-bit number B");
- Fl_Input* inputB = new Fl_Input(50, 80, 200, 25);
- inputB->maximum_size(4); // Ограничение ввода до 4 символов
- // Кнопка "Count Result. Show Scheme"
- Fl_Button* compareButton = new Fl_Button(50, 120, 200, 30, "Count Result. Show Scheme");
- // Надпись "Result: A and B are"
- Fl_Box* labelResult = new Fl_Box(FL_NO_BOX, 50, 160, 200, 20, "Result: A and B are");
- // Поле для вывода результата
- Fl_Output* output = new Fl_Output(50, 180, 200, 25);
- // Кнопка "Exit"
- Fl_Button* exitButton = new Fl_Button(50, 210, 200, 30, "Exit");
- // Привязка функций к кнопкам
- Fl_Widget* widgets[] = { inputA, inputB, output };
- compareButton->callback(compare_numbers, widgets);
- exitButton->callback(exit_callback);
- window->end();
- window->show();
- return Fl::run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement