Chào mọi người! Hiện giờ em đang tạo 1 chương trình gồm menu và đồng hồ hiển thị giờ ngay tại menu luôn. Em có xem qua multithreading nhưng nó lại có nhược điểm như đồng hồ làm mới liên tục theo giây nên khi mình muốn dùng phím mũi tên cho menu của mình sẽ quay lại cái nơi đồng hồ tạo mới ( vì em dùng gotoxy ). Ai có gợi ý cho em dùng kiểu gì chạy 2 chương trình song song mà không ảnh hưởng tới chương trình kia không ạ?

#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <pthread.h>
#include <unistd.h>
void gotoxy(int x, int y) {
COORD pos = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int wherex() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.X;
}
int wherey() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.Y;
}
void* move(void * arg){
char op1[10] = "Option1", op2[10] = "Option2";
int x, y, ch, c;
gotoxy(10,6);
printf("%s", op1);
gotoxy(10,7);
printf("%s", op2);
do
{
ch = getch();
if (ch == 224)
{
ch = getch();
x = wherex();
y = wherey();
switch (ch)
{
case 72:
gotoxy(x, y - 1);
break;
case 80:
gotoxy(x, y + 1);
break;
// case 77:
// gotoxy(x + 8, y);
// break;
// case 75:
// gotoxy(x - 8, y);
// break;
}
}
} while (ch != 13);
printf("Long");
return NULL;
}
int main(){
int sec, min, hour, day, month, year, day2;
pthread_t newthread;
pthread_create(&newthread, NULL, &move, NULL);
// move();
while(1){
// system("cls");
time_t now;
time(&now);
struct tm *local = localtime(&now);
sec = local->tm_sec;
min = local->tm_min;
hour = local->tm_hour;
day = local->tm_mday;
day2 = local->tm_wday;
month = local->tm_mon + 1;
year = local->tm_year + 1900;
gotoxy(3,1);
switch(day2){
case 0:
printf("Sun");
break;
case 1:
printf("Mon");
break;
case 2:
printf("Tue");
break;
case 3:
printf("Wed");
break;
case 4:
printf("Thu");
break;
case 5:
printf("Fri");
break;
case 6:
printf("Sat");
break;
}
printf(" %02d/%02d/%d ", day, month, year);
printf("%d:%d:%d", hour, min, sec);
sleep(1);
}
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?