123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <time.h>
- #include <locale.h>
- #include <unistd.h>
- #include <pthread.h>
- typedef struct {
- int hours;
- int minutes;
- int seconds;
- } Time;
- volatile int running = 1;
- volatile int timerMode = 0;
- Time currentTime;
- Time timerTime;
- void *threadClock(void *arg); //отдельный поток работы часов
- void *threadControlClock(void *arg); //поток управления часами
- int main() {
- pthread_t clockThread[2];
- clockThread[0] = pthread_create(&clockThread[0], NULL, threadClock, ¤tTime);
- clockThread[1] = pthread_create(&clockThread[1], NULL, threadControlClock, &timerTime);
- sleep(1);
- char command;
- while (1) {
- printf("\nВведите команду (p - пауза, r - продолжить, s - установить время, t - таймер, m - секундомер, q - выход): \n");
- scanf(" %c", &command);
- switch (command) {
- case 'p':
- running = 0;
- printf("Часы остановлены.\n");
- break;
- case 'r':
- running = 1;
- printf("\033c");
- printf("Часы запущены.\n");
- break;
- case 's':
- printf("Введите новое время (часы минуты секунды): ");
- scanf("%d %d %d", ¤tTime.hours, ¤tTime.minutes, ¤tTime.seconds);
- printf("\033c");
- break;
- case 't':
- printf("\033c");
- printf("Режим таймера.\n");
- printf("Введите время таймера (часы минуты секунды): ");
- scanf("%d %d %d", &timerTime.hours, &timerTime.minutes, &timerTime.seconds);
- timerMode = 1;
- break;
- case 'm':
- timerMode = 2;
- printf("\033c");
- printf("Режим секундомера.\n");
- timerTime.hours = 0;
- timerTime.minutes = 0;
- timerTime.seconds = 0;
- break;
- case 'q':
- running = 0;
- pthread_join(clockThread[0], NULL);
- pthread_join(clockThread[1], NULL);
- return 0;
- default:
- printf("Неверная команда.\n");
- break;
- }
- }
- }
- void *threadClock(void *arg) {
- Time *current = (Time *)arg;
- time_t timer;
- struct tm *tm_info;
- time(&timer);
- tm_info = localtime(&timer);
- current->hours = tm_info->tm_hour;
- current->minutes = tm_info->tm_min;
- current->seconds = tm_info->tm_sec;
- while (1) {
- if (running) {
- current->seconds++;
- if (current->seconds == 60) {
- current->seconds = 0;
- current->minutes++;
- if (current->minutes == 60) {
- current->minutes = 0;
- current->hours++;
- if (current->hours == 24) {
- current->hours = 0;
- }
- }
- }
- printf("\rТекущее время: %02d:%02d:%02d", current->hours, current->minutes, current->seconds);
- fflush(stdout); // Flush output buffer
- sleep(1);
- } else {
- sleep(1);
- }
- }
- return NULL;
- }
- void *threadControlClock(void *arg) {
- Time *timer = (Time *)arg;
- while (running) {
- if (timerMode == 1) {
- if (timer->seconds > 0) {
- timer->seconds--;
- } else if (timer->minutes > 0) {
- timer->minutes--;
- timer->seconds = 59;
- } else if (timer->hours > 0) {
- timer->hours--;
- timer->minutes = 59;
- timer->seconds = 59;
- } else {
- printf("\nВремя таймера вышло!\n");
- timerMode = 0;
- }
- printf("\rТаймер: %02d:%02d:%02d", timer->hours, timer->minutes, timer->seconds);
- fflush(stdout);
- } else if (timerMode == 2) {
- timer->seconds++;
- if (timer->seconds == 60) {
- timer->seconds = 0;
- timer->minutes++;
- if (timer->minutes == 60) {
- timer->minutes = 0;
- timer->hours++;
- }
- }
- printf("\rСекундомер: %02d:%02d:%02d", timer->hours, timer->minutes, timer->seconds);
- fflush(stdout);
- }
- sleep(1);
- }
- return NULL;
- }
|