[YTU]_2911(我想放假)
題目描述
小明的弟弟上小學了,每次剛入學就想知道什么時候放假,但是每學期開學的日子和每學期的有效天數都不一樣,請你編程幫他計算放假日期。本題只需要提交填空部分
#include <iostream>
using namespace std;
class Date
{
public:
??? void input(int y,int m,int d);
??? friend Date operator+(Date &c,int &day);
??? void display();
private:
??? int year;
??? int month;
??? int day;
};
void Date::input(int y,int m,int d)
{
??? year=y;
??? month=m;
??? day=d;
}
Date operator+(Date &c,int &day)
{
??? /*********************/
?? ? ? ? ? ?? 填空部分
??? /*********************/
}
void Date::display()
{
??? cout<<year<<"/"<<month<<"/"<<day<<endl;
}
int main()
{
??? Date date1,date2;
??? int y,m,d;
??? int day;
??? cin>>y>>m>>d;
??? date1.input(y,m,d);
??? cin>>day;
??? date2=date1+day;
??? date2.display();
??? return 0;
}
輸入
第一行輸入開學的年月日,以空格隔開;第二行輸入本學期的有效天數
輸出
輸出計算后得到的放假日期,格式為年/月/日。
樣例輸入
2008 2 29 140樣例輸出
2008/7/18#include <iostream> using namespace std; class Date { public: void input(int y,int m,int d); friend Date operator+(Date &c,int &day); void display(); private: int year; int month; int day; }; void Date::input(int y,int m,int d) { year=y; month=m; day=d; } Date operator+(Date &c,int &day) { int array[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};if((c.year%4==0&&c.year%100!=0)||c.year%400==0)array[2]=29;c.day+=day;while(c.day>array[c.month]){c.day-=array[c.month];++c.month;if(c.month>12){++c.year;c.month-=12;}array[2]=((c.year%4==0&&c.year%100!=0)||c.year%400==0)?29:28;}return c; } void Date::display() { cout<<year<<"/"<<month<<"/"<<day<<endl; } int main() { Date date1,date2; int y,m,d; int day; cin>>y>>m>>d; date1.input(y,m,d); cin>>day; date2=date1+day; date2.display(); return 0; }總結
以上是生活随笔為你收集整理的[YTU]_2911(我想放假)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2907(类重载实现矩阵加法
- 下一篇: [YTU]_2445(C++习题 输入输