Source.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. struct ThreadArgs {
  6. int hours;
  7. int minutes;
  8. int seconds;
  9. int milisec;
  10. };
  11. struct ThreadArgs globalTime;
  12. CRITICAL_SECTION section = { 0 }; //Êðèòè÷åñêàÿ ñåêöèÿ
  13. int flag = 0;
  14. int flagSecund = 0;
  15. int flagTimer = 0;
  16. DWORD WINAPI PrintTime(LPVOID param) {
  17. while (1) {
  18. if (flag) {
  19. printf("Âðåìÿ îáû÷íîå: %d:%d:%d\n", globalTime.hours, globalTime.minutes, globalTime.seconds);
  20. flag = 0;
  21. }
  22. }
  23. }
  24. DWORD WINAPI PrintSecundTime(LPVOID param) {
  25. struct ThreadArgs* secundStruct = (struct ThreadArgs*)param;
  26. while (1) {
  27. if (flagSecund) {
  28. printf("Ñåêóíäîìåð: %d:%d:%d:%d\n", secundStruct->hours, secundStruct->minutes, secundStruct->seconds, secundStruct->milisec);
  29. flagSecund = 0;
  30. }
  31. }
  32. }
  33. DWORD WINAPI PrintTimer(LPVOID param) {
  34. struct ThreadArgs* timerStruct = (struct ThreadArgs*)param;
  35. while (1) {
  36. switch (flagTimer)
  37. {
  38. case 1:
  39. printf("Òàéìåð: %02d:%02d:%02d\n", timerStruct->hours, timerStruct->minutes, timerStruct->seconds);
  40. flagTimer = 0;
  41. break;
  42. case 2:
  43. printf("ÒÀÉÌÅÐ ÇÀÊÎÍ×ÈËÑß\n");
  44. ExitThread(1);
  45. default:
  46. break;
  47. }
  48. }
  49. }
  50. DWORD WINAPI TimerCommon(LPVOID param) {
  51. while (1) {
  52. globalTime.seconds++;
  53. if (globalTime.seconds >= 60) {
  54. globalTime.seconds = 0;
  55. globalTime.minutes++;
  56. if (globalTime.minutes >= 60) {
  57. globalTime.minutes = 0;
  58. globalTime.hours++;
  59. if (globalTime.hours >= 24) {
  60. globalTime.hours = 0;
  61. }
  62. }
  63. }
  64. flag = 1;
  65. Sleep(1000);
  66. }
  67. }
  68. DWORD WINAPI Secundomer(LPVOID param) {
  69. LARGE_INTEGER frequency, start, end;
  70. // Ïîëó÷àåì ÷àñòîòó ñ÷åò÷èêà ïðîèçâîäèòåëüíîñòè
  71. QueryPerformanceFrequency(&frequency);
  72. // Çàïèñûâàåì íà÷àëüíîå âðåìÿ
  73. QueryPerformanceCounter(&start);
  74. struct ThreadArgs* secundStruct = (struct ThreadArgs*)param;
  75. while (1) {
  76. QueryPerformanceCounter(&end);
  77. // Âû÷èñëÿåì âðåìÿ, ïðîøåäøåå ñ íà÷àëà ðàáîòû ñåêóíäîìåðà
  78. LONGLONG elapsed = end.QuadPart - start.QuadPart;
  79. LONGLONG elapsed_ms = (elapsed * 1000) / frequency.QuadPart;
  80. secundStruct->hours = (int)(elapsed_ms / (1000 * 60 * 60));
  81. secundStruct->minutes = (int)((elapsed_ms % (1000 * 60 * 60)) / (1000 * 60));
  82. secundStruct->seconds = (int)((elapsed_ms % (1000 * 60)) / 1000);
  83. secundStruct->milisec = (int)(elapsed_ms % 1000);
  84. flagSecund = 1;
  85. Sleep(1);
  86. }
  87. }
  88. DWORD WINAPI Timer(LPVOID param) {
  89. struct ThreadArgs* timerStruct = (struct ThreadArgs*)param;
  90. while (1) {
  91. timerStruct->seconds--;
  92. flagTimer = 1;
  93. if (timerStruct->seconds == 0 && timerStruct->minutes == 0 && timerStruct->hours == 0) {
  94. flagTimer = 2;
  95. ExitThread(0);
  96. }
  97. else if (timerStruct->seconds == 0) {
  98. timerStruct->seconds = 60;
  99. timerStruct->minutes--;
  100. if (timerStruct->minutes == 0) {
  101. timerStruct->minutes = 60;
  102. timerStruct->hours--;
  103. }
  104. }
  105. Sleep(1000);
  106. }
  107. }
  108. //DWORD WINAPI Timer(LPVOID param) {
  109. // /*struct ThreadArgs* args = (struct ThreadArgs*)param;
  110. // while (1) {
  111. // if (paused) {
  112. // continue;
  113. // }
  114. // else {
  115. // if (timerMode == 0) {
  116. // printf("Âðåìÿ: %02d:%02d:%02d\n", args->hours, args->minutes, args->seconds);
  117. // args->seconds++;
  118. // if (args->seconds >= 60) {
  119. // args->seconds = 0;
  120. // args->minutes++;
  121. // if (args->minutes >= 60) {
  122. // args->minutes = 0;
  123. // args->hours++;
  124. // if (args->hours >= 24) {
  125. // args->hours = 0;
  126. // }
  127. // }
  128. // }
  129. // Sleep(1000);
  130. // }
  131. // else if (timerMode == 1) {
  132. // printf("Âðåìÿ: %02d:%02d:%02d\n", args->hours, args->minutes, args->seconds);
  133. // args->seconds++;
  134. // if (args->seconds >= 60) {
  135. // args->seconds = 0;
  136. // args->minutes++;
  137. // if (args->minutes >= 60) {
  138. // args->minutes = 0;
  139. // args->hours++;
  140. // }
  141. // }
  142. // Sleep(1000);
  143. //
  144. // }
  145. // else if (timerMode == 2) {
  146. // printf("Âðåìÿ: %02d:%02d:%02d\n", args->hours, args->minutes, args->seconds);
  147. // if (args->hours <= 0 && args->minutes <= 0 && args->seconds <= 0) {
  148. // printf("\nÒàéìåð çàêîí÷èëñÿ\n");
  149. // SYSTEMTIME st;
  150. // GetLocalTime(&st);
  151. // args->hours = st.wHour;
  152. // args->minutes = st.wMinute;
  153. // args->seconds = st.wSecond;
  154. // timerMode = 0;
  155. // }
  156. // args->seconds--;
  157. // if (args->seconds <= 0 && !(args->hours <= 0 && args->minutes <= 0 && args->seconds <= 0)) {
  158. // args->seconds = 60;
  159. // args->minutes--;
  160. // if (args->minutes <= 0 && !(args->hours <= 0 && args->minutes <= 0 && args->seconds <= 0)) {
  161. // args->minutes = 60;
  162. // args->hours--;
  163. //
  164. // }
  165. // }
  166. // Sleep(1000);
  167. //
  168. // }
  169. // }
  170. //
  171. // }
  172. // ExitThread(0);*/
  173. //}
  174. int main() {
  175. system("chcp 1251>nul");
  176. SYSTEMTIME st;
  177. GetLocalTime(&st);
  178. globalTime.hours = st.wHour;
  179. globalTime.minutes = st.wMinute;
  180. globalTime.seconds = st.wSecond;
  181. HANDLE hFMain[2];
  182. HANDLE hFSecund[2];
  183. HANDLE hFTimer[2];
  184. int choise = 0;
  185. printf("Äëÿ îñòàíîâêè âðåìåíè íàïèøèòå 1\nÄëÿ âîçîáíîâëåíèÿ âðåìåíè íàïèøèòå 7\nÄëÿ íàñòðîéêè âðåìåíè íàæìèòå 3\nÄëÿ ðåæèìà ñåêóíäîìåðà íàæìèòå 4, ÷òîáû âûéòè íàæìèòå 5\nÄëÿ ðåæèìà òàéìåðà íàæìèòå 6\n");
  186. InitializeCriticalSection(&section);
  187. struct ThreadArgs secundStruct = { 0, 0, 0, 0 };
  188. struct ThreadArgs timerStruct = { 0, 0, 0, 0 };
  189. hFMain[0] = CreateThread(NULL, 0, TimerCommon, NULL, 0, 0);
  190. hFMain[1] = CreateThread(NULL, 0, PrintTime, NULL, 0, 0);
  191. while (1) {
  192. scanf("%d", &choise);
  193. switch (choise)
  194. {
  195. case 1:
  196. SuspendThread(hFMain[1]);
  197. SuspendThread(hFMain[0]);
  198. break;
  199. case 7:
  200. ResumeThread(hFMain[0]);
  201. ResumeThread(hFMain[1]);
  202. break;
  203. case 4:
  204. hFSecund[0] = CreateThread(NULL, 0, Secundomer, &secundStruct, 0, 0);
  205. hFSecund[1] = CreateThread(NULL, 0, PrintSecundTime, &secundStruct, 0, 0);
  206. break;
  207. case 5:
  208. TerminateThread(hFSecund[0], 0);
  209. TerminateThread(hFSecund[1], 1);
  210. printf("Âû çàñåêëè %02d:%02d:%02d:%04d\n", secundStruct.hours, secundStruct.minutes, secundStruct.seconds, secundStruct.milisec);
  211. struct ThreadArgs secundStruct = { 0, 0, 0, 0 };
  212. break;
  213. case 6:
  214. printf("Ââåäèòå ÷àñû:\n");
  215. scanf("%d", &timerStruct.hours);
  216. printf("Ââåäèòå ìèíóòû:\n");
  217. scanf("%d", &timerStruct.minutes);
  218. printf("Ââåäèòå ñåêóíäû:\n");
  219. scanf("%d", &timerStruct.seconds);
  220. hFTimer[0] = CreateThread(NULL, 0, Timer, &timerStruct, 0, 0);
  221. hFTimer[1] = CreateThread(NULL, 0, PrintTimer, &timerStruct, 0, 0);
  222. struct ThreadArgs timerStruct = { 0, 0, 0, 0 };
  223. break;
  224. case 3:
  225. SuspendThread(hFMain[1]);
  226. SuspendThread(hFMain[0]);
  227. printf("Ââåäèòå ÷àñû:\n");
  228. scanf("%d", &globalTime.hours);
  229. printf("Ââåäèòå ìèíóòû:\n");
  230. scanf("%d", &globalTime.minutes);
  231. printf("Ââåäèòå ñåêóíäû:\n");
  232. scanf("%d", &globalTime.seconds);
  233. ResumeThread(hFMain[0]);
  234. ResumeThread(hFMain[1]);
  235. default:
  236. break;
  237. }
  238. }
  239. /*while (1) {
  240. int input;
  241. scanf("%d", &input);
  242. if (input == 1) {
  243. paused = 1;
  244. }
  245. else if (input == 0) {
  246. paused = 0;
  247. }
  248. else if (input == 3) {
  249. paused = 1;
  250. printf("Ââåäèòå ÷àñû");
  251. scanf("%d", &args.hours);
  252. printf("Ââåäèòå ìèíóòû");
  253. scanf("%d", &args.minutes);
  254. printf("Ââåäèòå ñåêóíäû");
  255. scanf("%d", &args.seconds);
  256. paused = 0;
  257. }
  258. else if (input == 4) {
  259. timerMode = 1;
  260. paused = 1;
  261. args.hours = 0;
  262. args.minutes = 0;
  263. args.seconds = 0;
  264. paused = 0;
  265. }
  266. else if (input == 5) {
  267. paused = 1;
  268. printf("\nÂû çàñåêëè - %d:%d:%d\n", args.hours, args.minutes, args.seconds);
  269. GetLocalTime(&st);
  270. args.hours = st.wHour;
  271. args.minutes = st.wMinute;
  272. args.seconds = st.wSecond;
  273. timerMode = 0;
  274. paused = 0;
  275. }
  276. else if (input == 6) {
  277. paused = 1;
  278. printf("Ââåäèòå ÷àñû äëÿ òàéìåðà");
  279. scanf("%d", &args.hours);
  280. printf("Ââåäèòå ìèíóòû äëÿ òàéìåðà");
  281. scanf("%d", &args.minutes);
  282. printf("Ââåäèòå ñåêóíäû äëÿ òàéìåðà");
  283. scanf("%d", &args.seconds);
  284. timerMode = 2;
  285. paused = 0;
  286. }
  287. }*/
  288. WaitForMultipleObjects(2, hFMain, TRUE, INFINITE);
  289. DeleteCriticalSection(&section);
  290. }