Originally Posted by
bithub
Adak: Where is that delay() function coming from?
Oh Gawd! I slipped into Turbo C mode, didn't I?
Salem was asking "Why would anyone want to use Turbo C?" For me, it's because TC lets me do these kind of little programs with one hand behind my back, blindfolded.
Code:
/* Adak, Sept 23, '09.
stopwatch type program for Turbo C. Adjust loss or delay(),
for increased accuracy.
Status: ok
*/
#include <stdio.h>
#include <time.h> //for clock()
#include <dos.h> //for delay
#include <conio.h> //for getch()
#define ESC 27
//27 = 0x1B
//VK_ESCAPE (0x1B) ESC key- trap the key press maybe for Windows ?
int main(void) {
clock_t start, stop, loss;
int i, seconds1, seconds2, total_sec, keyPress;
loss = 20;
total_sec = 0;
start = clock();
seconds1 = start / CLK_TCK; //divide by TICKS_PER_SEC for Visual C;
for(i = 0; i < 36; ++i)
putchar('\n');
gotoxy(10, 8);
printf("Press ESC to Quit");
do {
/*pause in milliseconds (the OS will "schedule" the actual delay),
giving it at least 200, but usually more)
*/
delay(200);
stop = clock();
start = stop + loss; //See note
seconds2 = stop / CLK_TCK; //TICKS_PER_SEC;
if(seconds2 > seconds1) {
seconds1 = seconds2;
total_sec++;
/* SetConsoleCursorPosition for Windows (Visual C), (or gotoxy() if you have
conio.h), is good for this:
*/
gotoxy(10, 10);
printf( "Elapsed Time: %d seconds", total_sec);
if(kbhit())
keyPress = getch();
}
} while(keyPress != ESC);
printf("\n\n\n\t\t\t press enter when ready");
i = getchar();
return 0;
}
It's apparently accurate for 60 seconds, in an off-hand test. It would need adjusting to be even close to accurate.
This version gives a more "stopwatch" look to it:
Code:
/* Adak, Sept 23, '09.
stopwatch type program for Turbo C. Adjust loss or delay(),
for increased accuracy.
Status: ok
*/
#include <stdio.h>
#include <time.h>
#include <dos.h>
#include <conio.h>
#define ESC 27
//27 = 0x1B
//VK_ESCAPE (0x1B) ESC key- trap the key press
int main(void) {
clock_t start, stop, loss;
int i, seconds1, seconds2, total_sec, keyPress;
loss = 20;
total_sec = 0;
start = clock();
seconds1 = start / CLK_TCK; //TICKS_PER_SEC;
for(i = 0; i < 36; ++i)
putchar('\n');
gotoxy(10, 8);
printf("Press ESC to Quit");
do {
/*pause in milliseconds (the OS will "schedule" the actual delay),
giving it at least what you request, but usually more)
*/
delay(100);
stop = clock();
// start = stop + loss; //See note
// seconds2 = stop / CLK_TCK; //TICKS_PER_SEC;
gotoxy(10,10);
printf("Elapsed Time: %.2f ", (stop-start)/CLK_TCK);
/*
if(seconds2 > seconds1) {
seconds1 = seconds2;
total_sec++;
// SetConsoleCursorPosition (or gotoxy() if you have conio.h), is good for this:
gotoxy(10, 10);
printf( "Elapsed Time: %d seconds", total_sec);
}
*/
if(kbhit())
keyPress = getch();
} while(keyPress != ESC);
printf("\n\n\n\t\t\t press enter when ready");
seconds1 = getchar();
return 0;
}