c++刘军老师课上布置的作业
感觉非常有意思
自己一行一行敲出来Debug 还是很有成就感得
话不多说上图上代码
#include <iostream>
#include <iomanip>
using namespace std;
int un_leap_year[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //平年月份日
int leap_year[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31}; //闰年月份日
string chweek[7] = {"一","二","三","四","五","六","日"}; //周一到周日
bool is_leap_year(int year) //闰年判定
{
return (year % 4 == 0 && year % 100 == 0 || year % 400 == 0);
}
int cal_week(int year, int month, int day,int mode) //计算星期几
{
if(month == 1 || month == 2)
{
month += 12;
year --;
} //基姆拉尔森计算公式
int ans = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
if(mode == 1) //模式1输出星期几
cout<< chweek[ans] << endl << endl; //非模式1只返回计算值
return ans;
}
void put_month(int year, int month) //输出月份
{
int flag = 0,day = 1;
while(flag < 7) //输出周一到周日
{
cout<<setw(6)<<chweek[flag];
flag ++;
}
cout<<endl<<" "<<year<<'.'<<month<<".1 星期";
flag = cal_week(year, month, 1, 1);
cout<<"--------------------------------------------"<<endl;
while(flag --)
cout<<" "; //补齐每月不在的星期位置
flag = is_leap_year(year)?leap_year[month]:un_leap_year[month];
while(day <= flag)
{
cout<<setw(6)<<day; //输出日期
if(cal_week(year, month, day, 0) == 6) //周日换行
cout<<endl;
day ++;
}
cout<< endl <<"--------------------------------------------"<< endl << endl;
}
void put_year(int year)
{
int month = 1;
while(month < 13)
{
put_month(year, month); //重复调用月输出函数
month ++;
}
}
void put_welcome(int flag)
{
if(flag == 0) //仅首次输出欢迎界面
{
cout<<endl<<endl; //以后只输出查询界面
cout<<" ------------------------------------"<<endl;
cout<<" * *"<<endl;
cout<<" * 欢迎进入万年历查询系统 *"<<endl;
cout<<" * By 张峻溥 *"<<endl;
cout<<" * *"<<endl;
cout<<" ------------------------------------"<<endl<<endl;
}
cout<<"请选择您的查询内容;"<<endl<<endl;
cout<<" 1.显示一年的日历;"<<endl;
cout<<" 2.显示一月的日历;"<<endl;
cout<<" 3.显示某一天是星期几;"<<endl;
cout<<" 0.退出;"<<endl<<endl;
cout<<"请输入按键(0-3);" <<endl;
}
int main()
{
int year, month, day, ans,flag = 0;
while(1)
{
put_welcome(flag); //仅首次输出欢迎界面
flag = 1;
int mode;
cin>>mode;
if(mode == 1)
{
cout<<"请输入一个年份:"<<endl;
cin>>year;
put_year(year);
}
else if(mode == 2)
{
cout<<"请依次输入年份,月份:"<<endl;
cin>>year>>month;
put_month(year,month);
}
else if(mode == 3)
{
cout<<"请依次输入年份,月份,日份:"<<endl;
cin>>year>>month>>day;
cout<<endl<<endl<<" "<<year<<"年"<<month<<"月"<<day<<"日是:星期";
cal_week(year, month, day, 1);
}
else //非模式1,2,3退出
{
break;
}
}
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int un_leap_year[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //平年月份日
int leap_year[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31}; //闰年月份日
string chweek[7] = {"一","二","三","四","五","六","日"}; //周一到周日
bool is_leap_year(int year) //闰年判定
{
return (year % 4 == 0 && year % 100 == 0 || year % 400 == 0);
}
int cal_week(int year, int month, int day,int mode) //计算星期几
{
if(month == 1 || month == 2)
{
month += 12;
year --;
} //基姆拉尔森计算公式
int ans = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;
if(mode == 1) //模式1输出星期几
cout<< chweek[ans] << endl << endl; //非模式1只返回计算值
return ans;
}
void put_month(int year, int month) //输出月份
{
int flag = 0,day = 1;
while(flag < 7) //输出周一到周日
{
cout<<setw(6)<<chweek[flag];
flag ++;
}
cout<<endl<<" "<<year<<'.'<<month<<".1 星期";
flag = cal_week(year, month, 1, 1);
cout<<"--------------------------------------------"<<endl;
while(flag --)
cout<<" "; //补齐每月不在的星期位置
flag = is_leap_year(year)?leap_year[month]:un_leap_year[month];
while(day <= flag)
{
cout<<setw(6)<<day; //输出日期
if(cal_week(year, month, day, 0) == 6) //周日换行
cout<<endl;
day ++;
}
cout<< endl <<"--------------------------------------------"<< endl << endl;
}
void put_year(int year)
{
int month = 1;
while(month < 13)
{
put_month(year, month); //重复调用月输出函数
month ++;
}
}
void put_welcome(int flag)
{
if(flag == 0) //仅首次输出欢迎界面
{
cout<<endl<<endl; //以后只输出查询界面
cout<<" ------------------------------------"<<endl;
cout<<" * *"<<endl;
cout<<" * 欢迎进入万年历查询系统 *"<<endl;
cout<<" * By 张峻溥 *"<<endl;
cout<<" * *"<<endl;
cout<<" ------------------------------------"<<endl<<endl;
}
cout<<"请选择您的查询内容;"<<endl<<endl;
cout<<" 1.显示一年的日历;"<<endl;
cout<<" 2.显示一月的日历;"<<endl;
cout<<" 3.显示某一天是星期几;"<<endl;
cout<<" 0.退出;"<<endl<<endl;
cout<<"请输入按键(0-3);" <<endl;
}
int main()
{
int year, month, day, ans,flag = 0;
while(1)
{
put_welcome(flag); //仅首次输出欢迎界面
flag = 1;
int mode;
cin>>mode;
if(mode == 1)
{
cout<<"请输入一个年份:"<<endl;
cin>>year;
put_year(year);
}
else if(mode == 2)
{
cout<<"请依次输入年份,月份:"<<endl;
cin>>year>>month;
put_month(year,month);
}
else if(mode == 3)
{
cout<<"请依次输入年份,月份,日份:"<<endl;
cin>>year>>month>>day;
cout<<endl<<endl<<" "
cout<<year<<"年"<<month<<"月"<<day<<"日是:星期";
cal_week(year, month, day, 1);
}
else //非模式1,2,3退出
{
break;
}
}
return 0;
}
以上!