-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add libraryFine.c to calculate library late return fines
This program determines library fines based on book return dates. It calculates fines according to the difference between due and actual return dates, using rules for days, months, and years. Includes helper functions for input handling and string trimming.
- Loading branch information
Showing
1 changed file
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
#include <assert.h> | ||
#include <ctype.h> | ||
#include <limits.h> | ||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
char* readline(); | ||
char* ltrim(char*); | ||
char* rtrim(char*); | ||
char** split_string(char*); | ||
|
||
int parse_int(char*); | ||
|
||
/* | ||
* Complete the 'libraryFine' function below. | ||
* | ||
* The function is expected to return an INTEGER. | ||
* The function accepts following parameters: | ||
* 1. INTEGER d1 | ||
* 2. INTEGER m1 | ||
* 3. INTEGER y1 | ||
* 4. INTEGER d2 | ||
* 5. INTEGER m2 | ||
* 6. INTEGER y2 | ||
*/ | ||
|
||
int libraryFine(int d1, int m1, int y1, int d2, int m2, int y2) { | ||
// Case 1: If returned on or before the due date | ||
if (y1 < y2 || (y1 == y2 && m1 < m2) || (y1 == y2 && m1 == m2 && d1 <= d2)) { | ||
return 0; // No fine | ||
} | ||
|
||
// Case 2: If returned within the same year and month but late | ||
if (y1 == y2 && m1 == m2) { | ||
return 15 * (d1 - d2); | ||
} | ||
|
||
// Case 3: If returned within the same year but in a later month | ||
if (y1 == y2 && m1 > m2) { | ||
return 500 * (m1 - m2); | ||
} | ||
|
||
// Case 4: If returned in a different year | ||
if (y1 > y2) { | ||
return 10000; | ||
} | ||
|
||
// Default (just in case) | ||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); | ||
|
||
char** first_multiple_input = split_string(rtrim(readline())); | ||
|
||
int d1 = parse_int(*(first_multiple_input + 0)); | ||
|
||
int m1 = parse_int(*(first_multiple_input + 1)); | ||
|
||
int y1 = parse_int(*(first_multiple_input + 2)); | ||
|
||
char** second_multiple_input = split_string(rtrim(readline())); | ||
|
||
int d2 = parse_int(*(second_multiple_input + 0)); | ||
|
||
int m2 = parse_int(*(second_multiple_input + 1)); | ||
|
||
int y2 = parse_int(*(second_multiple_input + 2)); | ||
|
||
int result = libraryFine(d1, m1, y1, d2, m2, y2); | ||
|
||
fprintf(fptr, "%d\n", result); | ||
|
||
fclose(fptr); | ||
|
||
return 0; | ||
} | ||
|
||
char* readline() { | ||
size_t alloc_length = 1024; | ||
size_t data_length = 0; | ||
|
||
char* data = malloc(alloc_length); | ||
|
||
while (true) { | ||
char* cursor = data + data_length; | ||
char* line = fgets(cursor, alloc_length - data_length, stdin); | ||
|
||
if (!line) { | ||
break; | ||
} | ||
|
||
data_length += strlen(cursor); | ||
|
||
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { | ||
break; | ||
} | ||
|
||
alloc_length <<= 1; | ||
|
||
data = realloc(data, alloc_length); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if (data[data_length - 1] == '\n') { | ||
data[data_length - 1] = '\0'; | ||
|
||
data = realloc(data, data_length); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
} | ||
} else { | ||
data = realloc(data, data_length + 1); | ||
|
||
if (!data) { | ||
data = '\0'; | ||
} else { | ||
data[data_length] = '\0'; | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
|
||
char* ltrim(char* str) { | ||
if (!str) { | ||
return '\0'; | ||
} | ||
|
||
if (!*str) { | ||
return str; | ||
} | ||
|
||
while (*str != '\0' && isspace(*str)) { | ||
str++; | ||
} | ||
|
||
return str; | ||
} | ||
|
||
char* rtrim(char* str) { | ||
if (!str) { | ||
return '\0'; | ||
} | ||
|
||
if (!*str) { | ||
return str; | ||
} | ||
|
||
char* end = str + strlen(str) - 1; | ||
|
||
while (end >= str && isspace(*end)) { | ||
end--; | ||
} | ||
|
||
*(end + 1) = '\0'; | ||
|
||
return str; | ||
} | ||
|
||
char** split_string(char* str) { | ||
char** splits = NULL; | ||
char* token = strtok(str, " "); | ||
|
||
int spaces = 0; | ||
|
||
while (token) { | ||
splits = realloc(splits, sizeof(char*) * ++spaces); | ||
|
||
if (!splits) { | ||
return splits; | ||
} | ||
|
||
splits[spaces - 1] = token; | ||
|
||
token = strtok(NULL, " "); | ||
} | ||
|
||
return splits; | ||
} | ||
|
||
int parse_int(char* str) { | ||
char* endptr; | ||
int value = strtol(str, &endptr, 10); | ||
|
||
if (endptr == str || *endptr != '\0') { | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
return value; | ||
} |