我正在尝试使用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);
}
答案 0 :(得分:2)
好吧,似乎您的代码在以下情况下失败:1 9 2001 y 1 10 2000
。
如何为日历的每一天提供一个恒定值?
我修改了你的calculateAge()
函数并创建了另一个名为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;
}
答案 1 :(得分:0)
这对我来说非常有用,我在纸上研究算法,从纸到Visual Studio。 很简单,首先从当前年份中减去出生年份,然后从当前月份中减去出生月份,然后检查出生日期是否小于当前日期,如果是,则从最后月份中减去1个月。值,但如果为false,则不减去任何值。
void AgeCalc(string Birthdate, float &saveAge)
{
time_t now = time(0);
tm *ltm = localtime(&now);
float bDay = 1, bMonth = 1, bYear = 1;
float cDay = ltm->tm_mday;
float cMonth = 1 + ltm->tm_mon;
float cYear = 1900 + ltm->tm_year;
float ageYear, ageMonth;
float ageMonthCalcFromMonths;
float ageMonthCalcFromDays;
float CharToNum;
float FinalAge = 0;
for (int i = 0; i < 11; i++)
{
switch (i)
{
case 0:
CharToNum = (int)(Birthdate[i] - 48);
CharToNum *= 10;
bDay += CharToNum;
break;
case 1:
CharToNum = (int)(Birthdate[i] - 48);
bDay += CharToNum;
bDay -= 1;
break;
case 3:
CharToNum = (int)(Birthdate[i] - 48);
CharToNum *= 10;
bMonth += CharToNum;
break;
case 4:
CharToNum = (int)(Birthdate[i] - 48);
bMonth += CharToNum;
bMonth -= 1;
break;
case 6:
CharToNum = (int)(Birthdate[i] - 48);
CharToNum *= 1000;
bYear += CharToNum;
break;
case 7:
CharToNum = (int)(Birthdate[i] - 48);
CharToNum *= 100;
bYear += CharToNum;
break;
case 8:
CharToNum = (int)(Birthdate[i] - 48);
CharToNum *= 10;
bYear += CharToNum;
break;
case 9:
CharToNum = (int)(Birthdate[i] - 48);
bYear += CharToNum;
bYear -= 1;
break;
}
}
ageYear = cYear - bYear;
ageMonthCalcFromMonths = cMonth - bMonth;
if (cDay > bDay)
ageMonthCalcFromDays = 0;
else
ageMonthCalcFromDays = -1;
if (ageMonthCalcFromMonths < 0 || (ageMonthCalcFromMonths > 0 && ageMonthCalcFromDays == -1))
{
ageYear--;
ageMonth = 12 + ageMonthCalcFromMonths + ageMonthCalcFromDays;
}
else
ageMonth = ageMonthCalcFromMonths + ageMonthCalcFromDays;
FinalAge += ageYear;
ageMonth /= 100;
FinalAge += ageMonth;
saveAge = FinalAge;
}