Advertisement
Korotkodul

logic_main.cpp5

Mar 17th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <sstream>
  4. #include <exception>
  5.  
  6. #include <Graph_lib/Graph.h>
  7. #include <Graph_lib/Window.h>
  8. #include <Graph_lib/Simple_window.h>
  9. #include <Graph_lib/GUI.h>
  10. #include "logic.h"
  11. #include "logic_shapes.h"
  12.  
  13. #include <FL/Fl.H>
  14. #include <FL/Fl_Window.H>
  15. #include <FL/Fl_Button.H>
  16. #include <FL/Fl_Box.H>
  17.  
  18.  
  19.  
  20.  
  21. int main ()
  22. try
  23. {
  24. int win_w = 600;
  25. int win_h = 400;
  26. Graph_lib::Point lt{ Graph_lib::x_max()/2 - win_w/2, Graph_lib::y_max()/2 - win_h/2 };
  27. Simple_window win{ lt, win_w , win_h * 2, "Scheme of Logic Elements" };
  28.  
  29. using namespace Logic;
  30.  
  31. Source A0;
  32. Source A1;
  33. Source A2;
  34. Source A3;
  35. Source B0;
  36. Source B1;
  37. Source B2;
  38. Source B3;
  39.  
  40. // Заменим And на Xor
  41. Xor xor0;
  42. Xor xor1;
  43. Xor xor2;
  44. Xor xor3;
  45.  
  46. // Подключение источников к XOR
  47. A0 >> xor0;
  48. B0 >> xor0;
  49. A1 >> xor1;
  50. B1 >> xor1;
  51. A2 >> xor2;
  52. B2 >> xor2;
  53. A3 >> xor3;
  54. B3 >> xor3;
  55.  
  56. // Объединение XOR
  57. Or nor{ Out_state::inverted };
  58.  
  59. xor0 >> nor;
  60. xor1 >> nor;
  61. xor2 >> nor;
  62. xor3 >> nor;
  63.  
  64. // Создание схемы
  65. SchemeShape scheme{ Graph_lib::Point{5, 5}, win_w - 80, win_h - 10 };
  66. win.attach(scheme);
  67.  
  68. auto column_x = [] (double c) -> int
  69. {
  70. return int(30 + 100 * c);
  71. };
  72.  
  73. auto line_y = [] (double l) -> int
  74. {
  75. return int(40 + 75 * l);
  76. };
  77.  
  78. // Создание отображений для источников
  79. SourceShape A0_shape{ scheme, A0, "A0", Graph_lib::Point{ column_x(0), line_y(0) } };
  80. SourceShape B0_shape{ scheme, B0, "B0", Graph_lib::Point{ column_x(0), line_y(1) } };
  81. SourceShape A1_shape{ scheme, A1, "A1", Graph_lib::Point{ column_x(0), line_y(2) } };
  82. SourceShape B1_shape{ scheme, B1, "B1", Graph_lib::Point{ column_x(0), line_y(3) } };
  83. SourceShape A2_shape{ scheme, A2, "A2", Graph_lib::Point{ column_x(0), line_y(4) } };
  84. SourceShape B2_shape{ scheme, B2, "B2", Graph_lib::Point{ column_x(0), line_y(5) } };
  85. SourceShape A3_shape{ scheme, A3, "A3", Graph_lib::Point{ column_x(0), line_y(6) } };
  86. SourceShape B3_shape{ scheme, B3, "B3", Graph_lib::Point{ column_x(0), line_y(7) } };
  87.  
  88. Graph_lib::Text TA0{ Graph_lib::Point{ column_x(0) + 10, line_y(0) + 20}, "-1" };
  89. Graph_lib::Text TB0{ Graph_lib::Point{ column_x(0) + 10, line_y(1) + 20}, "-1" };
  90. Graph_lib::Text TA1{ Graph_lib::Point{ column_x(0) + 10, line_y(2) + 20}, "-1" };
  91. Graph_lib::Text TB1{ Graph_lib::Point{ column_x(0) + 10, line_y(3) + 20}, "-1" };
  92. Graph_lib::Text TA2{ Graph_lib::Point{ column_x(0) + 10, line_y(4) + 20}, "-1" };
  93. Graph_lib::Text TB2{ Graph_lib::Point{ column_x(0) + 10, line_y(5) + 20}, "-1" };
  94. Graph_lib::Text TA3{ Graph_lib::Point{ column_x(0) + 10, line_y(6) + 20}, "-1" };
  95. Graph_lib::Text TB3{ Graph_lib::Point{ column_x(0) + 10, line_y(7) + 20}, "-1" };
  96.  
  97. TA0.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  98. TA0.set_font_size(20); // Увеличиваем размер шрифта
  99. TA1.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  100. TA1.set_font_size(20); // Увеличиваем размер шрифта
  101. TA2.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  102. TA2.set_font_size(20); // Увеличиваем размер шрифта
  103. TA3.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  104. TA3.set_font_size(20); // Увеличиваем размер шрифта
  105. TB0.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  106. TB0.set_font_size(20); // Увеличиваем размер шрифта
  107. TB1.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  108. TB1.set_font_size(20); // Увеличиваем размер шрифта
  109. TB2.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  110. TB2.set_font_size(20); // Увеличиваем размер шрифта
  111. TB3.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  112. TB3.set_font_size(20); // Увеличиваем размер шрифта
  113.  
  114. win.attach(TA0);
  115. win.attach(TA1);
  116. win.attach(TB0);
  117. win.attach(TB1);
  118. win.attach(TA2);
  119. win.attach(TA3);
  120. win.attach(TB2);
  121. win.attach(TB3);
  122.  
  123. // Заменим AndShape на XorShape
  124. XorShape xor0_shape{ scheme, xor0, "xor0", Graph_lib::Point{ column_x(1), line_y(1) } };
  125. XorShape xor1_shape{ scheme, xor1, "xor1", Graph_lib::Point{ column_x(1), line_y(3) } };
  126. XorShape xor2_shape{ scheme, xor2, "xor2", Graph_lib::Point{ column_x(1), line_y(5) } };
  127. XorShape xor3_shape{ scheme, xor3, "xor3", Graph_lib::Point{ column_x(1), line_y(7) } };
  128.  
  129. Graph_lib::Text Txor0{ Graph_lib::Point{ column_x(1) + 10, line_y(1) + 20}, "-1" };
  130. Graph_lib::Text Txor1{ Graph_lib::Point{ column_x(1) + 10, line_y(3) + 20}, "-1" };
  131. Graph_lib::Text Txor2{ Graph_lib::Point{ column_x(1) + 10, line_y(5) + 20}, "-1" };
  132. Graph_lib::Text Txor3{ Graph_lib::Point{ column_x(1) + 10, line_y(7) + 20}, "-1" };
  133.  
  134. Txor0.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  135. Txor0.set_font_size(20); // Увеличиваем размер шрифта
  136. Txor1.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  137. Txor1.set_font_size(20); // Увеличиваем размер шрифта
  138. Txor2.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  139. Txor2.set_font_size(20); // Увеличиваем размер шрифта
  140. Txor3.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  141. Txor3.set_font_size(20); // Увеличиваем размер шрифта
  142.  
  143. win.attach(Txor0);
  144. win.attach(Txor1);
  145. win.attach(Txor2);
  146. win.attach(Txor3);
  147.  
  148.  
  149. // Создание отображения для объединения XOR
  150. OrShape nor_shape{ scheme, nor, "nor", Graph_lib::Point{ column_x(2), line_y(4) } };
  151. Graph_lib::Text Tnor{ Graph_lib::Point{ column_x(2) + 12, line_y(4) + 19 }, "-1" };
  152. Tnor.set_font(Graph_lib::Font::helvetica_bold); // Жирный шрифт
  153. Tnor.set_font_size(20); // Увеличиваем размер шрифта
  154. win.attach(Tnor);
  155.  
  156.  
  157. // Обновляем соединения схемы
  158. scheme.update_connections();
  159.  
  160. //return Fl::run();// Ожидаем событий и завершения работы окна
  161. std::string go_on;
  162.  
  163. // Основной цикл
  164. Fl_Button read_button(100, 50, 100, 30, "Read numbers");
  165. //win.redraw(); // Обновляем окно
  166. Graph_lib::Text* TATB_ptr_vec[] = {&TA0, &TA1, &TA2, &TA3, &TB0, &TB1, &TB2, &TB3};
  167. //сдлать л-да ф-ю
  168. //конв-ть в ук-ль на ф-ю
  169. //предать в cl-ck ук-ль и
  170. auto lambda = [](Fl_Widget*, void* TATB_ptr_vec_ptr) {
  171. auto TATB_ptr_vec = reinterpret_cast<Graph_lib::Text**>(TATB_ptr_vec_ptr);
  172. auto& TA0 = *(TATB_ptr_vec[0]);
  173. auto& TA1 = *(TATB_ptr_vec[1]);
  174. auto& TA2 = *(TATB_ptr_vec[2]);
  175. auto& TA3 = *(TATB_ptr_vec[3]);
  176. auto& TB0 = *(TATB_ptr_vec[4]);
  177. auto& TB1 = *(TATB_ptr_vec[5]);
  178. auto& TB2 = *(TATB_ptr_vec[6]);
  179. auto& TB3 = *(TATB_ptr_vec[7]);
  180. bool Abit0, Abit1, Abit2, Abit3, Bbit0, Bbit1, Bbit2, Bbit3;
  181. bool x0, x1, x2, x3;
  182. std::cin >> Abit0 >> Abit1 >> Abit2 >> Abit3 >> Bbit0 >> Bbit1 >> Bbit2 >> Bbit3;
  183. TA0.set_label(std::to_string(Abit0));
  184. TA1.set_label(std::to_string(Abit1));
  185. TA2.set_label(std::to_string(Abit2));
  186. TA3.set_label(std::to_string(Abit3));
  187. TB0.set_label(std::to_string(Bbit0));
  188.  
  189. TB1.set_label(std::to_string(Bbit1));
  190. TB2.set_label(std::to_string(Bbit2));
  191. TB3.set_label(std::to_string(Bbit3));
  192. x0 = Abit0 ^ Bbit0;
  193. x1 = Abit1 ^ Bbit1;
  194. x2 = Abit2 ^ Bbit2;
  195. x3 = Abit3 ^ Bbit3;
  196. // Txor0.set_label(std::to_string(x0));
  197. // Txor1.set_label(std::to_string(x1));
  198. // Txor2.set_label(std::to_string(x2));
  199. // Txor3.set_label(std::to_string(x3));
  200. // bool res = !(x0 || x1 || x2 || x3);
  201. // Tnor.set_label(std::to_string(res));
  202. };
  203. void(*function)(Fl_Widget*, void* ) = lambda;
  204.  
  205. read_button.callback(function, TATB_ptr_vec);
  206.  
  207.  
  208.  
  209.  
  210.  
  211. win.redraw(); // Обновляем окно
  212.  
  213. std::cin >> go_on;
  214. if (go_on == "stop") {
  215. return 0;
  216. }
  217.  
  218. // Ожидание нажатия кнопки
  219. //win.wait_for_button();
  220.  
  221. //return 0;
  222.  
  223. }
  224. catch (std::exception& e)
  225. {
  226. std::cerr << e.what() << std::endl;
  227. return 1;
  228. }
  229. catch (...)
  230. {
  231. std::cerr <<"Oops, something went wrong..."<< std::endl;
  232. return 2;
  233. }
  234.  
  235.  
  236. /*#include <FL/Fl.H>
  237. #include <FL/Fl_Window.H>
  238. #include <FL/Fl_Button.H>
  239. #include <FL/Fl_Box.H>
  240. #include <iostream>
  241. #include <sstream>
  242.  
  243. int main() {
  244. Fl_Window win(300, 200, "Sum of 3 numbers");
  245. Fl_Button button(100, 50, 100, 30, "Read numbers");
  246. Fl_Box output_box(50, 100, 200, 30, "");
  247.  
  248. button.callback([](Fl_Widget*, void* output) {
  249. int a, b, c;
  250. std::cout << "Write 3 numbers: ";
  251. std::cin >> a >> b >> c;
  252.  
  253. int sum = a + b + c;
  254.  
  255. std::ostringstream oss;
  256. oss << "Sum: " << sum;
  257. static_cast<Fl_Box*>(output)->label(oss.str().c_str());
  258. }, &output_box);
  259.  
  260. win.end();
  261. win.show();
  262.  
  263. return Fl::run();
  264. }*/
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement