万年历求修改!!
/**************************************/程序实现的是输入一个年份,输出该年的日历。
本程序存在的问题:开头第一行日期总是无法控制宽度,对位不齐,后面都对的很齐。遗憾无法上传图片,请大虾帮帮运行改改。谢谢了!
#include<iostream>
#include<iomanip>
using namespace std;
int year,weekday;
int firstday(int y)
{
double s;
s=year-1+(year-1)/4.0-(year-1)/100.0+(year-1)/400.0+1;
return (s/7);
}
int month(int m)
{
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:
if((year%4==0 && year%100!=0)||(year%400==0))
return 29;
else
return 28;
default:return 0;
}
}
void printhead(int m)
{
int i;
cout<<m<<"月"<<"\n"<<" 日 一 二 三 四 五 六 "<<"\n";
for(i=1;i<=weekday;i++)
cout<<" ";
}
void printmonth(int m)
{
int i,days;
printhead(m);
days=month(m);
for(i=1;i<=days;i++)
{
cout<<" "<<setw(2)<<i;
weekday=(weekday+1)%7;
if(weekday==0)
cout<<"\n";
}
}
void main()
{
int i;
cout<<"请输入年份:";
cin>>year;
weekday=firstday(year);
cout<<year<<"年"<<"\n";
for(i=1;i<=12;i++)
{
printmonth(i);
cout<<"\n";
}
cout<<"\n";
}