From 14130dfa1d338aae6fd8bf08deded6fe07f83d2d Mon Sep 17 00:00:00 2001 From: SharafatKarim Date: Fri, 11 Aug 2023 15:10:01 +0600 Subject: [PATCH] 10 | 1-5 | sharafat --- solutions/sharafat/11/3.c | 51 +++++++++++++++++++++ solutions/sharafat/11/4.c | 48 ++++++++++++++++++++ solutions/sharafat/11/5.c | 94 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 solutions/sharafat/11/3.c create mode 100644 solutions/sharafat/11/4.c create mode 100644 solutions/sharafat/11/5.c diff --git a/solutions/sharafat/11/3.c b/solutions/sharafat/11/3.c new file mode 100644 index 0000000..11bdd93 --- /dev/null +++ b/solutions/sharafat/11/3.c @@ -0,0 +1,51 @@ +#include + +struct time_struct +{ + int hour; + int minute; + int second; +}; + +void input_time(struct time_struct *time) +{ + printf("Enter time in HH:MM:SS format: "); + scanf("%d:%d:%d", &time->hour, &time->minute, &time->second); +} + +void print_time(struct time_struct time) +{ + printf("Equivalent time: "); + printf("%d:%d:%d\n", time.hour, time.minute, time.second); +} + +struct time_struct increment_second_in_time(struct time_struct time) +{ + time.second++; + if (time.second == 60) + { + time.second = 0; + time.minute++; + if (time.minute == 60) + { + time.minute = 0; + time.hour++; + if (time.hour == 24) + { + time.hour = 0; + } + } + } + return time; +} + +int main() +{ + struct time_struct time; + input_time(&time); + print_time(time); + printf("Time after incrementing second: "); + time = increment_second_in_time(time); + print_time(time); + return 0; +} \ No newline at end of file diff --git a/solutions/sharafat/11/4.c b/solutions/sharafat/11/4.c new file mode 100644 index 0000000..5dd6e67 --- /dev/null +++ b/solutions/sharafat/11/4.c @@ -0,0 +1,48 @@ +#include +#include + +struct date +{ + int day; + int month; + int year; +}; + +void input_date(struct date *date) +{ + printf("Enter date in DD/MM/YYYY format: "); + scanf("%d/%d/%d", &date->day, &date->month, &date->year); +} + +void validate_data(struct date *date) +{ + if (date->year < 1) + { + printf("Invalid year\n"); + exit(1); + } + if (date->month < 1 || date->month > 12) + { + printf("Invalid month\n"); + exit(1); + } + if ((date->day < 1 || date->day > 31) || (date-> day > 30 && (date->month == 4 || date->month == 6 || date->month == 9 || date->month == 11)) || (date->day > 28 && date->month == 2) || (date->day > 29 && date->month == 2 && (date->year % 4 != 0 || (date->year % 100 == 0 && date->year % 400 != 0)))) + { + printf("Invalid day\n"); + exit(1); + } +} + +void print_date(struct date *date) +{ + printf("Date: %d/%d/%d\n", date->day, date->month, date->year); +} + +int main() +{ + struct date date; + input_date(&date); + validate_data(&date); + print_date(&date); + return 0; +} \ No newline at end of file diff --git a/solutions/sharafat/11/5.c b/solutions/sharafat/11/5.c new file mode 100644 index 0000000..845e19d --- /dev/null +++ b/solutions/sharafat/11/5.c @@ -0,0 +1,94 @@ +#include +#include + +struct date +{ + int day; + int month; + int year; +}; + +void input_date(struct date *date) +{ + printf("Enter date in DD/MM/YYYY format: "); + scanf("%d/%d/%d", &date->day, &date->month, &date->year); +} + +void validate_data(struct date *date) +{ + if (date->year < 1) + { + printf("Invalid year\n"); + exit(1); + } + if (date->month < 1 || date->month > 12) + { + printf("Invalid month\n"); + exit(1); + } + if ((date->day < 1 || date->day > 31) || (date-> day > 30 && (date->month == 4 || date->month == 6 || date->month == 9 || date->month == 11)) || (date->day > 28 && date->month == 2) || (date->day > 29 && date->month == 2 && (date->year % 4 != 0 || (date->year % 100 == 0 && date->year % 400 != 0)))) + { + printf("Invalid day\n"); + exit(1); + } +} + +int validate_data_and_return(struct date date) +{ + if (date.year < 1) + { + printf("Invalid year\n"); + return(1); + } + if (date.month < 1 || date.month > 12) + { + printf("Invalid month\n"); + return(1); + } + if ((date.day < 1 || date.day > 31) || (date. day > 30 && (date.month == 4 || date.month == 6 || date.month == 9 || date.month == 11)) || (date.day > 28 && date.month == 2) || (date.day > 29 && date.month == 2 && (date.year % 4 != 0 || (date.year % 100 == 0 && date.year % 400 != 0)))) + { + printf("Invalid day\n"); + return(1); + } + return(0); +} + +void update(struct date *date) +{ + struct date temp_date = *date; + temp_date.day++; + if (validate_data_and_return(temp_date)) + { + temp_date.day = 1; + temp_date.month++; + if (validate_data_and_return(temp_date)) + { + temp_date.month = 1; + temp_date.year++; + if (validate_data_and_return(temp_date)) + { + printf("Invalid date\n"); + exit(1); + } + } + } + *date = temp_date; +} + +void print_date(struct date *date) +{ + printf("Date: %d/%d/%d\n", date->day, date->month, date->year); +} + +int main() +{ + struct date date; + input_date(&date); + validate_data(&date); + print_date(&date); + + printf("Date after incrementing day: \n"); + update(&date); + print_date(&date); + return 0; +} \ No newline at end of file