C++申明一个表示时间的结构体
2个回答
展开全部
#include<iostream>
using namespace std;
enum M{JAN=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,OTHER};
class Timedate{
private:
int Year;
M month1;
int Day;
int Hour;
int Minute;
int Second;
public:
void Settime(int,M,int,int,int,int);
void Outtime(void);
};
void Timedate::Settime(int year,M month2,int day,int hour,int minute,int second){
Year=year;
month1=month2;
Day=day;
Hour=hour;
Minute=minute;
Second=second;
}
void Timedate::Outtime(void){
cout<<"你所设置的时间是:"<<Year<<"年"<<month1<<"月"<<Day<<"日"<<Hour<<"时"<<Minute<<"分"<<Second<<"秒"<<endl;
}
void main(){
int year=2001,month,day=1,hour=1,minute=1,second=1;//给所有变量初始化,防止直接进入每个变量错误的范围内
M month2=JAN;
yerr:
if(year>2008||year<2000){cout<<"你输入的年份不在规定年份内,请重新输入!"<<endl;}
cout<<"请输入你要输入的年份(2000-2008):";
cin>>year;
if(year>2008||year<2000)goto yerr;
merr:
if(month2<1||month2>12){cout<<"你输入的月份有误,请重新输入!"<<endl;}
cout<<"请输入你要输入的月份:";
cin>>month;
switch(month){
case 1:month2=JAN;break;
case 2:month2=FEB;break;
case 3:month2=MAR;break;
case 4:month2=APR;break;
case 5:month2=MAY;break;
case 6:month2=JUN;break;
case 7:month2=JUL;break;
case 8:month2=AUG;break;
case 9:month2=SEP;break;
case 10:month2=OCT;break;
case 11:month2=NOV;break;
case 12:month2=DEC;break;
default:month2=OTHER;
}
if(month2<1||month2>12)goto merr;
derr:
switch(month){
case 1:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 2:if(day<1||day>29){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 3:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 4:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 5:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 6:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 7:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 8:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 9:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 10:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 11:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 12:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
}
cout<<"请输入你要输入的日期:";
cin>>day;
switch(month){
case 1:if(day<1||day>31)goto derr;
case 2:if(day<1||day>29)goto derr;
case 3:if(day<1||day>31)goto derr;
case 4:if(day<1||day>30)goto derr;
case 5:if(day<1||day>31)goto derr;
case 6:if(day<1||day>30)goto derr;
case 7:if(day<1||day>31)goto derr;
case 8:if(day<1||day>31)goto derr;
case 9:if(day<1||day>30)goto derr;
case 10:if(day<1||day>31)goto derr;
case 11:if(day<1||day>30)goto derr;
case 12:if(day<1||day>31)goto derr;
}
herr:
if(hour>23||hour<0){cout<<"你输入的时间有误,请重新输入!"<<endl;}
cout<<"几点:";
cin>>hour;
if(hour>23||year<0)goto herr;
minerr:
if(minute>59||minute<0){cout<<"你输入的时间有误,请重新输入!"<<endl;}
cout<<"几分:";
cin>>minute;
if(minute>59||minute<0)goto minerr;
serr:
if(second>59||second<0){cout<<"你输入的时间有误,请重新输入!"<<endl;}
cout<<"几秒:";
cin>>second;
if(second>59||second<0)goto serr;
Timedate da;
da.Settime(year,month2,day,hour,minute,second);
da.Outtime();
}
using namespace std;
enum M{JAN=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC,OTHER};
class Timedate{
private:
int Year;
M month1;
int Day;
int Hour;
int Minute;
int Second;
public:
void Settime(int,M,int,int,int,int);
void Outtime(void);
};
void Timedate::Settime(int year,M month2,int day,int hour,int minute,int second){
Year=year;
month1=month2;
Day=day;
Hour=hour;
Minute=minute;
Second=second;
}
void Timedate::Outtime(void){
cout<<"你所设置的时间是:"<<Year<<"年"<<month1<<"月"<<Day<<"日"<<Hour<<"时"<<Minute<<"分"<<Second<<"秒"<<endl;
}
void main(){
int year=2001,month,day=1,hour=1,minute=1,second=1;//给所有变量初始化,防止直接进入每个变量错误的范围内
M month2=JAN;
yerr:
if(year>2008||year<2000){cout<<"你输入的年份不在规定年份内,请重新输入!"<<endl;}
cout<<"请输入你要输入的年份(2000-2008):";
cin>>year;
if(year>2008||year<2000)goto yerr;
merr:
if(month2<1||month2>12){cout<<"你输入的月份有误,请重新输入!"<<endl;}
cout<<"请输入你要输入的月份:";
cin>>month;
switch(month){
case 1:month2=JAN;break;
case 2:month2=FEB;break;
case 3:month2=MAR;break;
case 4:month2=APR;break;
case 5:month2=MAY;break;
case 6:month2=JUN;break;
case 7:month2=JUL;break;
case 8:month2=AUG;break;
case 9:month2=SEP;break;
case 10:month2=OCT;break;
case 11:month2=NOV;break;
case 12:month2=DEC;break;
default:month2=OTHER;
}
if(month2<1||month2>12)goto merr;
derr:
switch(month){
case 1:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 2:if(day<1||day>29){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 3:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 4:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 5:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 6:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 7:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 8:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 9:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 10:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 11:if(day<1||day>30){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
case 12:if(day<1||day>31){cout<<"此月的日期输入有误,请重新输入!"<<endl;};break;
}
cout<<"请输入你要输入的日期:";
cin>>day;
switch(month){
case 1:if(day<1||day>31)goto derr;
case 2:if(day<1||day>29)goto derr;
case 3:if(day<1||day>31)goto derr;
case 4:if(day<1||day>30)goto derr;
case 5:if(day<1||day>31)goto derr;
case 6:if(day<1||day>30)goto derr;
case 7:if(day<1||day>31)goto derr;
case 8:if(day<1||day>31)goto derr;
case 9:if(day<1||day>30)goto derr;
case 10:if(day<1||day>31)goto derr;
case 11:if(day<1||day>30)goto derr;
case 12:if(day<1||day>31)goto derr;
}
herr:
if(hour>23||hour<0){cout<<"你输入的时间有误,请重新输入!"<<endl;}
cout<<"几点:";
cin>>hour;
if(hour>23||year<0)goto herr;
minerr:
if(minute>59||minute<0){cout<<"你输入的时间有误,请重新输入!"<<endl;}
cout<<"几分:";
cin>>minute;
if(minute>59||minute<0)goto minerr;
serr:
if(second>59||second<0){cout<<"你输入的时间有误,请重新输入!"<<endl;}
cout<<"几秒:";
cin>>second;
if(second>59||second<0)goto serr;
Timedate da;
da.Settime(year,month2,day,hour,minute,second);
da.Outtime();
}
推荐于2016-08-17
展开全部
//---------------------------------------------------------------------------
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
typedef struct {
unsigned year;
unsigned month;
unsigned day;
unsigned hour;
unsigned min;
unsigned sec;
} tm;
int main(void)
{
tm t;
cout<<"Year:"<<endl;
cin>>t.year;
cout<<"Month:"<<endl;
cin>>t.month ;
cout<<"Day:"<<endl;
cin>>t.day;
cout<<"Hour:"<<endl;
cin>>t.hour ;
cout<<"Min:"<<endl;
cin>>t.min;
cout<<"Sec:"<<endl;
cin>>t.sec;
cout<<t.hour<<':'<<t.min<<':'<<t.sec<<' '<<t.year<<'-'<<t.month<<'-'<<t.day<<endl;
return 0;
}
//---------------------------------------------------------------------------
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
typedef struct {
unsigned year;
unsigned month;
unsigned day;
unsigned hour;
unsigned min;
unsigned sec;
} tm;
int main(void)
{
tm t;
cout<<"Year:"<<endl;
cin>>t.year;
cout<<"Month:"<<endl;
cin>>t.month ;
cout<<"Day:"<<endl;
cin>>t.day;
cout<<"Hour:"<<endl;
cin>>t.hour ;
cout<<"Min:"<<endl;
cin>>t.min;
cout<<"Sec:"<<endl;
cin>>t.sec;
cout<<t.hour<<':'<<t.min<<':'<<t.sec<<' '<<t.year<<'-'<<t.month<<'-'<<t.day<<endl;
return 0;
}
//---------------------------------------------------------------------------
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询