(1)用下面的格式输出日期 日/月/年 定义一个满足如下要求的Date类 (1)用下面的格式输出日期 日/月/年
答案:4 悬赏:40 手机版
解决时间 2021-02-09 14:29
- 提问者网友:咪咪
- 2021-02-09 01:36
(1)用下面的格式输出日期 日/月/年 定义一个满足如下要求的Date类 (1)用下面的格式输出日期 日/月/年
最佳答案
- 五星知识达人网友:蓝房子
- 2021-02-09 01:53
这个不是写过么?上次有个XX,帮他写了把别人的列为最佳答案
class Date
{
public:
Date()
{
year = 0;
month = 0;
day = 0;
}
void SetDate(int y, int m, int d);
void GetDate();
void Tomorrow();
private:
int year;
int month;
int day;
};
void Date::SetDate(int y, int m, int d)
{
if (m < 1 || m > 12)
{
cout << "month error!" << endl;
return;
}
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if (d < 1 || d > 31)
{
cout << "day error!" << endl;
return;
}
}
else if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d < 1 || d > 30)
{
cout << "day error!" << endl;
return;
}
}
else
{
if ((((y % 4) == 0) && ((y % 100) != 0)) || ((y % 400) == 0))
{
if (d < 1 || d > 29)
{
cout << "day error!" << endl;
return;
}
}
else
{
if (d < 1 || d > 28)
{
cout << "day error!" << endl;
return;
}
}
}
year = y;
month = m;
day = d;
}
void Date::GetDate()
{
cout << day << "/" << month << "/" << year << endl;
}
void Date::Tomorrow()
{
day++;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day > 31)
{
day = 1;
month++;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day > 30)
{
day = 1;
month++;
}
}
else
{
if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0))
{
if (day > 29)
{
day = 1;
month++;
}
}
else
{
if (day > 28)
{
day = 1;
month++;
}
}
}
if (month > 12)
{
month = 1;
year++;
}
}
int main()
{
Date date;
date.SetDate(2000, 2, 29);
date.GetDate();
date.Tomorrow();
date.GetDate();
}
void main() //gets问题
{
char name [5][10];
float score[5];
int i;
for (i = 0; i < 5; i++)
{
//gets(name[i]);
scanf("%s", name[i]);
scanf("%f", &score[i]);
}
cout << "-----------------------\n";
for (i = 0; i < 5; i++)
{
printf("%s,%f\n", name[i], score[i]);
}
}
class Date
{
public:
Date()
{
year = 0;
month = 0;
day = 0;
}
void SetDate(int y, int m, int d);
void GetDate();
void Tomorrow();
private:
int year;
int month;
int day;
};
void Date::SetDate(int y, int m, int d)
{
if (m < 1 || m > 12)
{
cout << "month error!" << endl;
return;
}
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if (d < 1 || d > 31)
{
cout << "day error!" << endl;
return;
}
}
else if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d < 1 || d > 30)
{
cout << "day error!" << endl;
return;
}
}
else
{
if ((((y % 4) == 0) && ((y % 100) != 0)) || ((y % 400) == 0))
{
if (d < 1 || d > 29)
{
cout << "day error!" << endl;
return;
}
}
else
{
if (d < 1 || d > 28)
{
cout << "day error!" << endl;
return;
}
}
}
year = y;
month = m;
day = d;
}
void Date::GetDate()
{
cout << day << "/" << month << "/" << year << endl;
}
void Date::Tomorrow()
{
day++;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day > 31)
{
day = 1;
month++;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day > 30)
{
day = 1;
month++;
}
}
else
{
if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0))
{
if (day > 29)
{
day = 1;
month++;
}
}
else
{
if (day > 28)
{
day = 1;
month++;
}
}
}
if (month > 12)
{
month = 1;
year++;
}
}
int main()
{
Date date;
date.SetDate(2000, 2, 29);
date.GetDate();
date.Tomorrow();
date.GetDate();
}
void main() //gets问题
{
char name [5][10];
float score[5];
int i;
for (i = 0; i < 5; i++)
{
//gets(name[i]);
scanf("%s", name[i]);
scanf("%f", &score[i]);
}
cout << "-----------------------\n";
for (i = 0; i < 5; i++)
{
printf("%s,%f\n", name[i], score[i]);
}
}
全部回答
- 1楼网友:西岸风
- 2021-02-09 04:18
引用Jorecus的回答:
#include
class Date
{
public:
bool Legal(int y, int m, int d)
{if(y>9999||y<1||d<1||m<1||m>12)
return false;
int dayLimit=30;
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
dayLimit++;
if(m==2) dayLimit = IsLeapYear(y) ? 29 : 28;
return (d>dayLimit)? false : true;}
bool IsLeapYear(int y)
{return (y%4==0)&&(y%100==0)||(y%400==0);}
void SetDay(int y,int m,int d)
{year=y;month=m;day=d;}
void AddOneDay()
{if(Legal(year,month,day+1))
day++;
else if(Legal(year,month+1,1))
day=1,month++;
else if(Legal(year+1,1,1))
year++,month=1,day=1;
}
void Display()
{
cout < }
protected:
int year;
int month;
int day;
};
void main()
{Date a;
a.SetDay(2011,9,29);
a.Display();
a.AddOneDay();
a.Display();
}一楼的运行不了
#include
class Date
{
public:
bool Legal(int y, int m, int d)
{if(y>9999||y<1||d<1||m<1||m>12)
return false;
int dayLimit=30;
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
dayLimit++;
if(m==2) dayLimit = IsLeapYear(y) ? 29 : 28;
return (d>dayLimit)? false : true;}
bool IsLeapYear(int y)
{return (y%4==0)&&(y%100==0)||(y%400==0);}
void SetDay(int y,int m,int d)
{year=y;month=m;day=d;}
void AddOneDay()
{if(Legal(year,month,day+1))
day++;
else if(Legal(year,month+1,1))
day=1,month++;
else if(Legal(year+1,1,1))
year++,month=1,day=1;
}
void Display()
{
cout <
protected:
int year;
int month;
int day;
};
void main()
{Date a;
a.SetDay(2011,9,29);
a.Display();
a.AddOneDay();
a.Display();
}一楼的运行不了
- 2楼网友:举杯邀酒敬孤独
- 2021-02-09 04:09
楼上的秀才做的可行,不过你可以优化一下!
- 3楼网友:一袍清酒付
- 2021-02-09 03:23
#include
class Date
{
public:
bool Legal(int y, int m, int d)
{if(y>9999||y<1||d<1||m<1||m>12)
return false;
int dayLimit=30;
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
dayLimit++;
if(m==2) dayLimit = IsLeapYear(y) ? 29 : 28;
return (d>dayLimit)? false : true;}
bool IsLeapYear(int y)
{return (y%4==0)&&(y%100==0)||(y%400==0);}
void SetDay(int y,int m,int d)
{year=y;month=m;day=d;}
void AddOneDay()
{if(Legal(year,month,day+1))
day++;
else if(Legal(year,month+1,1))
day=1,month++;
else if(Legal(year+1,1,1))
year++,month=1,day=1;
}
void Display()
{
cout < }
protected:
int year;
int month;
int day;
};
void main()
{Date a;
a.SetDay(2011,9,29);
a.Display();
a.AddOneDay();
a.Display();
}
class Date
{
public:
bool Legal(int y, int m, int d)
{if(y>9999||y<1||d<1||m<1||m>12)
return false;
int dayLimit=30;
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
dayLimit++;
if(m==2) dayLimit = IsLeapYear(y) ? 29 : 28;
return (d>dayLimit)? false : true;}
bool IsLeapYear(int y)
{return (y%4==0)&&(y%100==0)||(y%400==0);}
void SetDay(int y,int m,int d)
{year=y;month=m;day=d;}
void AddOneDay()
{if(Legal(year,month,day+1))
day++;
else if(Legal(year,month+1,1))
day=1,month++;
else if(Legal(year+1,1,1))
year++,month=1,day=1;
}
void Display()
{
cout <
protected:
int year;
int month;
int day;
};
void main()
{Date a;
a.SetDay(2011,9,29);
a.Display();
a.AddOneDay();
a.Display();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯