c++获取用户选择的日期与实际日期之间的年份(计算日、月、年)
c++ get years between date chosen by user and actual date(counting days,months,years)
我试过了:
struct Den_t
{
int day, month, year;
};
int main()
{
struct Den_t* Datum = new struct Den_t;
struct Den_t* Dnes = new struct Den_t;
time_t theTime = time(NULL);
struct tm aTime;
localtime_s(&aTime, &theTime);
Dnes->day = aTime.tm_mday;
Dnes->month = aTime.tm_mon + 1;
Dnes->year = aTime.tm_yday + 1900;
cin >> Datum->day >> Datum->month >> Datum->year;
if (Dnes->year - Datum->year >= 18 )
cout << "full aged " << endl;
else
cout << "not full aged " << endl;
system("PAUSE");
return 0;
}
但是我不明白我应该怎么比较和减少,有人能解释一下吗
我还需要做什么来告诉人们的日期,例如float by比较年、月、日的实际时间和用户输入的日期这个项目吗?
这里您的代码逻辑有问题。例如:
Datum is 31/12/1982
Dnes is 01/01/2000
年龄相差18岁,年龄相差17天。
考虑使用标准库函数,而不是重新发明轮子。Difftime可能很有用,例如
这是一个非常肮脏的例子,但它会完成工作:
time_t dnes;
time(&dnes);
// Set datum here ...
cin >> Datum->day >> Datum->month >> Datum->year;
datum.tm_mday = Datum->day;
datum.tm_mon = Datum->month - 1;
datum.tm_yday = Datum->year - 1900;
datum->tm_yday+=18;
if (difftime(dnes, mktime(&datum)) <0 )
cout << "not full aged " << endl;
else
cout << "full aged " << endl;
使用这些库:http://howardhinnant.github.io/date/date.html
http://howardhinnant.github.io/date/tz.html这就是我将如何处理这个问题。首先是代码,然后是解释:
#include "tz.h"
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
std::cout << "Enter birthday [day month year]:";
int di, mi, yi;
std::cin >> di >> mi >> yi;
if (std::cin.fail())
{
std::cout << "Invalid daten";
return 1;
}
auto y = year{yi};
if (!y.ok())
{
std::cout << "Invalid yearn";
return 1;
}
auto m = month(mi);
if (!m.ok())
{
std::cout << "Invalid monthn";
return 1;
}
auto d = day(di);
if (!d.ok())
{
std::cout << "Invalid dayn";
return 1;
}
auto birthday = y/m/d;
if (!birthday.ok())
{
std::cout << "Invalid birthdayn";
return 1;
}
auto local_time = current_zone()->to_local(system_clock::now());
auto today = year_month_day{floor<days>(local_time)};
auto age = today.year() - birthday.year();
if (birthday + age > today)
--age;
if (age >= years{18})
std::cout << "full aged at " << age.count() << "n";
else
std::cout << "not full aged at " << age.count() << "n";
}
我会先费点劲来检查用户输入的有效性。下面的内容看起来像是最小值:
- 必须为整型输入。
- 每个积分输入必须有一个合理的值(例如month必须在[1,12]范围内)。
- y/m/d必须是有效的日期。
一个更健壮的程序可能会给用户输入的内容一些反馈,并给他另一个改正错误的机会。
一旦确定我们有一个有效的生日,我们需要在当地时区获得当前日期。:
auto local_time = current_zone()->to_local(system_clock::now());
获取本地时间。
该本地时间可通过以下命令转换为本地年、月、日:
auto today = year_month_day{floor<days>(local_time)};
这个计算遵循你的生日从当地午夜开始的习俗,不管你实际上是在一天中的什么时间(以及在地球上的什么地方)出生的。也就是说,一旦确定了当地的年/月/日,这个问题就与当地的时区无关,甚至与当地的时间无关。
接下来,计算当前年龄:
auto age = today.year() - birthday.year();
if (birthday + age > today)
--age;
今天的年龄和生日的年龄之差是年龄的近似。通过计算今年您的生日是的日期,可以改进这个近似。如果今年的生日还在未来,那么按照习俗,我们将其计算为年轻一岁。如果我们做的事情不太倾向于法律体系,而更倾向于科学工作,我们可以用其他方法进行计算,比如四舍五入到最近的年份(在这个库中也很容易做到)。
如果生日是2月29日,上述代码仍然有效:birthday + age
将(75%的可能性)导致无效日期,例如:Feb/29/2015。然而,这个无效的日期将正确地比较大于feb/28/2015和小于mar/1/2015,正如我们所需要的那样!无效日期是你的朋友,而不是你的敌人——你只需要知道它们的存在,并知道如何处理它们。
现在报告你的发现很简单:
if (age >= years{18})
std::cout << "full aged at " << age.count() << "n";
else
std::cout << "not full aged at " << age.count() << "n";
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- 计算当前日期和用户输入日期问题之间的天数
- 使用向量计算到期日期
- C++日期比较并不总是计算出相同的逻辑值
- 在SQLite中存储日期和计算年龄
- 像儒略数字用于计算日期一样,是否有任何特定的数字来计算时间
- 计算日期数的日期
- 如何使用C Win32 API计算两个日期之间的天数
- C++计算日期之间的差异
- 在unix/win32上高效计算用于日志记录的日期/时间戳
- C++ 根据tm_yday和tm_year值计算日期
- 根据给定的年份和周数计算开始和结束日期/时间
- 从日期开始计算一年中的某一天
- c++获取用户选择的日期与实际日期之间的年份(计算日、月、年)
- 设计一个结构来存储时间和日期.写一个函数来计算两个时间之间的差(以分钟为单位)
- 用c++计算两个日期的差异
- 计算一年中的日期
- 在C或c++中计算日历日期(在给定日期上加N天)
- 计算与通配符匹配的下一个最近日期时间