gpt4 book ai didi

c++ 获取用户选择的日期和实际日期之间的年数(计算天数、月数、年数)

转载 作者:行者123 更新时间:2023-11-30 05:37:20 25 4
gpt4 key购买 nike

我试过这样做:

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

但我不知何故无法理解我什至应该比较和递减什么,有人能解释一下吗

what else i need to do to tell people's date for example in float by comparing year,month and day of actual time and date user inputs in the program?

最佳答案

您的代码逻辑有问题。例如:

Datum is 31/12/1982
Dnes is 01/01/2000

年差是 18,但年龄是 17 和 2 天。

考虑使用标准库函数而不是重新发明轮子。 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;

关于c++ 获取用户选择的日期和实际日期之间的年数(计算天数、月数、年数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33282974/

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