我有三个整数数组
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
函数创建所有布尔运算符(<=, >=, ==, !=, >
)
#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
是让代码更易于阅读。您仍然可以
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句