因为我这是做一个测试时的代码,那个测试是分步完成的,也就是说,判断闰年,输入日期判断星期几,打印日历本事好几个题,所以下面代码思路也是这样,显得更长,当然更多是因为自己的菜。。。
#include <iostream>
using namespace std;
int leapYear(int y)
{
if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
return 1;
return 0;
}//判断闰年
//构造判断1号是星期几的函数
int whatDay(int year, int month)
{
int n,s=0,d=1;
for(int i=1;i<year;i++){
if(leapYear(i))
s+=366;
else
s+=365;
}
if(leapYear(year)){
switch(month){
case 1:
n=d;
break;
case 2:
n=31+d;
break;
case 3:
n=60+d;
break;
case 4:
n=91+d;
break;
case 5:
n=121+d;
break;
case 6:
n=152+d;
break;
case 7:
n=182+d;
break;
case 8:
n=213+d;
break;
case 9:
n=244+d;
break;
case 10:
n=274+d;
break;
case 11:
n=305+d;
break;
case 12:
n=335+d;
break;
}
}
else{
switch(month){
case 1:
n=d;
break;
case 2:
n=31+d;
break;
case 3:
n=60+d-1;
break;
case 4:
n=91+d-1;
break;
case 5:
n=121+d-1;
break;
case 6:
n=152+d-1;
break;
case 7:
n=182+d-1;
break;
case 8:
n=213+d-1;
break;
case 9:
n=244+d-1;
break;
case 10:
n=274+d-1;
break;
case 11:
n=305+d-1;
break;
case 12:
n=335+d-1;
break;
}
}
s+=n;
if(s%7==0)
return 7;
else
return s%7;
}
int main()
{
int month,year,en,st=1;
cin>>year>>month;
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
en=31;
else if(month==4 || month==6 || month==9 || month==11)
en=30;
else if(month==2 && leapYear(year))
en=29;
else
en=28;
int m=en;
//cout<<whatDay(year,month)<<endl;
cout<<" "<<"一";
cout<<" "<<"二";
cout<<" "<<"三";
cout<<" "<<"四";
cout<<" "<<"五";
cout<<" "<<"六";
cout<<" "<<"日"<<endl;
switch(whatDay(year,month)){//7日分别处理
case 1:
for(int i=1;i<=m/7+1;i++ && st+(i-1)*7<=en && st+(i-1)*7+1<=en && st+(i-1)*7+2<=en && st+(i-1)*7+3<=en && st+(i-1)*7+4<=en && st+(i-1)*7+5<=en && st+(i-1)*7+6<=en){
if(i<m/7+1)
printf("%4d%4d%4d%4d%4d%4d%4d\n",st+(i-1)*7,st+(i-1)*7+1,st+(i-1)*7+2,st+(i-1)*7+3,st+(i-1)*7+4,st+(i-1)*7+5,st+(i-1)*7+6);
else{
if(en==31)
cout<<" 29 30 31"<<endl;
if(en==30)
cout<<" 29 30"<<endl;
if(en==29)
cout<<" 29"<<endl;//日历最后一行单独讨论处理
}
}
break;
case 2:
for(int i=1;i<=m/7+1;i++ && st+(i-1)*7-1<=en && st+(i-1)*7+1-1<=en && st+(i-1)*7+2-1<=en && st+(i-1)*7+3-1<=en && st+(i-1)*7+4-1<=en && st+(i-1)*7+5-1<=en && st+(i-1)*7+6-1<=en){
if(i==1)
printf(" %4d%4d%4d%4d%4d%4d\n",st+(i-1)*7,st+(i-1)*7+1,st+(i-1)*7+2,st+(i-1)*7+3,st+(i-1)*7+4,st+(i-1)*7+5);
else if(i>1 &&i<=m/7)
printf("%4d%4d%4d%4d%4d%4d%4d\n",st+(i-1)*7-1,st+(i-1)*7+1-1,st+(i-1)*7+2-1,st+(i-1)*7+3-1,st+(i-1)*7+4-1,st+(i-1)*7+5-1,st+(i-1)*7+6-1);
else{
if(en==31)
cout<<" 28 29 30 31"<<endl;
else if(en==30)
cout<<" 28 29 30"<<endl;
else if(en==29)
cout<<" 28 29"<<endl;
else
cout<<" 28"<<endl;
}
}
break;
case 3:
for(int i=1;i<=m/7+1;i++ && st+(i-1)*7-2<=en && st+(i-1)*7+1-2<=en && st+(i-1)*7+2-2<=en && st+(i-1)*7+3-2<=en && st+(i-1)*7+4-2<=en && st+(i-1)*7+5-2<=en && st+(i-1)*7+6-2<=en){
if(i==1)
printf(" %4d%4d%4d%4d%4d\n",st+(i-1)*7,st+(i-1)*7+1,st+(i-1)*7+2,st+(i-1)*7+3,st+(i-1)*7+4);
else if(i>1 && i<=m/7)
printf("%4d%4d%4d%4d%4d%4d%4d\n",st+(i-1)*7-2,st+(i-1)*7+1-2,st+(i-1)*7+2-2,st+(i-1)*7+3-2,st+(i-1)*7+4-2,st+(i-1)*7+5-2,st+(i-1)*7+6-2);
else{
if(en==31)
cout<<" 27 28 29 30 31"<<endl;
else if(en==30)
cout<<" 27 28 29 30"<<endl;
else if(en==29)
cout<<" 27 28 29"<<endl;
else
cout<<" 27 28"<<endl;
}
}
break;
case 4:
for(int i=1;i<=m/7+1;i++ && st+(i-1)*7-3<=en && st+(i-1)*7+1-3<=en && st+(i-1)*7+2-3<=en && st+(i-1)*7+3-3<=en && st+(i-1)*7+4-3<=en && st+(i-1)*7+5-3<=en && st+(i-1)*7+6-3<=en){
if(i==1)
printf(" %4d%4d%4d%4d\n",st+(i-1)*7,st+(i-1)*7+1,st+(i-1)*7+2,st+(i-1)*7+3);
else if(i>1 && i<=m/7)
printf("%4d%4d%4d%4d%4d%4d%4d\n",st+(i-1)*7-3,st+(i-1)*7+1-3,st+(i-1)*7+2-3,st+(i-1)*7+3-3,st+(i-1)*7+4-3,st+(i-1)*7+5-3,st+(i-1)*7+6-3);
else{
if(en==31)
cout<<" 26 27 28 29 30 31"<<endl;
else if(en==30)
cout<<" 26 27 28 29 30"<<endl;
else if(en==29)
cout<<" 26 27 28 29"<<endl;
else
cout<<" 26 27 28"<<endl;
}
}
break;
case 5:
for(int i=1;i<=m/7+1;i++ && st+(i-1)*7-4<=en && st+(i-1)*7+1-4<=en && st+(i-1)*7+2-4<=en && st+(i-1)*7+3-4<=en && st+(i-1)*7+4-4<=en && st+(i-1)*7+5-4<=en && st+(i-1)*7+6-4<=en){
if(i==1)
printf(" %4d%4d%4d\n",st+(i-1)*7,st+(i-1)*7+1,st+(i-1)*7+2);
else if(i>1 && i<=m/7)
printf("%4d%4d%4d%4d%4d%4d%4d\n",st+(i-1)*7-4,st+(i-1)*7+1-4,st+(i-1)*7+2-4,st+(i-1)*7+3-4,st+(i-1)*7+4-4,st+(i-1)*7+5-4,st+(i-1)*7+6-4);
else{
if(en==31)
cout<<" 25 26 27 28 29 30 31"<<endl;
else if(en==30)
cout<<" 25 26 27 28 29 30"<<endl;
else if(en==29)
cout<<" 25 26 27 28 29"<<endl;
else
cout<<" 25 26 27 28"<<endl;
}
}
break;
case 6:
for(int i=1;i<=m/7+1;i++ && st+(i-1)*7-5<=en && st+(i-1)*7+1-5<=en && st+(i-1)*7+2-5<=en && st+(i-1)*7+3-5<=en && st+(i-1)*7+4-5<=en && st+(i-1)*7+5-5<=en && st+(i-1)*7+6-5<=en){
if(i==1)
printf(" %4d%4d\n",st+(i-1)*7,st+(i-1)*7+1);
else if(i>1 && i<=m/7)
printf("%4d%4d%4d%4d%4d%4d%4d\n",st+(i-1)*7-5,st+(i-1)*7+1-5,st+(i-1)*7+2-5,st+(i-1)*7+3-5,st+(i-1)*7+4-5,st+(i-1)*7+5-5,st+(i-1)*7+6-5);
else{
if(en==31)
cout<<" 31"<<endl;
else if(en==30)
cout<<" 24 25 26 27 28 29 30"<<endl;
else if(en==29)
cout<<" 24 25 26 27 28 29"<<endl;
else
cout<<" 24 25 26 27 28"<<endl;
}
}
break;
case 7:
for(int i=1;i<=m/7;i++ && st+(i-1)*7-6<=en && st+(i-1)*7+1-6<=en && st+(i-1)*7+2-6<=en && st+(i-1)*7+3-6<=en && st+(i-1)*7+4-6<=en && st+(i-1)*7+5-6<=en && st+(i-1)*7+6-6<=en){
if(i==1)
printf(" %4d\n",st+(i-1)*7);
else if(i>1 && i<=m/7)
printf("%4d%4d%4d%4d%4d%4d%4d\n",st+(i-1)*7-6,st+(i-1)*7+1-6,st+(i-1)*7+2-6,st+(i-1)*7+3-6,st+(i-1)*7+4-6,st+(i-1)*7+5-6,st+(i-1)*7+6-6);
else{
if(en==31)
cout<<" 30 31"<<endl;
else if(en==30)
cout<<" 31"<<endl;
else if(en==29)
cout<<" 23 24 25 26 27 28 29"<<endl;
else
cout<<" 22 24 25 26 27 28"<<endl;
}
}
break;
}
return 0;
}
//判断1号是星期几的测试函数
/*
int main()
{
int y, m, xq; // 年、月、星期几
cin >> y >> m; // 输入年月
xq = whatDay(y,m); // 计算星期几
cout << y << "年" << m << "月1日是星期"; // 输出星期
if(xq == 7)
cout << "日" << endl;
else
cout << xq << endl;
return 0;
} */
又臭又长,纪念一下,希望自己的能力尽快提升!!!
输入输出:
#######################################################
为什么你的代码又臭又长?
因为你对数组不熟悉!!
下面是参照一个好朋友的写法:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int year,month,flag,sum=0,weekday;
int a[2][12]={{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};
//利用数组存储数据
cin>>year>>month;
if(year%4==0 && year%100!=0 || year%400==0)
flag=0;
else
flag=1;
for(int i=1;i<year;i++){
if(i%4==0 && i%100!=0 || i%400==0)
sum+=366;
else
sum+=365;
}
if(month>2){
for(int i=0;i<month-1;i++)
sum+=a[flag][i];
}
else if(month==2)
sum+=31;
else
sum+=0;
weekday=(sum+1)%7;//至此找到该月1号是星期几
if(weekday==0)
weekday=7;
cout<<" 一 二 三 四 五 六 日"<<endl;
for(int k=1;k<weekday;k++){
cout<<" ";
}
int n=0;
for(int i=1;i<=a[flag][month-1];i++){
printf("%4d",i);
if((weekday+n)==7){
cout<<endl;
weekday=0;
n=0;
}
n++;
}
return 0;
}