gpt4 book ai didi

C++ 年龄计算器 - 查找两个日期之间时间的最有效方程式

转载 作者:搜寻专家 更新时间:2023-10-31 02:19:02 29 4
gpt4 key购买 nike

我正在尝试用 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);
}

最佳答案

好吧,看起来您的代码在以下情况下会失败: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;
}

修改后的calculateAge函数:

//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;
}

关于C++ 年龄计算器 - 查找两个日期之间时间的最有效方程式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978882/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com