main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <locale.h>
  5. #include <unistd.h>
  6. #include <pthread.h>
  7. typedef struct {
  8. int hours;
  9. int minutes;
  10. int seconds;
  11. } Time;
  12. volatile int running = 1;
  13. volatile int timerMode = 0;
  14. Time currentTime;
  15. Time timerTime;
  16. void *threadClock(void *arg); //отдельный поток работы часов
  17. void *threadControlClock(void *arg); //поток управления часами
  18. int main() {
  19. pthread_t clockThread[2];
  20. clockThread[0] = pthread_create(&clockThread[0], NULL, threadClock, &currentTime);
  21. clockThread[1] = pthread_create(&clockThread[1], NULL, threadControlClock, &timerTime);
  22. sleep(1);
  23. char command;
  24. while (1) {
  25. printf("\nВведите команду (p - пауза, r - продолжить, s - установить время, t - таймер, m - секундомер, q - выход): \n");
  26. scanf(" %c", &command);
  27. switch (command) {
  28. case 'p':
  29. running = 0;
  30. printf("Часы остановлены.\n");
  31. break;
  32. case 'r':
  33. running = 1;
  34. printf("\033c");
  35. printf("Часы запущены.\n");
  36. break;
  37. case 's':
  38. printf("Введите новое время (часы минуты секунды): ");
  39. scanf("%d %d %d", &currentTime.hours, &currentTime.minutes, &currentTime.seconds);
  40. printf("\033c");
  41. break;
  42. case 't':
  43. printf("\033c");
  44. printf("Режим таймера.\n");
  45. printf("Введите время таймера (часы минуты секунды): ");
  46. scanf("%d %d %d", &timerTime.hours, &timerTime.minutes, &timerTime.seconds);
  47. timerMode = 1;
  48. break;
  49. case 'm':
  50. timerMode = 2;
  51. printf("\033c");
  52. printf("Режим секундомера.\n");
  53. timerTime.hours = 0;
  54. timerTime.minutes = 0;
  55. timerTime.seconds = 0;
  56. break;
  57. case 'q':
  58. running = 0;
  59. pthread_join(clockThread[0], NULL);
  60. pthread_join(clockThread[1], NULL);
  61. return 0;
  62. default:
  63. printf("Неверная команда.\n");
  64. break;
  65. }
  66. }
  67. }
  68. void *threadClock(void *arg) {
  69. Time *current = (Time *)arg;
  70. time_t timer;
  71. struct tm *tm_info;
  72. time(&timer);
  73. tm_info = localtime(&timer);
  74. current->hours = tm_info->tm_hour;
  75. current->minutes = tm_info->tm_min;
  76. current->seconds = tm_info->tm_sec;
  77. while (1) {
  78. if (running) {
  79. current->seconds++;
  80. if (current->seconds == 60) {
  81. current->seconds = 0;
  82. current->minutes++;
  83. if (current->minutes == 60) {
  84. current->minutes = 0;
  85. current->hours++;
  86. if (current->hours == 24) {
  87. current->hours = 0;
  88. }
  89. }
  90. }
  91. printf("\rТекущее время: %02d:%02d:%02d", current->hours, current->minutes, current->seconds);
  92. fflush(stdout); // Flush output buffer
  93. sleep(1);
  94. } else {
  95. sleep(1);
  96. }
  97. }
  98. return NULL;
  99. }
  100. void *threadControlClock(void *arg) {
  101. Time *timer = (Time *)arg;
  102. while (running) {
  103. if (timerMode == 1) {
  104. if (timer->seconds > 0) {
  105. timer->seconds--;
  106. } else if (timer->minutes > 0) {
  107. timer->minutes--;
  108. timer->seconds = 59;
  109. } else if (timer->hours > 0) {
  110. timer->hours--;
  111. timer->minutes = 59;
  112. timer->seconds = 59;
  113. } else {
  114. printf("\nВремя таймера вышло!\n");
  115. timerMode = 0;
  116. }
  117. printf("\rТаймер: %02d:%02d:%02d", timer->hours, timer->minutes, timer->seconds);
  118. fflush(stdout);
  119. } else if (timerMode == 2) {
  120. timer->seconds++;
  121. if (timer->seconds == 60) {
  122. timer->seconds = 0;
  123. timer->minutes++;
  124. if (timer->minutes == 60) {
  125. timer->minutes = 0;
  126. timer->hours++;
  127. }
  128. }
  129. printf("\rСекундомер: %02d:%02d:%02d", timer->hours, timer->minutes, timer->seconds);
  130. fflush(stdout);
  131. }
  132. sleep(1);
  133. }
  134. return NULL;
  135. }