C++实现简易万年历的方法-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

C++实现简易万年历的方法

这篇文章主要介绍了C++实现简易万年历的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联公司制作网站网页找三站合一网站制作公司,专注于网页设计,成都网站设计、成都网站建设、外贸网站建设,网站设计,企业网站搭建,网站开发,建网站业务,680元做网站,已为上千多家服务,创新互联公司网站建设将一如既往的为我们的客户提供最优质的网站建设、网络营销推广服务!

具体内容如下

代码如下:

/*
*文件名称:万年历.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<<"\n是否继续查询,是(yes)否(no)\n";
   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<year = year;
    this->month = month;
    this->day = day;
}

//显示星期数
void Date::Disp_Date(){    
    cout << year << "年" << month << "月" << day << "日 星期" ;
    switch(this->week()){
     case 0:
     cout<<"日\n";
     break;
     case 1:
     cout<<"一\n";
     break;
     case 2:
     cout<<"二\n";
     break;
     case 3:
     cout<<"三\n";
     break;
     case 4:
     cout<<"四\n";
     break;
     case 5:
     cout<<"五\n";
     break;
     case 6:
     cout<<"六\n";
     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<<"---------------------------------------------------"<year<<"年"<month<<"月"<<"---------------------\n"; 
 cout<<"日 一 二 三 四 五 六\n";
 for(int i=0;iyear)){
  for(int j=1;j<30;j++){
  if(j<10)cout<<" "<

运行结果:

C++实现简易万年历的方法

感谢你能够认真阅读完这篇文章,希望小编分享的“C++实现简易万年历的方法”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


当前名称:C++实现简易万年历的方法
转载来源:http://kswsj.cn/article/jchspc.html

其他资讯