BorrowTheProgrammer

asdas

Apr 19th, 2022 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <pthread.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8.  
  9. struct termios savetty;
  10. struct termios tty;
  11.  
  12. #define ABS(N) (((N) < 0) ? (-(N)) : ((N)))
  13. #define GOTOXY(x, y) printf("\033" "[%d;%dH", y, x);
  14.  
  15. #define PLIFT 0
  16. #define GLIFT 1
  17. #define NONE -1
  18.  
  19. char stateBuf[22];
  20. int end = 0;
  21.  
  22. typedef struct
  23. {
  24. const int floors; // количество этажей, на которое рассчитан лифт
  25. double speed;
  26. int floor;
  27. char moving;
  28. int isEmpty;
  29.  
  30. } Elevator;
  31.  
  32. // лифты и их начальные состояния
  33. // кол-во этажей, скорость, тек. этаж, движется, пустой
  34.  
  35. Elevator plift = {10, 10.0, 1, 0, 1}; // пассажирский
  36. Elevator glift = {10, 5.0, 1, 0, 1}; // грузовой
  37.  
  38. // клавиши для управления пассажирским и грузовым лифтом соответственно
  39. char lp_inside[11] = {'D', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
  40. char lg_inside[11] = {'D', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';'};
  41. char lift_move = NONE;
  42. int floor_move = 1;
  43. char from_move = 1;
  44.  
  45. void printState()
  46. {
  47. system("clear");
  48.  
  49. for (int i = 0; i < plift.floors; i++)
  50. {
  51. if (i != (plift.floor - 1))
  52. {
  53. stateBuf[i] = '-';
  54. }
  55. else
  56. {
  57. if ((plift.isEmpty))
  58. {
  59. stateBuf[i] = '0';
  60. }
  61. else
  62. {
  63. stateBuf[i] = '1';
  64. }
  65. }
  66. }
  67. stateBuf[plift.floors] = '\n';
  68. for (int i = 0; i < glift.floors; i++)
  69. {
  70. if (i != (glift.floor - 1))
  71. {
  72. stateBuf[i + plift.floors + 1] = '-';
  73. }
  74. else
  75. {
  76. if ((glift.isEmpty))
  77. {
  78. stateBuf[i + plift.floors + 1] = '0';
  79. }
  80. else
  81. {
  82. stateBuf[i + plift.floors + 1] = '1';
  83. }
  84. }
  85. }
  86. stateBuf[21] = '\n';
  87. write(1, stateBuf, 21);
  88. }
  89.  
  90. // отправить лифт на указанный этаж
  91. char go(Elevator *lift, int destination, char from)
  92. {
  93. if (destination <= 0 || destination > lift->floors)
  94. {
  95. printf("Недоступное место назначения лифта: %d\n", destination);
  96. return 0;
  97. }
  98.  
  99. int steps = ABS(destination - lift->floor);
  100. int step = (destination > lift->floor) ? 1 : -1;
  101.  
  102. for (int i = 0; i < steps; i++)
  103. {
  104. lift->moving = 1;
  105. sleep(10 / lift->speed);
  106. lift->floor += step;
  107. }
  108.  
  109. if (from == 0)
  110. {
  111. lift->isEmpty = 1;
  112. }
  113. else if (from == 1)
  114. {
  115. lift->isEmpty = 0;
  116. }
  117.  
  118. lift->moving = 0;
  119.  
  120. return 1;
  121. }
  122.  
  123. int parse_floor(char floor_char, char *which_lift, char *from)
  124. {
  125.  
  126. for (int i = 1; i < 11; i++)
  127. {
  128. if (floor_char == lp_inside[i])
  129. {
  130. *from = 0;
  131. *which_lift = PLIFT;
  132. return i;
  133. }
  134.  
  135. if (floor_char == lg_inside[i])
  136. {
  137. *from = 0;
  138. *which_lift = GLIFT;
  139. return i;
  140. }
  141. }
  142.  
  143. *from = 1;
  144. int result = atoi(&floor_char);
  145.  
  146. return (result != 0 ? result : 10);
  147. }
  148.  
  149. void *view(void *attr)
  150. {
  151. while (!end)
  152. {
  153. printState();
  154. sleep(1);
  155. }
  156. pthread_exit(0);
  157. }
  158.  
  159. void *calls(void *attr)
  160. {
  161.  
  162. char which_lift = PLIFT;
  163. char from = 1;
  164. char floor_chars[2];
  165. char none;
  166. floor_chars[1] = '\0';
  167.  
  168. while (!end)
  169. {
  170. read(0, &floor_chars, 1);
  171.  
  172. if (floor_chars[0] == 'C')
  173. {
  174. end = 1;
  175. pthread_exit(0);
  176. }
  177.  
  178. int floor = parse_floor(floor_chars[0], &which_lift, &from);
  179. lift_move = NONE;
  180. floor_move = floor;
  181.  
  182. if (from == 1)
  183. {
  184. from_move = 1;
  185.  
  186. if (plift.moving && glift.moving)
  187. {
  188. while (plift.moving && glift.moving)
  189. {
  190. }
  191. }
  192.  
  193. if (plift.moving)
  194. {
  195. lift_move = GLIFT;
  196. }
  197. else if (glift.moving)
  198. {
  199. lift_move = PLIFT;
  200. }
  201. else
  202. {
  203.  
  204. if (ABS(plift.floor - floor) > ABS(glift.floor - floor))
  205. {
  206. lift_move = GLIFT;
  207. }
  208. else
  209. {
  210. lift_move = PLIFT;
  211. }
  212. }
  213. }
  214. else
  215. {
  216. from_move = 0;
  217. if (which_lift == PLIFT)
  218. {
  219. if (plift.isEmpty == 1)
  220. {
  221. GOTOXY(1, 4);
  222. printf("Пассажирский лифт пустой, невозможно вызвать его изнутри!\n");
  223. }
  224. else
  225. {
  226. lift_move = PLIFT;
  227. }
  228. }
  229. else
  230. {
  231. if (glift.isEmpty == 1)
  232. {
  233. GOTOXY(1, 4);
  234. printf("Грузовой лифт пустой, невозможно вызвать его изнутри!\n");
  235. }
  236. else
  237. {
  238. lift_move = GLIFT;
  239. }
  240. }
  241. }
  242. }
  243. }
  244.  
  245. void *plift_move(void *attr)
  246. {
  247.  
  248. while (!end)
  249. {
  250. if (lift_move == PLIFT)
  251. {
  252. go(&plift, floor_move, from_move);
  253. if (lift_move != GLIFT)
  254. {
  255. lift_move = NONE;
  256. }
  257. }
  258. }
  259.  
  260. pthread_exit(0);
  261. }
  262.  
  263. void *glift_move(void *attr)
  264. {
  265.  
  266. while (!end)
  267. {
  268. if (lift_move == GLIFT)
  269. {
  270. go(&glift, floor_move, from_move);
  271. if (lift_move != PLIFT)
  272. {
  273. lift_move = NONE;
  274. }
  275. }
  276. }
  277.  
  278. pthread_exit(0);
  279. }
  280.  
  281.  
  282. int main(int argc, char *argv[])
  283. {
  284.  
  285. system("clear");
  286. tcgetattr(0, &tty);
  287. savetty = tty;
  288.  
  289. tty.c_lflag &= ~(ICANON | ECHO | ISIG);
  290.  
  291. tty.c_cc[VMIN] = 1;
  292.  
  293. tcsetattr(0, TCSAFLUSH, &tty);
  294.  
  295. pthread_attr_t pattr;
  296. pthread_attr_init(&pattr);
  297.  
  298. pthread_attr_setscope(&pattr, PTHREAD_SCOPE_SYSTEM);
  299. pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
  300.  
  301. pthread_t view_pid, calls_pid, plift_pid, glift_pid;
  302.  
  303. pthread_create(&calls_pid, &pattr, calls, NULL);
  304. pthread_create(&plift_pid, &pattr, plift_move, NULL);
  305. pthread_create(&glift_pid, &pattr, glift_move, NULL);
  306. pthread_create(&view_pid, &pattr, view, NULL);
  307.  
  308. pthread_join(calls_pid, NULL);
  309. pthread_join(view_pid, NULL);
  310. pthread_join(plift_pid, NULL);
  311. pthread_join(glift_pid, NULL);
  312.  
  313. tcsetattr(0, TCSAFLUSH, &savetty);
  314. system("clear");
  315.  
  316. exit(0);
  317. }
  318.  
Add Comment
Please, Sign In to add comment