- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试用 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()
的函数,它给出了相应的日期常量值。
因此,减去两个日期很容易得到结果。看下面的代码就清楚了。
请随时询问有关它的任何问题。
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;
}
关于C++ 年龄计算器 - 查找两个日期之间时间的最有效方程式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978882/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!