Today, a webpage is used to calculate the total number of days from birth to today. However, multiple attempts prompt incorrect input format, then I came up with the idea of writing this program in C language.
The basic idea of this program is to enter the birthday in the console and assign the data to the struct variable yearstart. Calculate the number of days from January 1, 1900. Call the system function to obtain the system time, assign it to another struct variable yearend, and calculate the number of days from January 1, 1900. Finally, subtract the latter from the former, and the data obtained is the number of days of the current day on the birthday.
Example of running result:
The following is my program code:
# Include "stdafx. H "<br/> # include <conio. h> <br/> # include <stdlib. h> <br/> # include <time. h> </P> <p> struct date <br/>{< br/> int year; <br/> int month; <br/> intday; <br/>} yearstart, yearend; <br/> int main () <br/>{< br/> int I; // variable I used for the cycle of the year <br/> int sum = 0, S = 0; // Save the number of days from the start year to January <br/> int sum1 = 0, s1 = 0; // number of days from the system year to January <br/> int month [13]; // This array is used to save the number of months of the input year and the current year <br/> int sumday [13]; // used to save the number of days from the beginning of the month to the beginning of the year <br/> int Mo Nthdayr [13] = {0, 31, 29,31, 30,31, 30,31, 31,30, 31,30, 31}; // defines the number of days per month in a leap year, save them as monthdayr [1] to monthdayr [12] <br/> int monthday [13] = {, 31, 31 }; // define the number of days of each month in the same year, respectively saved as monthdayr [1] to monthdayr [12] <br/> printf ("Enter your birthday:/N "); <br/> printf ("year:"); <br/> scanf ("% d", & yearstart. year); <br/> printf ("month:"); <br/> scanf ("% d", & yearstart. month); <br/> printf ("day:"); <br/> scanf ("% d", & yearstart. day); // enter the Year of birth Month, which is assigned to each member of the struct variable yearstart <br/> struct TM * P; <br/> time_t timep; <br/> time (& timep ); <br/> P = localtime (& timep); // obtain the system time <br/> yearend. year = 1900 + P-> tm_year; <br/> yearend. month = 1 + P-> tm_mon; <br/> yearend. day = p-> tm_mday; // assign the system time to the yearend members of the struct variable <br/> for (I = 1900; I <yearstart. year; I ++) <br/>{< br/> If (I % 4 = 0 & I % 100! = 0) | (I % 100 = 0 & I % 400 = 0) <br/> sum + = 366; <br/> else <br/> sum + = 365; <br/>} // The number of days from January 1, 1900 in the input year <br/> If (yearstart. year % 4 = 0 & yearstart. year % 100! = 0) | (yearstart. year % 100 = 0 & yearstart. year % 400 = 0) // determines whether the start year is a leap year <br/> {// a leap year <br/> for (I = 1; I <yearstart. month; I ++) <br/>{< br/> month [I] = I/I; // initialize the value in the array month <br/> sumday [I] = month [I] * monthdayr [I]; <br/> S + = sumday [I]; // calculate the number of days from the month to the beginning of the year recorded in the month <br/>}< br/> S + = yearstart. day; // Add the day to calculate the number of days from the beginning of the current year <br/> sum + = s; // count the total number of days of the input date Based on New Year's Day, January 1, 1900 <br/>}< br/> else // not a leap year <br/>{< br/> for (I = 1; I <yearstart. month; I ++) <br/>{< br/> month [I] = I/I; // initialize the values in the array month <br/> sumday [I] = month [I] * monthday [I]; <br/> S + = sumday [I]; // calculate the number of days from the month to the beginning of the month. <br/>}< br/> S + = yearstart. day; // Add the day to calculate the number of days from the beginning of the current year <br/> sum + = s; // count the total number of days from New Year's Day, January 1, 1900 <br/>}< br/> for (I = 1900; I <yearend. year; I ++) <br/>{< br/> If (I % 4 = 0 & I % 100! = 0) | (I % 100 = 0 & I % 400 = 0) <br/> sum1 + = 366; <br/> else <br/> sum1 + = 365; <br/>}// Number of days from January 1, 1900 in the statistical system year <br/> If (yearend. year % 4 = 0 & yearend. year & 100! = 0) | (yearend. year % 100 = 0 & yearend. year % 400 = 0) // determines whether the system year is a leap year <br/> {// a leap year <br/> for (I = 1; I <yearend. month; I ++) <br/>{< br/> month [I] = I/I; // initialize the value in the array month <br/> sumday [I] = month [I] * monthdayr [I]; <br/> S1 + = sumday [I]; // calculate the number of days from the month to the beginning of the year recorded in the month <br/>}< br/> S1 + = yearend. day; // Add the day to calculate the number of days from the beginning of the current year <br/> sum1 + = S1; // count the total number of days from the New Year's day of January 1, 1900 <br/>}< br/> else // not the runner-up <br/>{< br/> for (I = 1; I <yearend. month; I ++) <br/>{< br/> month [I] = I/I; // initialize the value in the array month <br/> sumday [I] = month [I] * monthday [I]; <br/> S1 + = sumday [I]; // calculate the number of days from the month to the beginning of the year recorded in the month <br/>}< br/> S1 + = yearend. day; // Add the day to calculate the number of days from the beginning of the current year <br/> sum1 + = S1; // count the total number of days from the New Year's Day in 1900 <br/>}< br/> sum = sum1-sum; // subtract the total number of days from the system date on January 1, 1900 from the total number of days from the input date on January 1, 1900 <br/> printf ("You have lived % d days/N", sum ); // output "how many days have you lived" <br/> system ("pause"); <br/>}< br/>
Sunday, January 1, March 20, 2011