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

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int Week(int day);
int output(int, int*);
int output2(int, int*);
int main()
{
cout << "本程序为实现万年历的功能,请输入要查找的年份:" << endl;
int ye;
cin >> ye; //要查看的年份
int year = ye - 1;
//从公元元年 1 月 1 日到这一年 1 月 1 日一共的天数
int day = ((year / 4) - (year / 100) + (year / 400)) * 366 + (year - ((year / 4) - (year / 100) + (year / 400))) * 365;
int month1[31] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
int month2b[28] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28};
int month2a[29] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
int month4[30] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
int Jan = sizeof(month1) / sizeof(0);
int Feb;
int Apr = sizeof(month4) / sizeof(0);
//一月
cout << "一月" << endl;
Week(day);
output(day, month1);
//二月
int day1 = day + Jan; //二月之前的天数
if((day1 % 7) != 0)
cout << "\n";
cout << "二月" << endl;
Week(day1);
if((ye % 4 == 0 && ye % 100 != 0) || ye % 400 == 0){
for(int i = 0; i < sizeof(month2a) / sizeof(0); i++){
cout << month2a[i];
if(((day1 + month2a[i]) % 7) == 0)
cout << "\n";
else
cout << "\t";
}
Feb = sizeof(month2a) / sizeof(0);
}
else
{
for(int i = 0; i < sizeof(month2b) / sizeof(0); i++){
cout << month2b[i];
if(((day + Jan + month2b[i]) % 7) == 0)
cout << "\n";
else
cout << "\t";
}
Feb = sizeof(month2b) / sizeof(0);
}
//三月
int day2 = day + Jan + Feb;
if((day2 % 7) != 0)
cout << "\n";
cout << "三月" << endl;
Week(day2);
output(day2, month1);
//四月
int day3 = day + Jan * 2 + Feb;
if((day3 % 7) != 0)
cout << "\n";
cout << "四月" << endl;
Week(day3);
output2(day3, month4);
//五月
int day4 = day + Jan * 2 + Feb + Apr;
if((day4 % 7) != 0)
cout << "\n";
cout << "五月" << endl;
Week(day4);
output(day4, month1);
//六月
int day5 = day + Jan * 3 + Feb + Apr;
if((day5 % 7) != 0)
cout << "\n";
cout << "六月" << endl;
Week(day5);
output2(day5, month4);
//七月
int day6 = day + Jan * 3 + Feb + Apr * 2;
if((day6 % 7) != 0)
cout << "\n";
cout << "七月" << endl;
Week(day6);
output(day6, month1);
//八月
int day7 = day + Jan * 4 + Feb + Apr * 2;
if((day7 % 7) != 0)
cout << "\n";
cout << "八月" << endl;
Week(day7);
output(day7, month1);
//九月
int day8 = day + Jan * 5 + Feb + Apr * 2;
if((day8 % 7) != 0)
cout << "\n";
cout << "九月" << endl;
Week(day8);
output2(day8, month4);
//十月
int day9 = day + Jan * 5 + Feb + Apr * 3;
if((day9 % 7) != 0)
cout << "\n";
cout << "十月" << endl;
Week(day9);
output(day9, month1);
//十一月
int day10 = day + Jan * 6 + Feb + Apr * 3;
if((day10 % 7) != 0)
cout << "\n";
cout << "十一月" << endl;
Week(day10);
output2(day10, month4);
//十二月
int day11 = day + Jan * 6 + Feb + Apr * 4;
if((day11 % 7) != 0)
cout << "\n";
cout << "十二月" << endl;
Week(day11);
output(day11, month1);
}
int Week(int day)
{
//这一年 1 月 1 日是星期几
int week = (day % 7) +1;
cout << "星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日" << endl;
for(int i = 0; i < week -1; i++){
cout << "\t";
}//打印第一天前面的空格
}
int output(int da, int month[])
{
for(int i = 0; i < 31; i++){
cout << month[i];
if(((da + month[i]) % 7) == 0)
cout << "\n";
else
cout << "\t";
}
}
int output2(int da, int month[])
{
for(int i = 0; i < 30; i++){
cout << month[i];
if(((da + month[i]) % 7) == 0)
cout << "\n";
else
cout << "\t";
}
}

猜你喜欢

转载自blog.csdn.net/Hello_Rainy/article/details/80001380