C++实现简易万年历

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

代码如下:

/*
*文件名称:万年历.cpp
*作  者:chenghan
*完成日期:2019/1/10
*版 本 号:1.0
*问题描述:制作一个简单的万年历
*/
#include<iostream>
#include <string>
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<<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---------------------------------------------------";
} 

运行结果:

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

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

(0)

相关推荐

  • C语言实现的一个万年历小程序

    该程序简单地输入一个年份(1901年之后的年份),随后程序输出该年份十二个月的日历. #include<stdio.h> #define Mon 1 #define Tues 2 #define Wed 3 #define Thur 4 #define Fri 5 #define Sat 6 #define Sun 0 #define January_days 31 #define February_days 28 #define March_days 31 #define April_day

  • C++实现万年历功能

    本文实例为大家分享了C++实现万年历的具体代码,供大家参考,具体内容如下 1.此万年历功能 1>日期加减天数 2>日期与日期之间的差值 3>输入年月显示当月日历 2.代码实现 #include<iostream> #include<iomanip> using namespace std; class Date { public: Date(int year = 1990, int month = 1, int day = 1) //构造函数 :_year(yea

  • C语言实现万年历

    C语言实现的万年历显示,按下上下左右按键来更换日期和年份,供大家参考,具体内容如下 #include <stdio.h> #include <stdlib.h> #include <time.h> #include<conio.h> typedef struct today { int day; int month; int year; } today; int day_cankao[2][13]={ {0,31,28,31,30,31,30,31,31,3

  • C++实现简易万年历

    本文实例为大家分享了C++实现简易的万年历,供大家参考,具体内容如下 代码如下: /* *文件名称:万年历.cpp *作 者:chenghan *完成日期:2019/1/10 *版 本 号:1.0 *问题描述:制作一个简单的万年历 */ #include<iostream> #include <string> using namespace std; //判断一年是否为闰年,是返回true 否返回false bool isleapyear(int year); //兔子图案 voi

  • C++实现万年历小功能

    本文实例为大家分享了C++实现万年历的具体代码,供大家参考,具体内容如下 用C++写了个简易的万年历. 具体功能如下: 1.打印指定年(用户输入)所有月份的年历 2.打印指定年指定月(用户输入)的月份 3.打印指定日期(用户输入)的星期数 4.可重复输入 贴上源码: #include<iostream> #include<windows.h> #include<iomanip> using namespace std; int number; //菜单键 int yea

  • Mysql 5.7.20压缩版下载和安装简易教程

    一.下载地址: http://dev.mysql.com/downloads/mysql/ http://www.jb51.net/softs/451120.html 1.进入官网下载,显示的应该是最新版本,选择第二个(mysql5.7.20-winx64.zip) 2.下载完成后,直接解压到自定义目录,解压目录就是安装目录 二.配置环境变量 1.新增环境变量,例: 变量名:MYSQL_HOME 变量值:D:\mysql\mysql5.7.20-winx64 2.修改环境变量PATH 在PATH

  • 详解JavaScript的Date对象(制作简易钟表)

    JS提供了Date类型来处理时间和日期.Date类型内置一系列获取和设置日期时间信息的方法.下面我们简单的 概述一下这个Date类型.        大概看了一下Date类型的方法,下面给出: 上面的方法自己尝试即可,我只简单的演示一下JS正确输出的格式: var today=new Date();//创建一个时间日期对象 document.write("<h4>下面的是世界标准的时间输出:</h4>"); document.write(today+"

  • JavaScript编写一个简易购物车功能

    网上关于购物车实现的代码非常多,今天看了一些知识点,决定自己动手写写,于是写了一个简易购物车,接下来讲解一下具体的实现. 1.用html实现内容: 2.用css修饰外观: 3.用js(jq)设计动效. 第一步:首先是进行html页面的设计,我用一个大的div将所有商品包含,然后用不同的div将不同的商品进行封装,商品列表中我用了ul li实现,具体实现代码如下(代码中涉及到的商品都是网上随便copy的,不具有参考价值): <div id="goods"> <div c

  • C语言数据结构之简易计算器

    本文实例为大家分享了C语言简易计算器的具体代码,供大家参考,具体内容如下 主要解决了处理负数.小数等的基础运算操作,无图形界面 #include <iostream> #include <stack> using namespace std; class Calculator{ private: int Priority(char fuhao); double CalSuffix(string PostfixExp); public: double Calculate(string

  • php实现的简易扫雷游戏实例

    本文实例讲述了php实现的简易扫雷游戏.分享给大家供大家参考.具体如下: <?php $init = $_POST["init"];//game restart $clickvalue = $_POST["clickvalue"];//minesweeping $checkflag = 0;//Victory or defeat $click_count = 0;//clicks count if($init == null && $click

  • PHP 万年历实现代码

    使用PHP实现万年历功能的要点: •得到当前要处理的月份总共有多少天$days •得到当前要处理的月份的一号是星期几$dayofweek $days的作用:知道要处理的月份共有多少天,就可以通过循环输出天数了 $dayofweek的作用:只有知道每个月的1号是星期几,才能知道在输出天数之前需要输出多少空格(空白) 最终效果图如下: "万年历类"的代码如下: 复制代码 代码如下: <?php /** * PHP万年历 * @author Fly 2012/10/16 */ clas

  • 超级简易的JS计算器实例讲解(实现加减乘除)

    废话不多说,直接上代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>简单的计算器</title> <style> body{ margin: 0; } .tab{ border: 3px solid black ; border-radius: 2px; border-collapse:collapse; width: 268p

  • golang基于websocket实现的简易聊天室程序

    本文实例讲述了golang基于websocket实现的简易聊天室.分享给大家供大家参考,具体如下: 先说点无关的,最近忙于工作没有更新博客,今天休息顺便把golang websocket研究了一下,挺好玩的,写了一个聊天室,分享给大家. websocket包 : code.google.com/p/go.net/websocket 文档 : http://go.pkgdoc.org/code.google.com/p/go.net/websocket 首先安装websocket包 复制代码 代码

随机推荐