这是一个用C#编写的,用NUnit进行单元测试的程序。要实现对一个日期加一,判断是否合法。请问我这个程序是否有内存溢出?是否还有别的问题?应该如何修改?using System;
using NUnit.Framework;namespace NextDate
{
class DateType
{
public int year;
public int month;
public int day; public DateType(int y, int m, int d)
{
year = y;
month = m;
day=d;
}
} class NextDate
{
public bool leap(int y)
{
if (y % 100 == 0 && y % 400 == 0)
return true;
if (y % 100 != 0 && y % 4 == 0)
return true;
return false;
}
public DateType plus(DateType today)
{
bool c1,c2;
DateType tomorrow=new DateType(0,0,0);
tomorrow.year = today.year;
tomorrow.month = today.month;
tomorrow.day = today.day; c1=(1<=today.day) && (today.day<=31);
c2 = (1 <= today.month) && (today.month <= 12);
if(c1==false)
return new DateType(0,0,0);
if(c2==false)
return new DateType(0,0,0); switch(today.month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if(today.day<31)
tomorrow.day=today.day+1;
else
{
tomorrow.day=1;
tomorrow.day=today.month+1;
}
break;
case 4:
case 6:
case 9:
case 11:
if(today.day<30)
tomorrow.day = today.day + 1;
else
{
tomorrow.day = 1;
tomorrow.day = today.month + 1;
}
break;
case 2:
if(today.day<28)
tomorrow.day = 1;
else
{
if(today.day==28)
{
if(leap(today.year))
tomorrow.day=29;
else
{
tomorrow.day=1;
tomorrow.month=3;
}
}
else
if(today.day==29)
{
tomorrow.day=1;
tomorrow.month=3;
}
else
return new DateType(0,0,0);
}
break;
} return tomorrow;
}
} [TestFixture]
class Program
{
[Test]
public static void Main(string[] args)
{
NextDate a = new NextDate(); Assert.AreEqual(new DateType(2001, 4, 15), a.plus(new DateType(2001, 4, 15)),"aa");
}
}
}
using NUnit.Framework;namespace NextDate
{
class DateType
{
public int year;
public int month;
public int day; public DateType(int y, int m, int d)
{
year = y;
month = m;
day=d;
}
} class NextDate
{
public bool leap(int y)
{
if (y % 100 == 0 && y % 400 == 0)
return true;
if (y % 100 != 0 && y % 4 == 0)
return true;
return false;
}
public DateType plus(DateType today)
{
bool c1,c2;
DateType tomorrow=new DateType(0,0,0);
tomorrow.year = today.year;
tomorrow.month = today.month;
tomorrow.day = today.day; c1=(1<=today.day) && (today.day<=31);
c2 = (1 <= today.month) && (today.month <= 12);
if(c1==false)
return new DateType(0,0,0);
if(c2==false)
return new DateType(0,0,0); switch(today.month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if(today.day<31)
tomorrow.day=today.day+1;
else
{
tomorrow.day=1;
tomorrow.day=today.month+1;
}
break;
case 4:
case 6:
case 9:
case 11:
if(today.day<30)
tomorrow.day = today.day + 1;
else
{
tomorrow.day = 1;
tomorrow.day = today.month + 1;
}
break;
case 2:
if(today.day<28)
tomorrow.day = 1;
else
{
if(today.day==28)
{
if(leap(today.year))
tomorrow.day=29;
else
{
tomorrow.day=1;
tomorrow.month=3;
}
}
else
if(today.day==29)
{
tomorrow.day=1;
tomorrow.month=3;
}
else
return new DateType(0,0,0);
}
break;
} return tomorrow;
}
} [TestFixture]
class Program
{
[Test]
public static void Main(string[] args)
{
NextDate a = new NextDate(); Assert.AreEqual(new DateType(2001, 4, 15), a.plus(new DateType(2001, 4, 15)),"aa");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货