怎么用C++实现简易万年历

发布时间:2021-09-14 23:04:35 作者:chen
来源:亿速云 阅读:130

本篇内容主要讲解“怎么用C++实现简易万年历”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用C++实现简易万年历”吧!

代码如下:

/**文件名称:万年历.cpp*作  者:chenghan*完成日期:2019/1/10*版 本 号:1.0*问题描述:制作一个简单的万年历 */ #include<iostream>#include <string>using namespace std;//判断一年是否为闰年,是返回true 否返回falsebool 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 否返回falsebool isleapyear(int year){  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)    return true;  else    return false;}//兔子图案void Rabbit()  { cout<<endl;  cout<<"         ┏━┓ ┏━┓"<<endl;  cout<<"作者:chenghan  ★│┃ ┃│┃"<<endl;  cout<<"         ┃│┗灬┛│┃"<<endl;  cout<<"版本:1.0     ┃     ┃"<<endl;  cout<<"         ┃ ^   ^ ┃"<<endl;  cout<<"时间:2019/1/10   ﹌ ˇ ﹌  "<<endl;  cout<<"         ┗○━━━○┛"<<endl;    cout<<"---------------------------------\n";    cout<<"欢 来 万 历"<<endl;  cout<<" 迎 到 年 !"<<endl;     cout<<"---------------------------------------------------\n";}//有参的构造函数 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<<"日\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<<"---------------------------------------------------"<<endl;  //粗制滥造的界面  cout<<"---------------------"<<this->year<<"年"<<this->month<<"月"<<"---------------------\n";  cout<<"日 一 二 三 四 五 六\n"; for(int i=0;i<count;i++){ cout<<" \t"; } 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<<" "<<j<<"\t";  else cout<<j<<"\t";  if(count==6){  cout<<"\n";  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<<" "<<j<<"\t";  else cout<<j<<"\t";  if(count==6){  cout<<"\n";  count = 0;  }  else count++; } } else if(temp.month==2){ if(isleapyear(this->year)){  for(int j=1;j<30;j++){  if(j<10)cout<<" "<<j<<"\t";  else cout<<j<<"\t";  if(count==6){   cout<<"\n";   count = 0;  }  else count++;  } } else{  for(int j=1;j<29;j++){  if(j<10)cout<<" "<<j<<"\t";  else cout<<j<<"\t";  if(count==6){   cout<<"\n";   count = 0;  }  else count++;  } }  } cout<<"\n---------------------------------------------------";}

到此,相信大家对“怎么用C++实现简易万年历”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. C++实现万年历小功能的方法
  2. C++实现万年历的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:spring类的静态属性实现注入的实例方法

下一篇:Java8中Comparator排序方法的实例使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》