C++编程:写一个表示时间的结构体,有年月日时秒的成员,可以使用short类型 (在主程序输入输出)

2025-01-05 14:55:56
推荐回答(5个)
回答1:

#include
using namespace std ;

typedef struct
{
int year ;
int month ;
int day;
int hour ;
int minute ;
int second ;
} Time ;

int main()
{
Time *time = new Time() ;

cout << "请输入年:" ;
cin >> time->year ;
cout << "请输入月:";
cin >> time->month ;
cout << "请输入日:";
cin >> time->day ;
cout << "请输入时:";
cin >> time->hour ;
cout << "请输入分钟:" ;
cin >> time->minute ;
cout << "请输入秒:" ;
cin >> time->second ;

//Display
cout << time->year << " 年 " << time->month << " 月 " << time->day << " 日 "
<< time->hour << " 时 " << time->minute << " 分 " << time->second << " 秒" << endl ;

return 0 ;
}

回答2:

#include
void main()
{
int year,month,day,hour,sec,miu;
printf("输入年:");scanf("%d",&year);
printf("输入月:");scanf("%d",&month);
printf("输入日:");scanf("%d",&day);
printf("输入时:");scanf("%d",&hour);
printf("输入分:");scanf("%d",&sec);
printf("输入秒:");scanf("%d",&miu);

printf("输入的日期为:%d年%d月%d日 %d:%d:%d",year,month,day,hour,sec,miu);

}

回答3:

#include
typedef struct time
{
int year,month,day,hour,sec,miu;

}time;
void main()
{
time test;
cout<<"输入年:"< cin>>test.year;
cout<<"输入月:"< cin>>test.month;
cout<<"输入日:"< cin>>test.day;
cout<<"输入时:"< cin>>test.hour;
cout<<"输入分钟:"< cin>>test.miu;
cout<<"输入秒:"< cin>>test.sec;
cout<<"您输入的日期为:"<
}

回答4:

#include
using namespace std;
struct time{
int y;
int m;
int d;
int h;
int mi;
int sec;};
int input();
int main()
{
time newtime;
cout<<"输入年:\n";
newtime.y=input();
cout<<"输入月:\n";
newtime.m=input();
cout<<"输入日:\n";
newtime.d=input();
cout<<"输入时:\n";
newtime.h=input();
cout<<"输入分:\n";
newtime.mi=input();
cout<<"输入秒:\n";
newtime.sec=input();
cout<<"你输入的是:"< < < < return 0;
}
int input()
{
int temp;
while(!(cin>>temp))
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"请输入数字!\n";
}
return temp;
}
当输入非数值时会报警。

回答5:

#include
using namespace std;

struct Time{
short year,month,day,hour,minute,second;
void SetTime();
void Print();
};
void Time::SetTime()
{
cout<<"set year:"; cin>>year;
cout<<"set month:"; cin>>month;
cout<<"set day:"; cin>>day;
cout<<"set hour:"; cin>>hour;
cout<<"set minute:"; cin>>minute;
cout<<"set second:"; cin>>second;
}
void Time::Print()
{
cout<<"The time is:";
cout< <}

int main()
{
Time TIME;
cout<<"Set Time\n";
TIME.SetTime();
TIME.Print();
return 0;
}