Time Calculator/src/timec.h (Time Calculator)
//This file contains function declarations and implementations for all non-file handling related functions
//used by main.c
#define USAGE "timec [OPTION] [ARGUMENT]... (The time is entered in 24-hour clock)\n"
#define INVALID_OPTION "The option you provided is invalid, type \"timec -h\" for help\n"
#define TIME_LENGTH 6
#define HELP "Enter \"typec -h\" for help\n"
typedef struct {
int hour;
int minute;
} Time;
//parseTime() function is used to parse time from a string and put the parsed hour and minute
//into the members of the provided Time struct using its pointer
void parseTime(char* str,Time* time);
//putTime function is used to convert Time struc into a string
void putTime(char* str,Time* time);
//calcDiffH() is used to calculate the difference between two given hours
int calcDiffH(Time* start,Time* end);
//calcDiffM() is used to calculate the difference between two given minutes
int calcDiffM(Time* start,Time* end);
//addTime() function is used two add two time values like
//5:00 and 4:00 will be 9:00 so it will return a pointer to a Time struct containing the corresponding hour and min
Time* addTime(Time* time1,Time* time2);
//subTime() is like addTime() but it subtracts and be sure to provide big time value as arguments first when using it from
//the command line i.e "timec -s 2:10 2:00"
Time* subTime(Time* time1,Time* time2);
//calcTime() calculates the hours passed between two time points for ex :-
//2:10 to 3:30 so it will return a Time* containing 1 as hour and 20 as min asthis is the time passed between these time points
Time* calcTime(Time* startTime,Time* endTime);
//calculateIt() is defined so the main script doesn't get cluttered
//and every function with "It" in the last is used for this purpose
//and every "It" function returns a int (0 if the execution is successfull otherwise 1) except helpIt()
//which just prints the help
//calculateIt() executes the calcTime() function and frees memory if the user enters the option associated with it
int calculateIt(int argc,char** argv);
//Same as calculateIt() it adds two time point given by user with addTime(), prints the result and frees memory
int addIt(int argc,char** argv);
//Same as addIt() but for subTime() function instead
int subIt(int argc,char** argv);
//Used to display help and the only "It" function which doesn't return anything
void helpIt();
void parseTime(char* str,Time* time){
sscanf(str,"%d:%d",&(time->hour),&(time->minute));
}
void putTime(char* str,Time* time){
if(time->minute < 10){
sprintf(str,"%d:0%d",time->hour,time->minute);
} else{
sprintf(str,"%d:%d",time->hour,time->minute);
}
}
int calcDiffH(Time* start,Time* end){
if(start->hour > end->hour){
return -(start->hour - end->hour);
} else if(start->hour == end->hour){
return 00;
} else {
return end->hour - start->hour;
}
}
int calcDiffM(Time* start,Time* end){
if(start->minute > end->minute){
return -(start->minute - end->minute);
} else if(start->minute == end->minute){
return 00;
} else {
return end->minute - start->minute;
}
}
Time* calcTime(Time* startTime,Time* endTime){
Time* calculatedTime = (Time*) malloc(sizeof(Time));
calculatedTime->hour = calcDiffH(startTime,endTime);
calculatedTime->minute = calcDiffM(startTime,endTime);
if (calculatedTime->minute < 0){
calculatedTime->hour -= 1;
calculatedTime->minute = 60 + calculatedTime->minute;
}
return calculatedTime;
}
Time* addTime(Time* time1,Time* time2)
{
Time* addedTime = (Time*) malloc(sizeof(Time));
addedTime->hour = time1->hour + time2->hour;
addedTime->minute = time1->minute + time2->minute;
if (addedTime->minute > 60){
addedTime->hour += 1;
addedTime->minute = addedTime->minute - 60;
} else if(addedTime->minute == 60){
addedTime->hour += 1;
addedTime->minute = 00;
}
return addedTime;
}
Time* subTime(Time* time1,Time* time2)
{
Time* subtractedTime = (Time*) malloc(sizeof(Time));
subtractedTime->hour = time1->hour - time2->hour;
subtractedTime->minute = time1->minute - time2->minute;
if (subtractedTime->minute < 0){
subtractedTime->hour -= 1;
subtractedTime->minute = 60 + subtractedTime->minute;
}
return subtractedTime;
}
int calculateIt(int argc,char** argv){
if (argc < 4){
printf("Invalid usage\n");
printf(HELP);
return 1;
}
Time start,end;
parseTime(argv[2],&start);
parseTime(argv[3],&end);
Time* result = calcTime(&start,&end);
char stringResult[TIME_LENGTH];
putTime(stringResult,result);
printf("%s\n",stringResult);
free(result);
return 0;
}
int addIt(int argc,char** argv){
if (argc < 4){
printf("Invalid usage\n");
printf(HELP);
return 1;
}
Time time1,time2;
parseTime(argv[2],&time1);
parseTime(argv[3],&time2);
Time* result = addTime(&time1,&time2);
char stringResult[TIME_LENGTH];
putTime(stringResult,result);
printf("%s\n",stringResult);
free(result);
return 0;
}
int subIt(int argc,char** argv){
if (argc < 4){
printf("Invalid usage\n");
printf(HELP);
return 1;
}
Time time1,time2;
parseTime(argv[2],&time1);
parseTime(argv[3],&time2);
Time* result = subTime(&time1,&time2);
char stringResult[TIME_LENGTH];
putTime(stringResult,result);
printf("%s\n",stringResult);
free(result);
return 0;
}
void helpIt() {
printf("Time Calculator Program\n");
printf("Usage:\n");
printf("\ttimec [OPTION] [ARGUMENT]... (The time is entered in 24-hour clock)\n\n");
printf("Options:\n");
printf("\t-a [Time 1] [Time 2]\tAdd two time values (e.g., \"timec -a 1:10 2:20\" outputs 3:30)\n");
printf("\t-s [Time 1] [Time 2]\tSubtract two time values (e.g., \"timec -s 2:30 1:10\" outputs 1:20)\n");
printf("\t-c [Start Time] [End Time]\tCalculate passed time from given time points (e.g., \"timec -c 12:00 13:10\" outputs 1:10)\n");
printf("\t-h\t\tPrint this help message\n");
printf("\t-f [File]\tCalculates the total time of the users in the file\n");
}
//used by main.c
#define USAGE "timec [OPTION] [ARGUMENT]... (The time is entered in 24-hour clock)\n"
#define INVALID_OPTION "The option you provided is invalid, type \"timec -h\" for help\n"
#define TIME_LENGTH 6
#define HELP "Enter \"typec -h\" for help\n"
typedef struct {
int hour;
int minute;
} Time;
//parseTime() function is used to parse time from a string and put the parsed hour and minute
//into the members of the provided Time struct using its pointer
void parseTime(char* str,Time* time);
//putTime function is used to convert Time struc into a string
void putTime(char* str,Time* time);
//calcDiffH() is used to calculate the difference between two given hours
int calcDiffH(Time* start,Time* end);
//calcDiffM() is used to calculate the difference between two given minutes
int calcDiffM(Time* start,Time* end);
//addTime() function is used two add two time values like
//5:00 and 4:00 will be 9:00 so it will return a pointer to a Time struct containing the corresponding hour and min
Time* addTime(Time* time1,Time* time2);
//subTime() is like addTime() but it subtracts and be sure to provide big time value as arguments first when using it from
//the command line i.e "timec -s 2:10 2:00"
Time* subTime(Time* time1,Time* time2);
//calcTime() calculates the hours passed between two time points for ex :-
//2:10 to 3:30 so it will return a Time* containing 1 as hour and 20 as min asthis is the time passed between these time points
Time* calcTime(Time* startTime,Time* endTime);
//calculateIt() is defined so the main script doesn't get cluttered
//and every function with "It" in the last is used for this purpose
//and every "It" function returns a int (0 if the execution is successfull otherwise 1) except helpIt()
//which just prints the help
//calculateIt() executes the calcTime() function and frees memory if the user enters the option associated with it
int calculateIt(int argc,char** argv);
//Same as calculateIt() it adds two time point given by user with addTime(), prints the result and frees memory
int addIt(int argc,char** argv);
//Same as addIt() but for subTime() function instead
int subIt(int argc,char** argv);
//Used to display help and the only "It" function which doesn't return anything
void helpIt();
void parseTime(char* str,Time* time){
sscanf(str,"%d:%d",&(time->hour),&(time->minute));
}
void putTime(char* str,Time* time){
if(time->minute < 10){
sprintf(str,"%d:0%d",time->hour,time->minute);
} else{
sprintf(str,"%d:%d",time->hour,time->minute);
}
}
int calcDiffH(Time* start,Time* end){
if(start->hour > end->hour){
return -(start->hour - end->hour);
} else if(start->hour == end->hour){
return 00;
} else {
return end->hour - start->hour;
}
}
int calcDiffM(Time* start,Time* end){
if(start->minute > end->minute){
return -(start->minute - end->minute);
} else if(start->minute == end->minute){
return 00;
} else {
return end->minute - start->minute;
}
}
Time* calcTime(Time* startTime,Time* endTime){
Time* calculatedTime = (Time*) malloc(sizeof(Time));
calculatedTime->hour = calcDiffH(startTime,endTime);
calculatedTime->minute = calcDiffM(startTime,endTime);
if (calculatedTime->minute < 0){
calculatedTime->hour -= 1;
calculatedTime->minute = 60 + calculatedTime->minute;
}
return calculatedTime;
}
Time* addTime(Time* time1,Time* time2)
{
Time* addedTime = (Time*) malloc(sizeof(Time));
addedTime->hour = time1->hour + time2->hour;
addedTime->minute = time1->minute + time2->minute;
if (addedTime->minute > 60){
addedTime->hour += 1;
addedTime->minute = addedTime->minute - 60;
} else if(addedTime->minute == 60){
addedTime->hour += 1;
addedTime->minute = 00;
}
return addedTime;
}
Time* subTime(Time* time1,Time* time2)
{
Time* subtractedTime = (Time*) malloc(sizeof(Time));
subtractedTime->hour = time1->hour - time2->hour;
subtractedTime->minute = time1->minute - time2->minute;
if (subtractedTime->minute < 0){
subtractedTime->hour -= 1;
subtractedTime->minute = 60 + subtractedTime->minute;
}
return subtractedTime;
}
int calculateIt(int argc,char** argv){
if (argc < 4){
printf("Invalid usage\n");
printf(HELP);
return 1;
}
Time start,end;
parseTime(argv[2],&start);
parseTime(argv[3],&end);
Time* result = calcTime(&start,&end);
char stringResult[TIME_LENGTH];
putTime(stringResult,result);
printf("%s\n",stringResult);
free(result);
return 0;
}
int addIt(int argc,char** argv){
if (argc < 4){
printf("Invalid usage\n");
printf(HELP);
return 1;
}
Time time1,time2;
parseTime(argv[2],&time1);
parseTime(argv[3],&time2);
Time* result = addTime(&time1,&time2);
char stringResult[TIME_LENGTH];
putTime(stringResult,result);
printf("%s\n",stringResult);
free(result);
return 0;
}
int subIt(int argc,char** argv){
if (argc < 4){
printf("Invalid usage\n");
printf(HELP);
return 1;
}
Time time1,time2;
parseTime(argv[2],&time1);
parseTime(argv[3],&time2);
Time* result = subTime(&time1,&time2);
char stringResult[TIME_LENGTH];
putTime(stringResult,result);
printf("%s\n",stringResult);
free(result);
return 0;
}
void helpIt() {
printf("Time Calculator Program\n");
printf("Usage:\n");
printf("\ttimec [OPTION] [ARGUMENT]... (The time is entered in 24-hour clock)\n\n");
printf("Options:\n");
printf("\t-a [Time 1] [Time 2]\tAdd two time values (e.g., \"timec -a 1:10 2:20\" outputs 3:30)\n");
printf("\t-s [Time 1] [Time 2]\tSubtract two time values (e.g., \"timec -s 2:30 1:10\" outputs 1:20)\n");
printf("\t-c [Start Time] [End Time]\tCalculate passed time from given time points (e.g., \"timec -c 12:00 13:10\" outputs 1:10)\n");
printf("\t-h\t\tPrint this help message\n");
printf("\t-f [File]\tCalculates the total time of the users in the file\n");
}
Comments
Post a Comment