304 lines
9.5 KiB
C++
304 lines
9.5 KiB
C++
//designed by yyw
|
|
//
|
|
#include <iostream>
|
|
#include<iomanip>
|
|
//setfill() 填充
|
|
//setw() 宽
|
|
//setprecision()显示数字位数
|
|
|
|
using namespace std;
|
|
|
|
class calendar
|
|
{
|
|
public:
|
|
void Menu_present()
|
|
{
|
|
cout<<"This is a calendar"<<endl;
|
|
cout<<endl;
|
|
cout<<" Menu"<<endl;
|
|
cout<<"1.Search the date is which day of the week"<<endl;
|
|
cout<<"2.Check the calendar for a certain year"<<endl;
|
|
cout<<endl;
|
|
cout<<"Exit: input 88 with \"ENTER\""<<endl;
|
|
}// 菜单显示
|
|
|
|
void open_calendar()// 主菜单
|
|
{
|
|
// Menu_present();
|
|
int menu_number=0;
|
|
while(menu_number!=88)
|
|
{
|
|
Menu_present();
|
|
cout<<"Please input the number before the option "<<endl;
|
|
while(1) {
|
|
cin >> menu_number;
|
|
if (!cin.fail())break;
|
|
if (cin.fail()) {
|
|
cin.clear();
|
|
cin.sync();
|
|
cout << "Your input is illegal" << endl;
|
|
cout << "please input the number correctly" << endl;
|
|
cout << "other char is not allowed" << endl;
|
|
continue;
|
|
}
|
|
}
|
|
switch(menu_number)
|
|
{
|
|
case 1: M_Search();
|
|
break;
|
|
case 2: Check();
|
|
break;
|
|
// case 3:
|
|
// break;
|
|
case 88:
|
|
cout<<"Good bye,have a good day!"<<endl;
|
|
break;
|
|
default:
|
|
cout<<"Invalid input"<<endl;
|
|
cout<<"please input the number correctly,and input it again"<<endl;
|
|
}// case 2
|
|
}
|
|
|
|
}
|
|
void M_Search()
|
|
{
|
|
int i=1,j=1,k=1;
|
|
int year;
|
|
int month;
|
|
int day;
|
|
cout<<"Please input the year "<<endl;
|
|
while(1) {
|
|
cin >> year;
|
|
// if(year<0){
|
|
// //cin.clear();
|
|
// cin.sync();
|
|
// cout<<"year wrong"<<endl;
|
|
// continue;
|
|
// }
|
|
if(!cin.fail()){
|
|
if(year<0){
|
|
cin.clear();
|
|
cin.sync();
|
|
cout<<"year wrong"<<endl;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
if (cin.fail()) {
|
|
cin.clear();
|
|
cin.sync();
|
|
cout << "Your input is illegal" << endl;
|
|
cout << "please input the number correctly" << endl;
|
|
cout << "other char is not allowed" << endl;
|
|
continue;
|
|
}
|
|
//if(year<1){cin.clear();cin.sync();cout<<"year wrong"<<endl;continue;}
|
|
}
|
|
cout<<"Please input the month "<<endl;
|
|
while(1) {
|
|
cin >> month;
|
|
if(!cin.fail()){
|
|
if(month<1||month>12){
|
|
cin.clear();
|
|
cin.sync();
|
|
cout<<"month wrong"<<endl;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
if (cin.fail()) {
|
|
cin.clear();
|
|
cin.sync();
|
|
cout << "Your input is illegal" << endl;
|
|
cout << "please input the number correctly" << endl;
|
|
cout << "other char is not allowed" << endl;
|
|
continue;
|
|
}
|
|
}
|
|
cout<<"Please input the day "<<endl;
|
|
while(1) {
|
|
cin >> day;
|
|
// if(day<1||day> month_judge(year,month)){cin.clear();cin.sync();cout<<"day wrong"<<endl;continue;}
|
|
if(!cin.fail()){
|
|
if(day<1||day> month_judge(year,month)){cin.clear();cin.sync();cout<<"day wrong"<<endl;continue;}
|
|
break;
|
|
}
|
|
if (cin.fail()) {
|
|
cin.clear();
|
|
cin.sync();
|
|
cout << "Your input is illegal" << endl;
|
|
cout << "please input the number correctly" << endl;
|
|
cout << "other char is not allowed" << endl;
|
|
continue;
|
|
}
|
|
}
|
|
if(year==1582&&month==10&&(day>4&&day<15))cout<<"This is none day"<<endl;
|
|
else cout<<day_of_week (year,month,day)<<endl;
|
|
if(year_judge(year))cout<<"This year is Leap year"<<endl;
|
|
else cout<<"This year is Non-leap year"<<endl;
|
|
|
|
}// 查某一天是星期几 写完了
|
|
void Check()
|
|
{
|
|
int i=1,j=1;
|
|
int year;
|
|
cout<<"Please input the year "<<endl;
|
|
while(1) {
|
|
cin >> year;
|
|
if(!cin.fail()){
|
|
if(year<0){
|
|
cin.clear();
|
|
cin.sync();
|
|
cout<<"year wrong"<<endl;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
if (cin.fail()) {
|
|
cin.clear();
|
|
cin.sync();
|
|
cout << "Your input is illegal" << endl;
|
|
cout << "please input the number correctly" << endl;
|
|
cout << "other char is not allowed" << endl;
|
|
continue;
|
|
}
|
|
}//cin year
|
|
int c;
|
|
aaa:
|
|
cout<<"Please select the way you want view"<<endl;
|
|
cout<<"1.send out every month with 1*12"<<endl;
|
|
cout<<"2.send out every month with 2*6"<<endl;
|
|
cout<<"Input the number before the option"<<endl;
|
|
while(1) {
|
|
cin >> c;
|
|
if(!cin.fail())break;
|
|
if (cin.fail()) {
|
|
cin.clear();
|
|
cin.sync();
|
|
cout << "Your input is illegal" << endl;
|
|
cout << "please input the number correctly" << endl;
|
|
cout << "other char is not allowed" << endl;
|
|
continue;
|
|
}
|
|
}//cin 选择
|
|
switch(c)
|
|
{
|
|
case 1:
|
|
send1_12(year);
|
|
break;
|
|
case 2:
|
|
send2_6(year);
|
|
break;
|
|
default:
|
|
cout<<"Invalid input"<<endl;
|
|
cout<<"Please Input the number before the option correctly"<<endl;
|
|
goto aaa;
|
|
break;
|
|
}
|
|
}// 输出那一年的日期
|
|
private:
|
|
int year_judge(int year)
|
|
{
|
|
|
|
if(year%4==0&&year%100!=0||year%400==0)
|
|
{
|
|
return 1;
|
|
}
|
|
return 0;
|
|
|
|
}// 1润 0平
|
|
int month_judge(int year,int month){
|
|
if(!(year==1582&&month==10)) {
|
|
if (month == 2) {
|
|
if (year_judge(year) == 1) {
|
|
return 29;
|
|
} else return 28;
|
|
}
|
|
if (month == 4 || month == 6 || month == 9 || month == 11) {
|
|
return 30;
|
|
} else {
|
|
return 31;
|
|
}
|
|
}else{return 31;}
|
|
}// 返回多少天
|
|
int day_of_week(int year,int month,int day){
|
|
if(month<3)month+=12,year-=1;
|
|
int f;
|
|
if(year>1582||((year==1582&&month>10)||(year==1582&&month==10&&day>4)))//geli
|
|
{
|
|
f=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
|
|
if(f==0)f=7;
|
|
return f;
|
|
}else//ruli
|
|
{
|
|
f=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+4)%7;
|
|
if(f==0)f=7;
|
|
return f;
|
|
}
|
|
}// 返回星期几
|
|
void send1_12(int year){
|
|
|
|
for(int i=1;i<=12;i++)// 12个月
|
|
{
|
|
|
|
cout<<setw(20)<<"month "<<i<<endl;
|
|
cout<<"Sun Mon Tue Wed Thu Fri Sat "<<endl;
|
|
int k1=1-day_of_week(year,i,1);
|
|
for(int r=1;r<=6;r++){
|
|
for(int j=1;j<=7;j++,k1++){
|
|
if(k1<=0){
|
|
cout<<" ";
|
|
}else{
|
|
if(k1<= month_judge(year,i)) {
|
|
if(year==1582&&i==10&&k1==5)k1+=10;
|
|
cout << setw(3) << k1 << " ";
|
|
}else{
|
|
cout<<" ";
|
|
}
|
|
}
|
|
}
|
|
cout<<endl;
|
|
}
|
|
}
|
|
};//完成
|
|
void send2_6(int year){
|
|
cout<<setw(33)<<"Happy "<<year<<'!'<<endl;
|
|
for (int i = 1; i <= 12; i += 2)// 12个月
|
|
{
|
|
|
|
cout << setw(20) << "month " << i << setw(35) << "month" << i + 1 << endl;
|
|
cout << "Sun Mon Tue Wed Thu Fri Sat " << "Sun Mon Tue Wed Thu Fri Sat " << endl;
|
|
|
|
int k1 = 1 - day_of_week(year, i, 1), k2 = 1 - day_of_week(year, i + 1, 1);
|
|
|
|
for (int j = 1; j <= 6; j++) {
|
|
for (int r = 1; r <= 7; r++, k1++) {
|
|
if (k1 <= 0)cout << " ";
|
|
else {
|
|
if (k1 <= month_judge(year, i)) {
|
|
cout << setw(3) << k1 << " ";
|
|
} else cout << " ";
|
|
}
|
|
}
|
|
cout<<" ";
|
|
|
|
for (int r = 1; r <= 7; r++, k2++) {
|
|
if (k2 <= 0)cout << " ";
|
|
else {
|
|
if (k2 <= month_judge(year, i + 1)) {
|
|
if(year==1582&&(i+1)==10&&k2==5)k2+=10;
|
|
cout << setw(3) << k2 << " ";
|
|
} else cout << " ";
|
|
}
|
|
}
|
|
cout << endl;
|
|
}
|
|
}
|
|
};// 完成
|
|
};
|
|
int main() {
|
|
class calendar r;
|
|
r.open_calendar();
|
|
return 0;
|
|
}
|