1个回答
展开全部
#include<iostream>
using namespace std;
class Date {
private:
char year[5];
char month[3];
char day[3];
public:
Date(char _year[],char _month[],char _day[])
{
strcpy(year,_year);
strcpy(month,_month);
strcpy(day,_day);
}
void display()
{
switch(strlen(year))
{
case 0:
cout << "0000"; break;
case 1:
cout << "000" << year; break;
case 2:
cout << "00" << year; break;
case 3:
cout << "0" << year; break;
case 4:
cout << year; break;
}
cout << "/";
switch(strlen(month))
{
case 0:
cout << "00"; break;
case 1:
cout << "0" << month; break;
case 2:
cout << month; break;
}
cout << "/";
switch(strlen(day)) {
case 0:
cout << "00"; break;
case 1:
cout << "0" << day; break;
case 2:
cout << day; break;
}
}
};
class Time {
private:
char hour[3];
char minute[3];
public:
Time(char _hour[], char _minute[])
{
strcpy(hour,_hour);
strcpy(minute,_minute);
}
void display()
{
switch(strlen(hour))
{
case 0:
cout << "00"; break;
case 1:
cout << "0" << hour; break;
case 2:
cout << hour; break;
}
cout << ":";
switch(strlen(minute)) {
case 0:
cout << "00"; break;
case 1:
cout << "0" << minute; break;
case 2:
cout << minute; break;
}
}
};
int main(int argc,char* argv[])
{
char year[5];
char month[3];
char day[3];
char hour[3];
char minute[3];
while(cin >> year >> month >> day >> hour >> minute)
{
if(strcmp(year,"0")==0
&& strcmp(month,"0")==0
&& strcmp(day,"0")==0
&& strcmp(hour,"0")==0
&& strcmp(minute,"0")==0
)break;
Date a = Date(year,month,day);
Time t = Time(hour,minute);
a.display();
cout << " ";
t.display();
cout << endl;
}
system("pause");
return 0;
}
追问
如果用setfill('0')好像会少很多代码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询