-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatchFinal.c
55 lines (47 loc) · 1.46 KB
/
stopwatchFinal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <conio.h>
int main() {
time_t startTime, currentTime, pausedTime;
time(&startTime);
int elapsedTime = 0, key, stop=0;
printf("Press 's' key to start the stopwatch\n");
char c = getch();
if(c == 's') {
time(&startTime);
} else {
printf("Invalid option.");
return 1;
}
printf("Press 'p' key to pause the stopwatch\n");
printf("Press 'r' key to resume the stopwatch\n");
printf("Press 's' key to stop the stopwatch\n");
while (stop == 0) {
time(¤tTime);
int elapsedTime = (int) difftime(currentTime, startTime);
int hours = elapsedTime / 3600;
int minutes = (elapsedTime % 3600) / 60;
int seconds = elapsedTime % 60;
printf("Elapsed time: %02d:%02d:%02d\r", hours, minutes, seconds);
sleep(1);
if (_kbhit()){
key = _getch(); //watches for keypress
if (key == 'p'){
time(&pausedTime);
printf("\nStopwatch Paused\n");
while (key != 'r'){
if (_kbhit()){
key = _getch();
}
}
startTime = startTime + (time(NULL) - pausedTime);
printf("Stopwatch Resumed\n");
}
else if (key == 's'){
stop = 1;
printf("\nYou pressed stop\n");
}
}
}
}