C++ Age Calculator-наиболее эффективные уравнения для нахождения времени между 2 датами



Я пытаюсь создать калькулятор возраста в C++ , который принимает данные пользователя за текущий месяц, день и год, а также День рождения пользователя (MM DD YYYY). Остальные функции работают нормально, но у меня возникли проблемы с моей функцией calculateAge ().



Может ли кто-нибудь увидеть проблемы с моими уравнениями в моей функции calculateAge() или способ, которым я мог бы сделать это более эффективным?

Редактировать: полный код



    #include <iostream>

using namespace std;

main () {

//declare local variables
bool valid, validBirthday;
int cmonth, cday, cyear;
int bmonth, bday, byear;
int ageYears, ageMonths, ageDays;
char answer;
const int MAXGUESSES = 3;

//function declarations
bool enterDate (int&, int&, int&, const int);
bool enterBirthDate (int, int, int, int&, int&, int&, const int);
void calculateAge (int, int, int, int, int, int, int&, int&, int&);

//prompt the user for todays date and invoke the enterDate() function
cout << "Please enter today's date, ";
valid = enterDate(cmonth, cday, cyear, MAXGUESSES);

//if validDate() function doesn't recognize a valid date after three tries
//the user is alerted
if (valid == false)
cout << endl;
else

//if the date was valid show the user the date they entered and ask
//if they would like to calculate their age.
if (valid == true) {
cout << "Date entered is: " << cmonth << '/' << cday << '/' << cyear << endl;

cout << "Would you like to see how old you are? (y/n) ";
cin >> answer;

if (answer == 'N' || answer == 'n')
cout << "OK" << endl;
else

//if the user would like to see their age, call the enterBirthDate
//function and assign the return value to the boolean variable
//validBirthDate
if (answer == 'Y' || answer == 'y') {
if (validBirthday = enterBirthDate (cmonth, cday, cyear, bmonth, bday, byear, MAXGUESSES) == true) {

cout << "Date entered is: " << bmonth << '/' << bday << '/' << byear << endl;

//call the calculateAge() function to calculate the user's age
calculateAge(bmonth, bday, byear, cmonth, cday, cyear, ageYears, ageMonths, ageDays);

//display results
cout << "You are " << ageYears << " years, " << ageMonths << " months, and " << ageDays << " days old." << endl;

//display a special result if it is the users birthday
if (bmonth == cmonth && bday == cday)
cout << "Happy Birthday!" << endl;
}

else
cout << "You have entered an invalid birth day." << endl;

} //end if answer is yes

else //if answe is not yes or no
cout << "You did not enter 'y' or 'n'" << endl;


} //end if current date is valid
}


Функции



#include <iostream>

using namespace std;

//function definition for enterDate() - the function that prompts
//the user for input and parses input into months, days, and years
//and then calls the validDate() function to make sure the entered
//date is valid.

bool enterDate (int&month, int&day, int&year, const int MAXGUESSES) {

bool valid;
int counter = 1;

bool validDate(int&, int&, int&);

cout << "Please enter the date as MM DD YYYY: ";
cin >> month >> day >> year;

valid = validDate(month, day, year);

// use the constant MAXGUESSES and the variable counter to
// allow the user only three tries to enter a valid date

if (valid == false) {
while (valid == false && counter < MAXGUESSES) {
counter++;
cout << "The entered date is invalid, please re-enter: ";
cin >> month >> day >> year;
valid = validDate(month, day, year);

}

// if the user reaches the limit of three guesses, alert them
// and bring an end to the program

cout << "You have reached the limit for invalid dates." << endl;
}

return(valid);

}

// function definition for enterBirthDate() which prompts the user to enter
// their birthdate and invokes the enterDate() function to parse the input and
// the dateBefore() function to make sure the entered birth date occurs before
// the current date entered by the user

bool enterBirthDate (int cmonth, int cday, int cyear, int&bmonth, int&bday, int&byear, const int MAXGUESSES) {

bool valid;
bool valid2;

bool dateBefore (int, int, int, int, int, int);
bool enterDate (int&, int&, int&, const int);

cout << "We need to know your date of birth, ";

// call function enterDate() to prompt the user for input and parse
// the input into b(irthday)month, bday, and byear

valid = enterDate(bmonth, bday, byear, MAXGUESSES);

//if the birth date entered by the user is a valid date, make sure
//that the birth date occurs before the current date supplied by
//the user in enterDate()

if (valid == true)
valid2 = dateBefore(cmonth, cday, cyear, bmonth, bday, byear);

return(valid2);

}

//function definition for dateBefore() (invoked by enterBirthDate() which
//compares variables c(urrent)month cday, and cyear to b(irth)month, bday,
//and byear to make sure the birthday occurs before the current date supplied by the user

bool dateBefore (int cmonth, int cday, int cyear, int bmonth, int bday, int byear) {

bool valid;


//if the birth year is greater than the current year assign the boolean
//variable 0 (false) to valid, indicating an invalid birth date

if (byear > cyear)
valid = false;
else

//if birth year is equal to the current year, perform relational operations
//on the bmonth and cmonth variables to determine which date occured first

if (byear == cyear) {

//if the birth year and current year are the same, but the birth
//month is less than the current month, assign the value of true
//to the boolean variable valid

if (bmonth < cmonth)
valid = true;
else

//if the bmonth = cmonth perform relational operations on bday and cday
if (bmonth == cmonth)

//if the bday is less than the cday assign true to the boolean variable valid
if (bday < cday)
valid = true;
else
//if the bday is greater than the cday assign the value false to valid
valid = false;
else

//if bmonth is greater than the cmonth and byear is equal to the cyear
//assign false to valid, indicating an invalid birth day

if (bmonth > cmonth)
valid = false;
else
//if the cyear is greater than the byear assign the value of false to valid
if (byear < cyear)
valid = false;
}

return(valid);
}


//function definition for validDate() (invoked by enterDate() ) which uses variables of day, month, and year
//to make sure that the date entered is valid based on month, day, and year variables
bool validDate (int&month, int&day, int&year) {

//local variables
bool valid;
int maxDay;

//function declaration
int getLastDay (int);

//calculate maximum day based on month
maxDay = getLastDay(month);

//if the month is less than zero or greater than 12 assign the value false to
//the boolean variable valid indicating an invalid date
if (month < 0 || month > 12)
valid = false;

else

//if the month is valid make sure that the day falls inbetween 1 and the maximum day
//associated with the month entered

if (month > 0 && month <= 12) {
if (day > 0 && day <= maxDay)
valid = true;
else
valid = false;
}

return (valid);
}

//function definition for calculareAge(). Invoked by main(), calculate age accept parameters bmonth, bday, byear,
//cmonth, cday, and cyear (by value) to perform calculations and relational operations to determine age. The parameters
//ageYears, ageMonths, and ageDays are passed by reference so they can be output in the main() program.

void calculateAge (int bmonth, int bday, int byear, int cmonth, int cday, int cyear, int&ageYears, int&ageMonths, int&ageDays) {

//local variable
int maxDay;

//function declaration
int getLastDay (int);

maxDay = getLastDay (bmonth);

if (cyear >= byear && cmonth >= bmonth && cday >= bday){
ageYears = cyear - byear;
ageMonths = cmonth - bmonth;
ageDays = cday - bday;

}
else

if (cyear > byear) {
if (cmonth >= bmonth) {
if (cday < bday) {
ageYears = cyear - byear;
ageMonths = cmonth - bmonth - 1;
ageDays = maxDay - bday + cday;
}
}
else
if (cmonth < bmonth) {
if (cday > bday) {
ageYears = cyear - byear - 1;
ageMonths = 12 - bmonth + cmonth;
ageDays = cday - bday;
}
else
if (bday > cday) {
ageYears = cyear - byear - 1;
ageMonths = 12 - bmonth + cmonth - 1;
ageDays = maxDay - bday + cday;
}
}
}

}

//function definition for getLastDay(), uses the month entered to calculate the maximum
//valid day for that month

int getLastDay (int month) {

int maxDay;

switch (month) {
case 4:
case 6:
case 9:
case 11:
maxDay = 30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
maxDay = 31;
break;
case 2:
maxDay = 28;
}

return(maxDay);
}
632   1  
c++

1 ответ:

Ну, похоже, ваш код просто терпит неудачу в таких случаях, как: 1 9 2001 y 1 10 2000.

Как насчет того, чтобы дать постоянное значение для каждого дня календаря?

Я модифицировал вашу функцию calculateAge() и создал другую функцию с именем getInt(), которая дает соответствующее постоянное значение даты. Таким образом, вычитание двух дат легко дает нам результат. Смотрите код ниже, чтобы быть ясным.

Не стесняйтесь спрашивать об этом.

Новая функция getInt:

int getInt(int y, int m, int d)
{
    int ret = y*365;

    //function declaration
    int getLastDay (int);

    for (int i = 1; i<m; i++)
        ret+=getLastDay(i);
    ret+=d;
    return ret;
}

Модифицированный расчет функция:

//function definition for calculareAge(). Invoked by main(), calculate age accept parameters bmonth, bday, byear,
//cmonth, cday, and cyear (by value) to perform calculations and relational operations to determine age. The parameters
//ageYears, ageMonths, and ageDays are passed by reference so they can be output in the main() program.

void calculateAge (int bmonth, int bday, int byear, int cmonth, int cday, int cyear, int&ageYears, int&ageMonths, int&ageDays) {

    //local variable
    int maxDay;

    //function declaration
    int getLastDay (int);

    int now = getInt(cyear,cmonth,cday);
    int birthday = getInt(byear,bmonth,bday);
    if(now<birthday) return;
    int diff = now-birthday;
    ageYears = (diff)/365;

    diff-=(365*ageYears);

    ageMonths = 0;
    int rest;
    while(1)
    {
        rest = getLastDay(ageMonths+1);
        if(diff>=rest)
        {
            diff-=rest;
            ageMonths++;
        }
        else break;
    }
    ageDays = diff;
}

Comments

    Ничего не найдено.