[英]Date Validation and Conversion in C++
I have to write 2 functions.我必须写2个函数。 One that takes in a date as a string and checks if its in mm/dd/yy format;将日期作为字符串并检查其是否为 mm/dd/yy 格式的一种; if its not in the correct format, it should be edited to make it so.如果它的格式不正确,则应对其进行编辑以使其正确。 The other function should convert the validated date to the format "Month dd, 20yy".另一个函数应该将验证日期转换为“Month dd, 20yy”格式。
I'm pretty sure I can take care of the second function, but I am having trouble with the first one.我很确定我可以处理第二个功能,但是我在处理第一个功能时遇到了麻烦。 I just have no idea how to check if its in that format... any ideas?我只是不知道如何检查它是否是那种格式……有什么想法吗?
I thought that this would work, but it doesn't seem to...我以为这会奏效,但似乎没有……
Updated code:更新代码:
bool dateValidation(string shipDate)
{
string temp;
if(shipDate.length() == 8 )
{
if(shipDate[2] == '/' && shipDate[5] =='/')
{
int tempDay, tempMonth, tempYear;
//Gather month
temp = shipDate[0];
temp += shipDate[1];
//convert string to int
tempMonth = temp.atoi;
temp = "";
//Gather day
temp = shipDate[3];
temp += shipDate[4];
//convert string to int
tempDay = temp.atoi;
temp = "";
//Gather year
temp = shipDate[6];
temp += shipDate[7];
//convert string to int
tempYear = temp.atoi;
temp = "";
if(tempMonth > 0 && tempMonth <= 12)
{
if(tempMonth == 9 ||
tempMonth == 4 ||
tempMonth == 6 ||
tempMonth == 11 ||)
{
if(tempDay > 0 && tempDay <= 30)
{
if 30 days
}
}
else if(tempMonth == 2)
{
if(tempDay > 0 && tempDay <= 28)
{
if 28 days
}
}
else
{
if(tempDay > 0 && tempDay <= 31)
{
if 31 days
}
}
}
}
}
}
There are 4 things you want to check:您需要检查 4 件事:
This should take care of the format, but if you want to make sure that the date is valid:这应该注意格式,但如果您想确保日期有效:
This looks a lot like a project I am about to grade.... You should verify that it is Gregorian Calendar compliant if it is the project I am about to grade.这看起来很像我要评分的项目......如果它是我要评分的项目,您应该验证它是否符合公历。 1/1/2012 is definitely valid though so what you may want to do and what I would hope you consider is creating a switch statement that examines for formats like 1/12/2012 and 10/2/2012 because these are valid. 2012 年 1 月 1 日绝对有效,因此您可能想要做的以及我希望您考虑的是创建一个 switch 语句,用于检查 1/12/2012 和 10/2/2012 等格式,因为它们是有效的。 Then parse out the month day and year from these.然后从这些中解析出月日和年。 Then verify that they are within the limit of the Gregorian calendar.然后验证它们是否在公历的范围内。 If it is for a class which I would guess that it is, you should consider writing the verification as a separate function from the parsing function.如果是针对我猜是这样的类,您应该考虑将验证编写为与解析函数分开的函数。
So first ask whether the date is too long if not, is it too short, if not which version is it, then pass the dmy to the verification function.所以先问日期是不是太长了,是不是太短了,不是是哪个版本,然后把dmy传给验证函数。 This kind of modularity will simplify your code and reduce instructions.这种模块化将简化您的代码并减少指令。
something like就像是
bool dateValidation(string shipDate) { string temp; bool dateValidation(string shipDate) { string temp;
switch(shipDate.length())
{
case(10):
// do what your doing
verify(m,d,y);
break;
case(8):
//dealing with single digits
// verify 1 and 3 are '/' and the rest are numbers
verifiy(m,d,y);
break;
case(9):
//a little more heavy lifting here
// but its good thinking for a new programmer
verifiy(m,d,y);
break;
default:
//fail message
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.