我想将表单的日期 strVal="1992-12-12" 或 "1992-9-9" 转换为 19921212 和 19920909。为此,我用 C/C++ 编写了以下代码。但问题在于它将 1992-12-12 转换为 1992012012。谁能指导我如何修复这个错误?我也可能有“1992-9”形式的输入,我想将其转换为 19920900。或“1992”到“19920000”
stringstream collectTimeBegin;
for (string::iterator it = strVal.begin(); it != strVal.end(); )
{
if (*it != '-')
{
cout<< "\n -- *it=" << *it;
collectTimeBegin << *it;
it++;
}
else
{
stringstream si("");
stringstream sj("");
int i;
it++;
si << *(it + 1);
sj<< *(it + 2);
i = atoi((si.str()).c_str()), j = atoi((sj.str()).c_str());
cout << "\n i=" << i << "\t j=" << j << "\n";
if ((i == 4) || (i == 5) || (i == 6) || (i == 7) || (i == 8) || (i == 9))
{
cout << "\n 1. *it=" << *it;
collectTimeBegin << *it;
it++;
}
else if ((j == 0) || (j == 1) || (j == 2) || (j == 3) || (j == 4) ||
(j == 5) || (j == 6) || (j == 7) || (j == 8) || (j == 9))
{
string str = "0";
cout << "\n 2. *it=" << *it;
collectTimeBegin << str;
collectTimeBegin << *it;
it++;
}
}
}