Created
November 12, 2014 16:06
-
-
Save nikAizuddin/df45d348da4c4757184c to your computer and use it in GitHub Desktop.
Programming Technique 2014/2015 Semester 1 Labsheet 4
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
| /*-------------------------------------------------------------------- | |
| | TITLE: Calculate age | |
| +-------------------------------------------------------------------- | |
| | AUTHOR: Nik Mohamad Aizuddin b. Nik Azmi | |
| | EMAIL: nickaizuddin93@gmail.com | |
| | DATE CREATED: 12/NOV/2014 | |
| | PURPOSE: My answer for the subject Programming Technique | |
| | 2014/2015 Semester 1 Labsheet 4. | |
| +-------------------------------------------------------------------- | |
| | LANGUAGE: C | |
| | STANDARD: C89-Strict | |
| +-------------------------------------------------------------------- | |
| | VERSION HISTORY: | |
| | | |
| | Version | Date | Description | |
| | ---------+-------------+------------------------------------------ | |
| | 1.0.0 | 12/NOV/2014 | First released. | |
| | | |
| +-------------------------------------------------------------------- | |
| | PROGRAM'S LIMITATIONS: | |
| | --> The program doesn't to check for date validity. Assume you | |
| | always insert valid date. | |
| | --> Assume that end date is more latest compared to start date. | |
| +------------------------------------------------------------------*/ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> /** for strcpy() use */ | |
| /************************ get_birth_date() *************************** | |
| * Get user birth date. | |
| * | |
| * @param1: pBirth_day = address to birth_day variable | |
| * @param2: pBirth_month = address to birth_month variable | |
| * @param3: pBirth_year = address to birth_year variable | |
| * | |
| * @return: nothing | |
| */ | |
| void get_birth_date(int *pBirth_day, | |
| int *pBirth_month, | |
| int *pBirth_year) | |
| { | |
| int birth_day = 0; | |
| int birth_month = 0; | |
| int birth_year = 0; | |
| /** Tell user to enter their birth date and get the input */ | |
| printf("Insert birth year [for example: 1993]: "); | |
| scanf(" %d", &birth_year); | |
| printf("Insert birth month [for example: 10]: "); | |
| scanf(" %d", &birth_month); | |
| printf("Insert birth day [for example: 29]: "); | |
| scanf(" %d", &birth_day); | |
| /** | |
| * Pass the birth_day value to the variable | |
| * at address pBirth_day | |
| */ | |
| *pBirth_day = birth_day; | |
| /** | |
| * Pass the birth_month value to the variable | |
| * at address pBirth_month | |
| */ | |
| *pBirth_month = birth_month; | |
| /** | |
| * Pass the birth_year value to the variable | |
| * at address pBirth_year | |
| */ | |
| *pBirth_year = birth_year; | |
| } | |
| /*********************** get_current_date() ************************** | |
| * Get current date | |
| * | |
| * @param1: pCurrent_day = address to current_day variable | |
| * @param2: pCurrent_month = address to current_month variable | |
| * @param3: pCurrent_year = address to current_year variable | |
| * | |
| * @return: | |
| */ | |
| void get_current_date(int *pCurrent_day, | |
| int *pCurrent_month, | |
| int *pCurrent_year) | |
| { | |
| int current_day = 0; | |
| int current_month = 0; | |
| int current_year = 0; | |
| /** Tell user to enter current date and get the input */ | |
| printf("Insert current year [for example: 2014]: "); | |
| scanf(" %d", ¤t_year); | |
| printf("Insert current month [for example: 11]: "); | |
| scanf(" %d", ¤t_month); | |
| printf("Insert current day [for example: 12]: "); | |
| scanf(" %d", ¤t_day); | |
| /** | |
| * Pass the current_day value to the variable | |
| * at address pCurrent_day | |
| */ | |
| *pCurrent_day = current_day; | |
| /** | |
| * Pass the current_month value to the variable | |
| * at address pCurrent_month | |
| */ | |
| *pCurrent_month = current_month; | |
| /** | |
| * Pass the current_year value to the variable | |
| * at address pCurrent_year | |
| */ | |
| *pCurrent_year = current_year; | |
| } | |
| /************************ calculate_age() *************************** | |
| * Calculate age at specified date | |
| * | |
| * @param1: birth_day = day of birth | |
| * @param2: birth_month = month of birth | |
| * @param3: birth_year = year of birth | |
| * @param4: current_day = current day | |
| * @param5: current_month = current month | |
| * @param6: current_year = current year | |
| * @param7: pAge_day = address to variable age_day | |
| * @param8: pAge_month = address to variable age_month | |
| * @param9: pAge_year = address to variable age_year | |
| * | |
| * @return: nothing | |
| */ | |
| void calculate_age(int birth_day, | |
| int birth_month, | |
| int birth_year, | |
| int current_day, | |
| int current_month, | |
| int current_year, | |
| int *pAge_day, | |
| int *pAge_month, | |
| int *pAge_year) | |
| { | |
| int age_day = 0; | |
| int age_month = 0; | |
| int age_year = 0; | |
| /** Find the difference in year */ | |
| age_year = current_year - birth_year; | |
| /** | |
| * If current_month < birth_month, | |
| * decrement age_year and add 12 to current_month | |
| */ | |
| if(current_month < birth_month) { | |
| age_year = age_year - 1; | |
| current_month = current_month + 12; | |
| } | |
| /** Find the differences in month */ | |
| age_month = current_month - birth_month; | |
| /** | |
| * If current_day < birth_day, | |
| * decrement age_month and add 31 to current_day | |
| */ | |
| if(current_day < birth_day) { | |
| age_month = age_month - 1; | |
| current_day = current_day + 31; | |
| } | |
| /** Find differences in day */ | |
| age_day = current_day - birth_day; | |
| /** | |
| * Pass value age_day to variable | |
| * address at pAge_day | |
| */ | |
| *pAge_day = age_day; | |
| /** | |
| * Pass value age_month to variable | |
| * address at pAge_month | |
| */ | |
| *pAge_month = age_month; | |
| /** | |
| * Pass value age_year to variable | |
| * address at pAge_year | |
| */ | |
| *pAge_year = age_year; | |
| } | |
| /*************************** print_age() ***************************** | |
| * Print age at 31 December and at specifed date | |
| * | |
| * @param1: current_day | |
| * @param2: current_month | |
| * @param3: current_year | |
| * @param4: age_31dec2009_day | |
| * @param5: age_31dec2009_month | |
| * @param6: age_31dec2009_year | |
| * @param7: age_current_day | |
| * @param8: age_current_month | |
| * @param9: age_current_year | |
| * | |
| * @return: nothing | |
| */ | |
| void print_age(int current_day, | |
| int current_month, | |
| int current_year, | |
| int age_31dec2009_day, | |
| int age_31dec2009_month, | |
| int age_31dec2009_year, | |
| int age_current_day, | |
| int age_current_month, | |
| int age_current_year) | |
| { | |
| char str_current_month[16]; | |
| /** Print age at 31 December 2009 */ | |
| printf( | |
| "Your age as 31 December 2009 is %d years %d months %d days\n", | |
| age_31dec2009_year, age_31dec2009_month, age_31dec2009_day); | |
| /** Print age at current date */ | |
| switch(current_month) { | |
| case 1: | |
| strcpy(str_current_month, "January"); | |
| break; | |
| case 2: | |
| strcpy(str_current_month, "February"); | |
| break; | |
| case 3: | |
| strcpy(str_current_month, "March"); | |
| break; | |
| case 4: | |
| strcpy(str_current_month, "April"); | |
| break; | |
| case 5: | |
| strcpy(str_current_month, "May"); | |
| break; | |
| case 6: | |
| strcpy(str_current_month, "Jun"); | |
| break; | |
| case 7: | |
| strcpy(str_current_month, "July"); | |
| break; | |
| case 8: | |
| strcpy(str_current_month, "August"); | |
| break; | |
| case 9: | |
| strcpy(str_current_month, "September"); | |
| break; | |
| case 10: | |
| strcpy(str_current_month, "October"); | |
| break; | |
| case 11: | |
| strcpy(str_current_month, "November"); | |
| break; | |
| case 12: | |
| strcpy(str_current_month, "December"); | |
| break; | |
| default: | |
| break; | |
| } | |
| printf( | |
| "Your age as %d %s %d is %d years %d months %d days\n", | |
| current_day, str_current_month, current_year, | |
| age_current_year, age_current_month, age_current_day); | |
| } | |
| int main(void) | |
| { | |
| int birth_day = 0; | |
| int birth_month = 0; | |
| int birth_year = 0; | |
| int current_day = 0; | |
| int current_month = 0; | |
| int current_year = 0; | |
| int age_31dec2009_day = 0; | |
| int age_31dec2009_month = 0; | |
| int age_31dec2009_year = 0; | |
| int age_current_day = 0; | |
| int age_current_month = 0; | |
| int age_current_year = 0; | |
| /** Get birth date and current date */ | |
| get_birth_date(&birth_day, &birth_month, &birth_year); | |
| get_current_date(¤t_day, ¤t_month, ¤t_year); | |
| /** Calculate age at 31 December 2009 */ | |
| calculate_age(birth_day, | |
| birth_month, | |
| birth_year, | |
| 31, | |
| 12, | |
| 2009, | |
| &age_31dec2009_day, | |
| &age_31dec2009_month, | |
| &age_31dec2009_year); | |
| /** Calculate age at specified date (current_date) */ | |
| calculate_age(birth_day, | |
| birth_month, | |
| birth_year, | |
| current_day, | |
| current_month, | |
| current_year, | |
| &age_current_day, | |
| &age_current_month, | |
| &age_current_year); | |
| /** Display age at 31 December 2009 and at specified date */ | |
| print_age(current_day, | |
| current_month, | |
| current_year, | |
| age_31dec2009_day, | |
| age_31dec2009_month, | |
| age_31dec2009_year, | |
| age_current_day, | |
| age_current_month, | |
| age_current_year); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment