Created
March 23, 2012 13:27
-
-
Save janithl/2170614 to your computer and use it in GitHub Desktop.
Silly little C code I wrote to record the time I spent doing past paper questions. Not technically sound or anything, but it works for me. :P
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Silly program to lap time and stuff, by Janith Leanage. | |
Uses signal handling code. :P | |
*/ | |
#include<stdio.h> | |
#include<signal.h> | |
int count[100]; | |
int c = 0; | |
int maxtimes = 5; | |
/* | |
Signal hanlder method | |
Prints output in a neat format, and finally prints the | |
total time elapsed | |
*/ | |
void printoutput() | |
{ | |
signal(SIGINT, printoutput); | |
int min = count[c]/60; | |
int sec = count[c]%60; | |
char *format = (sec < 10) ? "\n\t\t%2d:0%1d\t\t" : "\n\t\t%2d:%2d\t\t"; | |
printf(format, min, sec); | |
c++; | |
if(c >= maxtimes) | |
{ | |
int i; | |
for(i = 1; i < maxtimes; i++) | |
count[0] += count[i]; | |
printf("\n\t\t-----\nTotal time:\t"); | |
format = ((count[0]%60) < 10) ? "%2d:0%1d" : "%2d:%2d"; | |
printf(format, (count[0]/60), (count[0]%60)); | |
printf("\n\t\t=====\n\n"); | |
exit(0); | |
} | |
} | |
/* | |
Main method | |
Parses command line arguments and uses a primitive sleep | |
system call to count the number of seconds elapsed | |
*/ | |
main(int argc, char *argv[]) | |
{ | |
int x; | |
signal(SIGINT, printoutput); | |
if(argc > 1) | |
{ | |
maxtimes = atoi(argv[1]); | |
if(maxtimes < 1 || maxtimes > 100) | |
{ | |
maxtimes = 5; | |
printf("The argument you passed was invalid. Please select a "); | |
printf("number\nbetween 1 and 100. (Number has been set to 5 by default)\n\n"); | |
} | |
} | |
else | |
printf("You can pass the maximum amount of times (between 1 and 100)\n"); | |
printf("you need as a command line argument. (Now set to 5 by default)\n\n"); | |
for(x = 0; x < maxtimes; x++) | |
count[x] = 0; | |
while(1) | |
{ | |
sleep(1); | |
count[c]++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment