//打印日历实现输出某年某月 #include<iostream> #include<iomanip> #include<string> using namespace std; //判断平闰年 int doyear(int year) { if((year%4==0)&&(year%100!=0)||(year%400==0)) return 1; else return 0; } //判断某年的第一天是星期几 int doweek(int year) { int days=(year-1)*365+(year-1)/4-(year-100)/100+(year-1)/400; return days%7; } //打印星期 void printweek() { int i;//计数 string week[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; for(i=0;i<7;i++) cout<<setw(5)<<week[i]; cout<<endl; } //打印月份 void printmonth(int m) { //int i; string month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dev"}; //for(i=0;i<12;i++) //if(m=i+1) cout<<setw(5)<<month[m-1]; cout<<endl; } //打印日历 void printall(int year,int month) { int i,d; int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if(doyear(year)) monthday[1]=29; int sum=0; for(i=0;i<month-1;i++) sum+=monthday[i]; int weekday=((doweek(year)+sum)%7); cout<<setw(5)<<year<<" "<<month<<"月"<<endl<<endl; cout<<setw(5)<<"Calendar"; cout<<setw(20)<<year<<"-"<<month<<endl; for(i=1;i<=5*7;i++) cout<<"*"; cout<<endl; printmonth(month); printweek(); for(d=1;d<=monthday[month-1];d++) { if(d==1){ for(i=1;i<=weekday;i++) cout<<setw(5)<<" "; cout<<setw(5)<<d; } else cout<<setw(5)<<d; if((weekday+d-1)%7==6) cout<<endl; } } //主函数 int main() { int year,month; cout<<"please input which year do you want"<<endl; cin>>year; cout<<"please input which month do you want"<<endl; cin>>month; printall(year,month); cout<<endl; return 0; }
时间: 2024-10-17 13:16:22