解题思路:
既然是C++,用class代替struct也是很正常的吧。计算第几天就是建一个数组存放每个月有几天,然后把前面的月的天数加起来,加上day,最后闰年+1。
注意事项:
好像也没啥可说的。
参考代码:
#include <iostream> using namespace std; const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 定义成13是为了方便对应数组下标 class Date { private: short year; // 年 short month; // 月 short day; // 日 public: // 构造函数 Date(short year, short month, short day) { this->year = year; this->month = month; this->day = day; } // 析构函数 virtual ~Date() { } void countDay() { short count = day; // 计数 for(int i = 1; i < month; i++) { count += days[i]; } if((!(year % 4) && (year % 100)) || !(year % 400)) { count++; // 闰年则+1 } cout << count << endl; // 可以return但没必要 } }; int main() { short year = 0; short month = 0; short day = 0; cin >> year >> month >> day; // 读输入 Date date(year, month, day); date.countDay(); return 0; }
0.0分
1 人评分
Hello, world! (C语言代码)浏览:1202 |
【亲和数】 (C语言代码)浏览:860 |
输出正反三角形 (C语言代码)格式错误!!!浏览:1141 |
C语言训练-大、小写问题 (C语言代码)浏览:732 |
wu-理财计划 (C++代码)浏览:837 |
2005年春浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:591 |
简单的a+b (C语言代码)浏览:419 |
有关字符,字符串的输入输出函数说明浏览:479 |
星期判断机 (C语言代码)浏览:859 |
1035 题解浏览:786 |