gpt4 book ai didi

检查日期是否在c(数组)中的两个日期之间

转载 作者:行者123 更新时间:2023-11-30 21:34:19 24 4
gpt4 key购买 nike

我有三个 int 数组

int a[3] = {2,25,2015} //Date : d,mm,yyyy
int b[3] = {4,15,2016}
int c[3] = {7,10,2017}

我想检查b是否在a和c之间

最佳答案

下面的小程序正是您想要的。注意:您可以轻松使用smaller函数创建所有 bool 运算符 ( <=, >=, ==, !=, > )

#include <stdio.h>

typedef int Date[3];

int smaller(Date a, Date b)
{
if (a[2] < b[2]) return 1; // compare years
if (a[2] == b[2] && a[1] < b[1]) return 1; // compare months - but make sure years are equal
if (a[2] == b[2] && a[1] == b[1] && a[0] < b[0]) return 1; // compare days but make sure years and months are equal
return 0;
}

int main()
{
Date a = {2,25,2015}; //Date : d,mm,yyyy
Date b = {4,15,2016};
Date c = {7,10,2017};

printf("%s\n", (smaller(a, b) && smaller(b, c)) ? "b between a and c" : "b is not between a and c");
}

typedef是为了让代码更容易阅读。您还可以

关于检查日期是否在c(数组)中的两个日期之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35626133/

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