2009-12-24 83 views
0

我正在编写一个日期类,并且遇到后修复增量(前缀增量似乎很好)的问题。C#运算符超载后修复增量

下面是示例代码:

public class date 
{ 
    int year, 
     month, 
     day; 

    public date(int d, int m, int y) 
    { 
     day = d; 
     month = m; 
     year = y; 
    } 

    static public date operator ++(date d) 
    { 
     return d.Next(d); 
    } 
} 

的方法“接着(日期d)”发生的日期,并返回明天日期(I左出来为了简洁)。我想在C#中年轻一些,以了解为什么前缀是好的,但后缀增量什么都不做。但记住在C++中,我们必须有两种方法,而不仅仅是一种 - 用于前缀和后缀增量。

编译时也没有错误或警告。

回答

4

那么你没有显示Next方法,这将是一个方便...特别说明为什么它需要以date作为论点。我的猜测是你的Next方法有缺陷。

您还没有显示它失败后增量的例子。下面是这表明它确实工作一个简单的例子:

using System; 

public class Date 
{ 
    int year, month, day; 

    public Date(int d, int m, int y) 
    { 
     day = d; 
     month = m; 
     year = y; 
    } 

    public static Date operator ++(Date d) 
    { 
     return d.Next(); 
    } 

    private Date Next() 
    { 
     // Just a toy implementation, obviously 
     return new Date(day + 1, month, year); 
    } 

    static void Main() 
    { 
     Date x = new Date(1, 2, 3); 
     x++; 
     Console.WriteLine(x.day); // Prints 2 
    } 
} 

注意它是如何打印2,显示出这一天已递增(或更确切地说,x现指的Date一个新实例已递增日价值)。

就我个人而言,我不认为我会为Date类引入一个++运算符,但没关系。我还建议构造函数应该是年/月/日,而不是日/月/年;这是更传统的,并适合更好的情况下,你想让更多的参数更精确。

0

乔恩感谢,你是绝对正确的,让我装上缺少的next()方法是在类中:

public date Next(date d) 
    { 
     if (!d.valid()) return new date(); 
     date ndat = new date((d.Day() + 1), d.Month(), d.Year()); 
     if (ndat.valid()) return ndat; 
     ndat = new date(1, (d.Month() + 1), d.Year()); 
     if (ndat.valid()) return ndat; 
     ndat = new date(1, 1, (d.Year() + 1)); 
     return ndat; 
    }  

由于该使用有效()我也重视这一点:

public bool valid() 
    { 
     // This method will check the given date is valid or not. 
     // If the date is not valid then it will return the value false. 
     if (year < 0) return false; 
     if (month > 12 || month < 1) return false; 
     if (day > 31 || day < 1) return false; 
     if ((day == 31 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11))) 
      return false; 
     if (day == 30 && month == 2) return false; 
     if (day == 29 && month == 2 && (year % 4) != 0) return false; 
     if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false; 
     /* ASIDE. The duration of a solar year is slightly less than 365.25 days. Therefore, 
     years that are evenly divisible by 100 are NOT leap years, unless they are also 
     evenly divisible by 400, in which case they are leap years. */ 
     return true; 
    } 

Day(),Month()和Year()我认为是自我解释,但让我知道他们是否需要。我也有一个previous()方法,它与next()方法相反,我想在 - decrement方法中使用它。

现在在我的计划,我有

class Program 
{ 
    static void Main() 
    { 
     date today = new date(7,10,1985); 
     date tomoz = new date(); 

     tomorrow = today++; 

     tomorrow.Print(); // prints "7/10/1985" i.e. doesn't increment  
     Console.Read(); 
    } 
} 

所以它实际上并没有失败,只是打印当天的日期,而不是明天的,但正常工作,如果我用了++今天来代替。

关于D/M/Y的顺序,是的,我同意,更高的频率数据我可以看到这是如何改善的东西,接下来我将着手解决这个问题。

+0

呃,当然是吗?你明天将分配给今天的价值然后增加它。 – 2009-12-24 12:06:22

+0

你不会期望明天在2之后:'int today = 1; int明天=今天++;' – 2009-12-24 12:08:54

+0

啊坚果你是对的!谢谢Garry – Victor 2009-12-24 12:13:17

0
DateTime SchDate= DateTime.Now; 
SchDate= SchDate.AddDays(1); 

什么都可能是天或月/年你可以添加

0

我有一个额外的评论说,可能是太晚了原来的海报,但可能是有人在将来读书有用。

看着你实现“有效”:

if (day == 29 && month == 2 && (year % 4) != 0) return false; 
    if (day == 29 && month == 2 && (((year % 100) == 0) && ((year % 400) != 0))) return false; 

它都将失败29/2/2100,这是一个有效日期,但你的方法说,这是不是。