1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
//caclulate your age in years plus its fractional part
#include <iostream>
using namespace std;
float DaysOfYearLeft (int, int, int, int);
int GetYears (int, int, int, int);
int main()
{
float age;
int bday; // birthday
int bmonth;
int byear;
int cday; // current day
int cmonth; // current month
int cyear; // current year
float fyear; // fractional year
float fday; // fractional day
float fage; // fractional age is the sum of fyear and fday
cout<<"This program calculutes your age plus the fractional part of your age.";
cout<<"\n\n Enter your numerical birth day, month, and complete year in that order each\n value seperated by a single space.\n";
cin>>bday; cin>>bmonth; cin>>byear;
cout<<"\n\n Enter the current numerical date day, month, and complete year in that order \neach value seperated by a single space.\n";
cin>>cday; cin>>cmonth; cin>>cyear;
fyear = GetYears (byear, cyear, bmonth, cmonth);
fday = 1 -(DaysOfYearLeft (bday, bmonth, cday, cmonth)/365) ;
fage = fyear + fday;
cout<< "\n\nYour age with its fractional part is "<< fage;
cout<< "\n That is " << GetYears(byear, cyear, bmonth, cmonth) << " and "<< 365 - DaysOfYearLeft (bday, bmonth, cday, cmonth)<<" days.";
cout<< "\n" << DaysOfYearLeft (bday, bmonth, cday, cmonth) <<" days until your birthday!";
char f;
cin>>f;
return 0;
}
float DaysOfYearLeft (int bday, int bmonth, int cday, int cmonth){
float monthsleft;
if (bmonth > cmonth){
monthsleft = bmonth - cmonth; //months left until next ageplus1
}
else
if (bmonth < cmonth){
monthsleft = 12 - (cmonth - bmonth);
}
// function does not yet account for current month equal to birth month
float daysleft = (monthsleft * 30.5 + bday) - cday;
return daysleft;
}
int GetYears (int byear, int cyear, int bmonth, int cmonth){
int age;
if (bmonth > cmonth){
age= (cyear - byear) - 1;
}
else
if (bmonth < cmonth){
age = cyear - byear;
}
return age;
}
|