Solve.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <windows.h>
  7. #include <math.h>
  8. // Ãëîáàëüíûå ïåðåìåííûå äëÿ óïðàâëåíèÿ
  9. int hours = 0, minutes = 0, seconds = 0;
  10. int running = 1; // Ôëàã ðàáîòû ÷àñîâ
  11. int paused = 0; // Ôëàã ïàóçû
  12. HANDLE clockThread; // Äåñêðèïòîð ïîòîêà
  13. // Ôóíêöèÿ äëÿ óñòàíîâêè êóðñîðà â óêàçàííóþ ïîçèöèþ
  14. void setCursorPosition(int x, int y) {
  15. COORD coord;
  16. coord.X = x;
  17. coord.Y = y;
  18. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  19. }
  20. // Ôóíêöèÿ îòîáðàæåíèÿ âðåìåíè
  21. void displayTime() {
  22. setCursorPosition(0, 0);
  23. printf("Òåêóùåå âðåìÿ: %02d:%02d:%02d\n", hours, minutes, seconds);
  24. fflush(stdout);
  25. }
  26. // Ïîòîê äëÿ ÷àñîâ
  27. DWORD WINAPI clockFunction(LPVOID lpParam) {
  28. while (running) {
  29. if (!paused) {
  30. Sleep(1000); // Çàäåðæêà â 1 ñåêóíäó
  31. seconds++;
  32. if (seconds == 60) {
  33. seconds = 0;
  34. minutes++;
  35. if (minutes == 60) {
  36. minutes = 0;
  37. hours++;
  38. if (hours == 24) {
  39. hours = 0;
  40. }
  41. }
  42. }
  43. displayTime();
  44. }
  45. }
  46. return 0;
  47. }
  48. // Ôóíêöèÿ íàñòðîéêè âðåìåíè
  49. void setTime() {
  50. printf("\nÂâåäèòå ÷àñû: \n");
  51. scanf("%d", &hours);
  52. printf("Ââåäèòå ìèíóòû: \n");
  53. scanf("%d", &minutes);
  54. printf("Ââåäèòå ñåêóíäû: \n");
  55. scanf("%d", &seconds);
  56. displayTime();
  57. }
  58. // Ñåêóíäîìåð
  59. void stopwatch() {
  60. int sw_hours = 0, sw_minutes = 0, sw_seconds = 0;
  61. printf("\nÑåêóíäîìåð çàïóùåí. Íàæìèòå ëþáóþ êëàâèøó äëÿ îñòàíîâêè.\n");
  62. while (!kbhit()) {
  63. Sleep(1000);
  64. sw_seconds++;
  65. if (sw_seconds == 60) {
  66. sw_seconds = 0;
  67. sw_minutes++;
  68. if (sw_minutes == 60) {
  69. sw_minutes = 0;
  70. sw_hours++;
  71. }
  72. }
  73. setCursorPosition(0, 2);
  74. printf("\nÑåêóíäîìåð: %02d:%02d:%02d \n", sw_hours, sw_minutes, sw_seconds);
  75. fflush(stdout);
  76. }
  77. getchar(); // Îæèäàíèå íàæàòèÿ êëàâèøè
  78. setCursorPosition(0, 2);
  79. printf(" "); // Î÷èñòêà ñòðîêè
  80. }
  81. // Òàéìåð
  82. void timer() {
  83. int t_hours = 0, t_minutes = 0, t_seconds = 0;
  84. printf("\nÂâåäèòå âðåìÿ òàéìåðà (÷àñû, ìèíóòû, ñåêóíäû):\n");
  85. printf("×àñû: \n");
  86. scanf("%d", &t_hours);
  87. printf("Ìèíóòû: \n");
  88. scanf("%d", &t_minutes);
  89. printf("Ñåêóíäû: \n");
  90. scanf("%d", &t_seconds);
  91. printf("\nÒàéìåð çàïóùåí.\n");
  92. while (t_hours > 0 || t_minutes > 0 || t_seconds > 0) {
  93. Sleep(1000);
  94. if (t_seconds > 0) {
  95. t_seconds--;
  96. }
  97. else {
  98. t_seconds = 59;
  99. if (t_minutes > 0) {
  100. t_minutes--;
  101. }
  102. else {
  103. t_minutes = 59;
  104. if (t_hours > 0) {
  105. t_hours--;
  106. }
  107. }
  108. }
  109. setCursorPosition(0, 3);
  110. printf("Òàéìåð: %02d:%02d:%02d \n", t_hours, t_minutes, t_seconds);
  111. fflush(stdout);
  112. }
  113. printf("\nÒàéìåð çàâåðøåí!\n");
  114. setCursorPosition(0, 3);
  115. }
  116. // Îñíîâíàÿ ôóíêöèÿ
  117. int main() {
  118. system("chcp 1251>null");
  119. int choice;
  120. // Çàïóñê ïîòîêà ÷àñîâ
  121. clockThread = CreateThread(NULL, 0, clockFunction, NULL, 0, NULL);
  122. do {
  123. setCursorPosition(0, 5); // Ïåðåìåñòèòü êóðñîð ïîä ÷àñû
  124. printf("\nÌåíþ óïðàâëåíèÿ ÷àñàìè:\n");
  125. printf("1. Ïàóçà/Ïðîäîëæèòü\n");
  126. printf("2. Íàñòðîéêà âðåìåíè\n");
  127. printf("3. Ñåêóíäîìåð\n");
  128. printf("4. Òàéìåð\n");
  129. printf("5. Âûõîä\n");
  130. printf("\nÂûáåðèòå îïöèþ: ");
  131. scanf("%d", &choice);
  132. switch (choice) {
  133. case 1:
  134. paused = !paused;
  135. if (paused) {
  136. printf("\n×àñû íà ïàóçå.\n");
  137. }
  138. else {
  139. printf("\n×àñû ïðîäîëæàþò ðàáîòàòü.\n");
  140. }
  141. break;
  142. case 2:
  143. setTime();
  144. break;
  145. case 3:
  146. stopwatch();
  147. break;
  148. case 4:
  149. timer();
  150. break;
  151. case 5:
  152. running = 0;
  153. WaitForSingleObject(clockThread, INFINITE); // Îæèäàíèå çàâåðøåíèÿ ïîòîêà
  154. CloseHandle(clockThread);
  155. printf("\nÏðîãðàììà çàâåðøåíà.\n");
  156. break;
  157. default:
  158. printf("\nÍåâåðíûé âûáîð, ïîïðîáóéòå ñíîâà.\n");
  159. }
  160. } while (choice != 5);
  161. return 0;
  162. }