对于一项作业,我必须比较 C 中的两个日期使用结构。我不确定我的逻辑在这里是否错误
For an assignment I have to compare two dates use structures in C. I am not sure as to if my logic is wrong here
首先我比较了两个日期,看是早了还是一样了。我不确定我的逻辑在这里是错误的还是什么。 printf 语句似乎不起作用,说日期 1 早于或它们相同或日期 1 晚于。
#include<stdio.h>
#include<string.h>
struct dates
{
int month;
int date;
int year;
};
int compareDates(struct dates d1, struct dates d2)
{
printf(d2.year);
printf(d2.month);
printf(d2.date);
if(d2.year >= d1.year) // compares year
{
if(d2.month >= d1.month) // months
{
if(d2.date > d1.date)
{
printf("date 1 is earlier\n");
return - 1;
}
}
}
else if(d1.year == d2.year && d1.month == d2.month && d1.date == d2.date) // if they are the same
{
printf("They are the same\n");
return 0;
}
else
{
printf("D1 is later\n");
return 1;
}
}
int main()
{
struct dates d1;
struct dates d2;
printf("Enter year 1: ");
scanf("%d", &d1.year);
printf("Enter month 1: ");
scanf("%d", &d1.month);
printf("Enter date 1: ");
scanf("%d", &d1.date);
printf("Enter year 2: ");
scanf("%d", &d2.year);
printf(" Enter month 2: ");
scanf("%d", &d2.month);
printf("Enter date 2: ");
scanf("%d", &d2.date);
int return_value = compareDates(d1,d2);
}
似乎有很多情况(a)您没有在比较日期函数中发现。我建议从以下伪代码开始:
def isGreater(date1, date2):
# If year different, use that to decide.
if date1.year > date2.year: return true
if date1.year < date2.year: return false
# Years are the same, if month different use that.
if date1.month > date2.month: return true
if date1.month < date2.month: return false
# Years and months are the same, use day.
if date1.day > date2.day: return true
return false
这并没有提供特定的解决方案,但是,由于它是课堂作业,所以您应该自己做这件事(这将使您成为一个更好的开发人员,而不仅仅是提供一个现成的解决方案)。这只是向您展示方法。
(a) 具体来说,如果年和月都大于或等于并且日大于,则您只认为日期更大。那不会捕捉到 2019-01-30
大于 2018-12-01
.
的事实
compared the two dates and checked if the date is earlier or if it is the same. I am not sure as to if my logic is wrong here or what is.
printf(d2.year);
肯定是错的。尝试 printf("%d\n":, d2.year);
if(d2.month >= d1.month)
仅在 (d2.year == d1.year)
时才有意义,而不是 (d2.year >= d1.year)
逐个比较成员。从最重要的开始。
int compareDates(struct dates d1, struct dates d2) {
if (d1.year != d2.year) return d1.year > d2.year ? 1 : -1;
if (d1.month != d2.month) return d1.month > d2.month ? 1 : -1;
return (d1.date > d2.date) - (d1.date < d2.date);
}
如果成员不在 2019-11-15 < 2018-100-15
等主要范围内,则需要额外的代码。
首先我比较了两个日期,看是早了还是一样了。我不确定我的逻辑在这里是错误的还是什么。 printf 语句似乎不起作用,说日期 1 早于或它们相同或日期 1 晚于。
#include<stdio.h>
#include<string.h>
struct dates
{
int month;
int date;
int year;
};
int compareDates(struct dates d1, struct dates d2)
{
printf(d2.year);
printf(d2.month);
printf(d2.date);
if(d2.year >= d1.year) // compares year
{
if(d2.month >= d1.month) // months
{
if(d2.date > d1.date)
{
printf("date 1 is earlier\n");
return - 1;
}
}
}
else if(d1.year == d2.year && d1.month == d2.month && d1.date == d2.date) // if they are the same
{
printf("They are the same\n");
return 0;
}
else
{
printf("D1 is later\n");
return 1;
}
}
int main()
{
struct dates d1;
struct dates d2;
printf("Enter year 1: ");
scanf("%d", &d1.year);
printf("Enter month 1: ");
scanf("%d", &d1.month);
printf("Enter date 1: ");
scanf("%d", &d1.date);
printf("Enter year 2: ");
scanf("%d", &d2.year);
printf(" Enter month 2: ");
scanf("%d", &d2.month);
printf("Enter date 2: ");
scanf("%d", &d2.date);
int return_value = compareDates(d1,d2);
}
似乎有很多情况(a)您没有在比较日期函数中发现。我建议从以下伪代码开始:
def isGreater(date1, date2):
# If year different, use that to decide.
if date1.year > date2.year: return true
if date1.year < date2.year: return false
# Years are the same, if month different use that.
if date1.month > date2.month: return true
if date1.month < date2.month: return false
# Years and months are the same, use day.
if date1.day > date2.day: return true
return false
这并没有提供特定的解决方案,但是,由于它是课堂作业,所以您应该自己做这件事(这将使您成为一个更好的开发人员,而不仅仅是提供一个现成的解决方案)。这只是向您展示方法。
(a) 具体来说,如果年和月都大于或等于并且日大于,则您只认为日期更大。那不会捕捉到 2019-01-30
大于 2018-12-01
.
compared the two dates and checked if the date is earlier or if it is the same. I am not sure as to if my logic is wrong here or what is.
printf(d2.year);
肯定是错的。尝试 printf("%d\n":, d2.year);
if(d2.month >= d1.month)
仅在 (d2.year == d1.year)
时才有意义,而不是 (d2.year >= d1.year)
逐个比较成员。从最重要的开始。
int compareDates(struct dates d1, struct dates d2) {
if (d1.year != d2.year) return d1.year > d2.year ? 1 : -1;
if (d1.month != d2.month) return d1.month > d2.month ? 1 : -1;
return (d1.date > d2.date) - (d1.date < d2.date);
}
如果成员不在 2019-11-15 < 2018-100-15
等主要范围内,则需要额外的代码。