1. 首页
  2. 编程语言
  3. C
  4. 使用C++编写的万年历程序

使用C++编写的万年历程序

上传者: 2023-03-08 05:47:30上传 CPP文件 1.34KB 热度 10次
#include <iostream>
using namespace std;

int main() {
    int year, month, day;
    int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; // 闰年二月会被修改
    string week_days[7] = {"日", "一", "二", "三", "四", "五", "六"};

    cout << "请输入年份:" << endl;
    cin >> year;
    cout << "请输入月份:" << endl;
    cin >> month;
    cout << "请输入日期:" << endl;
    cin >> day;

    // 判断是否是闰年
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        month_days[1] = 29;
    }

    // 计算该日期是这一年中的第几天
    int sum = 0;
    for (int i = 0; i < month - 1; i++) {
        sum += month_days[i];
    }
    sum += day;

    // 计算该日期是星期几
    int week_day = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
    week_day += sum;
    week_day %= 7;

    // 输出结果
    cout << year << "年" << month << "月" << day << "日是星期" << week_days[week_day] << endl;

    return 0;
}
iostream>
用户评论