我只是想知道是否有人注意到我的代码块出错了。该程序应该是一个比较2个日期的测试程序。如果调用日期更大,则我正在处理的函数应该返回1,调用日期小于1时返回1,如果调用日期等于参数中的日期则返回0。我的考试计划:
#include <cstdlib>
#include <iostream>
#include <string>
#include "date.h"
using namespace std;
//date is initialized in a month/day/year format.
int main(int argc, char* argv[])
{
string* d;
date d1(4,1,4);
date d4(4,4,4);
int greaterTest = d4.compareTo(d1);
int lessTest = d1.compareTo(d4);
cout << greaterTest << endl; //i believe these two lines are printing out a
cout << lessTest << endl; //location in memory
cout<<&d <<endl;
system("pause");
return EXIT_SUCCESS;
}
巨大的compareTo()函数:
int date::compareTo (date another_date)
{
if (this->year == another_date.year && this->month == month && this->day < another_date.day) //if both year and month are the same, test to see if day is less
{
return -1;
}
else if (this->year == another_date.year && this->month == month && this->day > another_date.day) //if both year and month are the same, test to see if day is greater
{
return 1;
}
else if (this->year == another_date.year && this->month > month) //if the years are the same, test to see if the invoking month is greater
{
return 1;
}
else if (this->year == another_date.year && this->month < month) //if the years are the same, test to see if the invoking month is less
{
return -1;
}
else if (this->year > another_date.year) //test to see if the invoking year is greater
{
return 1;
}
else if (this->year < another_date.year) //test to see if the invoking year is less
{
return -1;
}
else if(this-> year == another_date.year && this-> month == another_date.month //test if the dates are exactly the same
&& this-> day == another_date.day)
{
return 0;
}
//else{ return 15;} //if none are true, return 15
}
我得到的唯一问题是当我尝试更改日期时(日期的第二个参数)。
答案 0 :(得分:2)
我不确定这是不是问题,因为我无法测试...但是,你的compareTo
函数有这一行:
this->month == month
不应该是:
this->month == another_date.month
答案 1 :(得分:2)
在第一个if语句和下面的一些语句中,你有:
this->month == month
这是比较月份,我认为你的意思是:
this->month == another_date.month
此外,您无需一直使用'this'指针,
month == another_date.month
应该足够了。
答案 2 :(得分:0)
这可能会从早期退出中受益:
int date::compareTo (date another_date)
{
if (year > another_date.year) {
//the invoking year is greater
return 1;
}
if (year < another_date.year) {
//the invoking year is less
return -1;
}
// if we reached here, the years are the same. Don't need to compare them for the other cases
if (month > another_date.month) {
return 1;
}
if (month < another_date.month) {
return -1;
}
// if we reached here, the year and month are the same
if (day > another_date.day) {
return 1;
}
if (day < another_date.day) {
return -1;
}
// if we reached here, the year and month and day are the same
return 0;
}
一路上,剪切+粘贴错误......消失了,因为测试变得多余了。
答案 3 :(得分:0)
我没有在原始代码中找到您的错误,因为我很难阅读。我想这就是你没找到它的原因。
这种替代方案可能更容易阅读,更容易证明是正确的:
// untested
int date::compareTo (date another_date)
{
if (year < another_date.year) return -1;
if (year > another_date.year) return 1;
if (month < another_date.month) return -1;
if (month > another_date.month) return 1;
if (day < another_date.day) return -1;
if (day > another_date.day) return 1;
return 0;
}
答案 4 :(得分:0)
除非你真的开始进行逐元素比较,否则我会将每组输入放入struct tm
,然后使用mktime
将这些输入转换为{{1}并直接比较两个time_t
。在典型的情况下,那些将是自1970年1月1日午夜以来的秒数的32位或64位整数,因此在转换之后,比较变得微不足道。