c语言 万年历 蔡勒公式,C++实现简易万年历

本文实例为大家分享了C++实现简易的万年历,供大家参考,具体内容如下

代码如下:

/*

*文件名称:万年历.cpp

*作 者:chenghan

*完成日期:2019/1/10

*版 本 号:1.0

*问题描述:制作一个简单的万年历

*/

#include

#include

using namespace std;

//判断一年是否为闰年,是返回true 否返回false

bool isleapyear(int year);

//兔子图案

void Rabbit();

//封装时间类 私有数据成员包括年月日

class Date

{

private:

int year, month, day; //私有数据成员

public:

Date(){} //无参的构造函数

Date(int year, int month, int day); //有参的构造函数

void Disp_Date(); //显示星期数

void set(); //用户输入时间

int week(); //判断星期的函数

void show(); //显示日历的函数

};

//主函数

int main()

{

Date t; //创建一个Date类对象

string N="yes";

Rabbit();

while(N=="yes"){

t.set(); //调用设置时间函数

t.Disp_Date(); //显示星期

t.show(); //展示日历画面

cout<

cin>>N;

}

return 0;

}

//判断一年是否为闰年,是返回true 否返回false

bool isleapyear(int year)

{

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

return true;

else

return false;

}

//兔子图案

void Rabbit()

{

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

}

//有参的构造函数

Date::Date(int year, int month, int day) //有参的构造函数

{

this->year = year;

this->month = month;

this->day = day;

}

//显示星期数

void Date::Disp_Date(){

cout << year << "年" << month << "月" << day << "日 星期" ;

switch(this->week()){

case 0:

cout<

break;

case 1:

cout<

break;

case 2:

cout<

break;

case 3:

cout<

break;

case 4:

cout<

break;

case 5:

cout<

break;

case 6:

cout<

break;

}

}

//用户设置时间

void Date::set()

{

cout<

cin>>year>>month>>day;

}

//判断星期的函数

int Date::week(){

int C,y,d,M;

if(this->month==1||this->month==2){

C = (this->year-1)/100;

y = (this->year-1)%100;

M = this->month+12;

d = this->day;

}

else{

C = this->year/100; //C世纪数减一

y = this->year%100; //y年份后两位

d = this->day; //d是日

M = this->month;

}

int W = C/4 - 2*C + y + y/4 + 13 * (M+1) / 5 + d - 1; //判断星期的蔡勒公式

if (W < 0) /* 如果w是负数,则计算余数方式不同 */

{

W = 7 - (-W) % 7;

return W; //返回值1~6对应星期一到六 0对应七

}

else return W%7;

}

//显示日历的函数

void Date::show(){

Date temp;

temp.year = this->year;

temp.month = this->month;

temp.day = 1;

int count = temp.week();

cout<

cout<year<month<

cout<

for(int i=0;i

cout<

}

if(temp.month == 1 ||temp.month == 3 ||temp.month == 5 || temp.month ==7 || temp.month ==8 || temp.month ==10 ||temp.month == 12){

for(int j=1;j<32;j++){

if(j<10)cout<

else cout<

if(count==6){

cout<

count = 0;

}

else count++;

}

}

else if(temp.month == 4 ||temp.month == 6 ||temp.month == 9 ||temp.month == 11){

for(int j=1;j<31;j++){

if(j<10)cout<

else cout<

if(count==6){

cout<

count = 0;

}

else count++;

}

}

else if(temp.month==2){

if(isleapyear(this->year)){

for(int j=1;j<30;j++){

if(j<10)cout<

else cout<

if(count==6){

cout<

count = 0;

}

else count++;

}

}

else{

for(int j=1;j<29;j++){

if(j<10)cout<

else cout<

if(count==6){

cout<

count = 0;

}

else count++;

}

}

}

cout<

}

运行结果:

c7c6f795a16106aba3cb1e8eac68fad6.png

代码中没有检查输入错误的机制,写的比较粗糙,有许多错误之处望指正。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值