C++实现万年历源代码

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

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
#include<time.h>
ofstream fout("日历.txt");

void Printtitle(int n);
int OrEndl(int n);
string Choose();    //选择功能
int Calculate(int mon,int day);
void Printday(int a);
void FirstDay(int wday);
void SomeYear(int p);
void ThisMonth();     //打印当月日历的主函数
void PrintFirstNum(int mon);
void Printyear(int year);
void ThisYear(int p);   //打印当年日历的主函数
void Month(int n);
void Printnum(int q,int mon);
int Firstwday[12];     //储存每个月1号的星期数
 struct tm *local;
string ch;
 int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //储存每月的天数
int main(void)
{
 long t;
  time(&t);
  local=localtime(&t);
  local->tm_year=local->tm_year+1900;
  local->tm_mon++;
  cout<<"今天是:"<<local->tm_year<<"年"<<local->tm_mon
    <<"月"<<local->tm_mday<<"日,";
  fout<<"今天是:"<<local->tm_year<<"年"<<local->tm_mon
    <<"月"<<local->tm_mday<<"日,";
  Month(local->tm_wday);
  cout<<endl;
  fout<<endl;
  cout<<"当前时间是:"<<local->tm_hour<<"时"<<local->tm_min<<"分"<<local->tm_sec
    <<"秒"<<local->tm_wday<<endl;
  fout<<"当前时间是:"<<local->tm_hour<<"时"<<local->tm_min<<"分"<<local->tm_sec
    <<"秒"<<local->tm_wday<<endl;
  string flag;
 int sum;
 if(((local->tm_year%4==0)&&(local->tm_year%100!=0))||(local->tm_year%400==0))
 a[1]=29;
 sum=Calculate(local->tm_mon,local->tm_mday);
 int p=sum-(local->tm_wday+1)-(sum/7)*7;
  do{
     flag=Choose();
     if(flag=="1")    //根据选择的数字确定调用哪个函数
      ThisMonth();
     else if(flag=="2")
      ThisYear(p);
     else if(flag=="3")
      SomeYear(p);
     else if(flag=="4")
      break;
  else
  {
  cout<<"输入错误"<<endl;
    fout<<"输入错误"<<endl;
  continue;
  }
 }while(1);
  return 0;
}

string Choose()
{
 cout<<"请选择"<<"1、当月日历"<<endl<<"   2、当年日历"<<endl
 <<"   3、万年历"<<endl<<"   4、退出"<<endl;
 fout<<"请选择"<<"1、当月日历"<<endl<<"   2、当年日历"<<endl
 <<"   3、万年历"<<endl<<"   4、退出"<<endl;
 cin>>ch;
 fout<<ch;
 cout<<endl;
 fout<<endl;
 return ch;
 }

void ThisMonth()
{
 int m=local->tm_mon%12;
 Printtitle(m);
 int p=local->tm_mday-(local->tm_wday+1)-(local->tm_mday/7)*7;
 Printnum(p,local->tm_mon);
}

void ThisYear(int p)
{
 FirstDay(p);
 Printyear(local->tm_year);
 for(int a=1;a<12;a=a+2)
 {
 Printtitle(a);
 PrintFirstNum(a);
 }
}

void SomeYear(int p)  //打印万年历的主函数
{
 int m;
 cout<<"Please enter a year number"<<endl;
 fout<<"Please enter a year number"<<endl;
 while(1)
 {  

  scanf("%d",&m);
  if(  m<0  )
  {
  printf("\nInput error,Please enter a year number again:\n");
  fflush(stdin);    //没加这句话会死循环,加了就ok
  }
  else  break;
 }
 fout<<m;
 cout<<endl;
  fout<<endl;
 Printyear(m);
  int n=m;
  if(n<local->tm_year)   //计算所输年份的1月1日星期几
  {
  for(;n<local->tm_year;n++)
  {
   if(((n%4==0)&&(n%100!=0))||(n%400==0))
   p=p+2;
    else
   p++;
  if(p>=7)
   p=p-7;
  }
 }
  else
  {
  for(;n>local->tm_year;n--)
  {
   if(((n%4==0)&&(n%100!=0))||(n%400==0))
   p=p-2;
   else
   p--;
  if(p<0)
   p=p+7;
  }
  }
FirstDay(p);
 for(int h=1;h<12;h=h+2)
 {
 Printtitle(h);
  if(((m%4==0)&&(m%100!=0))||(m%400==0))
  a[1]=29;
  else
  a[1]=28;
 PrintFirstNum(h);
 }
}

void Printtitle(int n)  //打印标题
{
 do{
   cout<<"     ";
   fout<<"     ";
 char str[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
 for(int h=0;h<10;h++)
 {
 cout<<str[n-1][h];
 fout<<str[n-1][h];
 }
 cout<<"     ";
   fout<<"     ";
 if(OrEndl(n))
 break;
n++;
 }while(!(n%2));
 do{
  cout<<"____________________________";
   fout<<"____________________________";
if(OrEndl(n))
 break;
n++;
}while(!(n%2));
  do{
cout<<" Sun Mon Tue Wed Thu Fri Sat ";
fout<<" Sun Mon Tue Wed Thu Fri Sat ";
if(OrEndl(n))
 break;
n++;
 }while(!(n%2));
}

int Calculate(int mon,int day)  //计算当天到当年1月1日的天数
{
 int sum=day;
for(mon--;mon!=0;mon--)
 sum=sum+a[mon-1];
 return sum;
}
void FirstDay(int wday)   //推算每个月1号的星期数
{
 if(wday<=0)
 wday=wday+7;
 Firstwday[0]=7-wday;
 for(int n=0;n<11;n++)
 {
 Firstwday[n+1]= Firstwday[n]+a[n]%7;
 if(Firstwday[n+1]>6)
      Firstwday[n+1]=Firstwday[n+1]-7;
 }
}

int OrEndl(int n)
{
   if(ch=="1")    //如果是打出当月日历就直接跳出循环
{
 cout<<endl;
 fout<<endl;
 return 1;
}
 if(n%2)        //判断单月输空格,双月回车
 {
 cout<<"    ";
 fout<<"    ";
 }
else
{
 cout<<endl;
 fout<<endl;
}
return 0;
}

void Printyear(int year) //打印年份
{
 int m=year/1000;
 int n=(year/100)%10;
 int p=(year/10)%10;
 int q=year%10;
 int num[4]={m,n,p,q};
 char str[5][10][7]={"***** ","  * ","***** ","***** ","*  * ","***** ","***** ","***** ","***** ","***** ",
         "*  * ","  * ","  * ","  * ","*  * ","*   ","*   ","  * ","*  * ","*  * ",
            "*  * ","  * ","***** ","***** ","***** ","***** ","***** ","  * ","***** ","***** ",
            "*  * ","  * ","*   ","  * ","  * ","  * ","*  * ","  * ","*  * ","  * ",
            "***** ","  * ","***** ","***** ","  * ","***** ","***** ","  * ","***** ","***** ",};
 for(int g=0;g<5;g++)
 {
 cout<<"          ";
 fout<<"          ";
 for(int i=0;i<4;i++)
   for(int h=0;h<7;h++)
 {
  cout<<str[g][num[i]][h];
  fout<<str[g][num[i]][h];
 }
  cout<<endl;
  fout<<endl;
 }
}

void PrintFirstNum(int mon)  //打印每两个月的日历
{
 int mday[2];       //储存每两个月当前打印的日期
 do{
 int k=0;
   for(;k<Firstwday[mon-1];k++)
 {   cout<<"  ";
  fout<<"  ";
 }
   k++;
 for(int d=1;k<8;d++,k++)      //输入每个月的第一行
 {
 cout<<" "<<d<<" ";
 fout<<" "<<d<<" ";
 }
 if(mon%2)             //判断单月输空格,双月回车
 {
  cout<<"    ";
  fout<<"    ";
  mday[mon%2-1]=d;
 }
 else
 {
  cout<<endl;
    fout<<endl;
    mday[mon%2+1]=d-1;
 }
 mon++;
 }while(!(mon%2));
 mon=mon-2;
  int i=0,k=1,m=mon-1;
  for(;mday[i]<a[m]+1;mday[i]++,k++)
  {
  if(mday[i]<10)
 {
   cout<<" "<<mday[i]<<" ";
 fout<<" "<<mday[i]<<" ";
  }
  else
 {
 cout<<" "<<mday[i]<<" ";
 fout<<" "<<mday[i]<<" ";
 }
  if(k==7)
  {
 if(!i)
   {
  cout<<"    ";
  fout<<"    ";
   i=1;
   m++;
 }
   else
   {
  cout<<endl;
  fout<<endl;
   i=0;
   m--;
 }
   k=0;
 }
  }
  m=mon-1;
  if(mday[0]==a[m]+1&&mday[1]<a[m+1]+1)    //当双月未结束,单月输入结束跳出时最后一行的输出情况
  {
  for(;k<8;k++)
  {
   cout<<"  ";
  fout<<"  ";
  }
  cout<<"    ";
 fout<<"    ";
   k=1;
  for(mday[1]++;mday[1]<a[m+1]+1;mday[1]++,k++)
   {
 cout<<" "<<mday[1]<<" ";
  fout<<" "<<mday[1]<<" ";
   if(k==7)
  {
  cout<<endl;
   fout<<endl;
    cout<<"                  ";
   fout<<"                  ";
  }
   }
   cout<<endl;
 fout<<endl;
  }
  if(mday[0]<a[m]+1&&mday[1]==a[m+1]+1)  //当单月未结束,双月输入结束跳出时最后一行的输出情况
  {
  cout<<endl;
 fout<<endl;
   k=1;
  for(mday[0]++;mday[0]<a[m]+1;mday[0]++,k++)
 {
 cout<<" "<<mday[0]<<" ";
  fout<<" "<<mday[0]<<" ";
   if(k==7)
  {
  cout<<endl;
   fout<<endl;
    cout<<"                  ";
   fout<<"                  ";
  }
   }
   cout<<endl;
 fout<<endl;
  }
}

void Month(int n)
{
 char str[7][7]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
 for(int h=0;h<7;h++)
 {
 cout<<str[n][h];
 fout<<str[n][h];
 }
}

void Printnum(int q,int mon)  //打印当月日历
{
 if(q<0)
    q=q+7;
int k=0;
 if(q!=7&&q)
 {
 for(;k<7-q;k++)
 {
  cout<<"  ";
 fout<<"  ";
 }
 }
  k++;
  for(int d=1;d<a[mon-1]+1;d++,k++)
 {
 cout<<setw(4)<<d;
   if(k==7)
 {
  cout<<endl;
  fout<<endl;
  k=0;
 }
 }
  cout<<endl;
  fout<<endl;
}

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

(0)

相关推荐

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

  • 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++实现万年历的具体代码,供大家参考,具体内容如下 #include<iostream> #include<string> #include<fstream> #include<iomanip> using namespace std; #include<time.h> ofstream fout("日历.txt"); void Printtitle(int n); int OrEndl(int n);

  • AJAX集天气\IP\多国语言翻译MP3(可同步LRC歌词显示)\万年历查询通

    '转发时请保留此声明信息,这段声明不并会影响你的速度! '****天枫AJAX集天气\IP\多国语言翻译MP3(可同步LRC歌词显示)\万年历查询通******** '作者:天枫 '网站:http://www.52515.net '电子邮件:chenshaobo@gmail.com 'WEB开发群:4635188 19182747 'QQ:76994859 '版权声明:版权所有,源代码公开,各种用途均可免费使用,但是修改后必须把修改后的文件 '发送一份给作者.并且保留作者此版权信息 '*****

  • Java实现简单的万年历

    本文实例为大家分享了Java实现简单万年历的具体代码,供大家参考,具体内容如下 1 要求 1.输入年份: 2.输入月份: 3.输出某年某月的日历. 2 思路 1.实现从控制台接收年和月,判断是否是闰年(判断是否是闰年:能被4整除但不能被100整除:或者能被400整除): 2.计算输入月份的天数: 3.计算该月第一天是星期几: 3.1 计算输入年份距离1900年1月1日的天数: 3.2 计算输入月份之前的天数(从当年年初开始): 3.3 将以上两组数据进行求和: 3.4 已知该月之前的天数,计算输

  • win7下从ruby源代码编译安装的方法

    工作中需要在c++代码中嵌入ruby c api,然而在vs工程中编译失败,所以现在通过手动从源代码编译ruby寻找原因(之前使用rubyinstaller安装). 先从官网下载ruby 2.4.1 版本,https://www.ruby-lang.org/en/downloads/ 从安装指导可以看到,官方只提供了linux平台下的编译安装步骤,https://www.ruby-lang.org/en/documentation/installation/#building-from-sour

  • PHP 万年历实现代码

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

  • 解密ASP源代码

    从网上兴致冲冲地下载了ASP源代码,准备学习研究的时候.一打开文件,天书般的加密代码.很让人郁闷吧 :( 在网上是找到了解密的方法,得一个文件挨一个文件地打开,复制,粘贴,解密,再复制,再粘贴,再保存......如果一个ASP程序有几百个文件??? 解决办法来了.. 复制代码 代码如下: <% @Language="JavaScript" %> <% /*  *--------------- decode.asp -----------------  * 功能:遍历某

  • FCKeditor 源代码分析附中文注释

    这几天都在研究FCKeditor的源代码 (FCKeditor就是网络中应用比较广泛的网页编辑器)  这里需要感谢nileaderblog的辛苦翻译. 几乎搜遍了Internet,似乎对于fckconfig.js这个文件讲解的很多,但对于fckeditor.js这个FCK的核心类文件的资料几乎为0. 所以,花了整整一天的时间,以挤牙膏的方式,对fckeditor.js这个fck核心类文件作了自己力所能及的注释,供同样学习fck的网友一个参考. 鉴于笔者水平有限,在此,请广大高手指出我的注释中不妥

  • 用js一招破解所有网页的加密源代码的方法

    现在很流行源代码加密,无论出于作者想保护自己的资源,还是放马者为了不让别人发现网页有马等等,都对源代码加密. 想破解它很简单,一招就搞定了,是不是用点心动了呢?是的就快跟我来吧!comeon gogogo! 1/首先我们来看看一个网页的源代码,为了不浪费大家的时间,我在免费空间随便上传个网页(已经加密的)大家来看看,为了便以大家看到结果,我做很简单(真的很简单咯)我门来看看他的源代码,方法有很多,我随便用一种 ,这是什么东西,难道这就是传说中的秘籍,,说笑了,大家是不是看不明呢,那我们就来进行第

  • jQuery语法高亮插件支持各种程序源代码语法着色加亮

    1) highlightjs Highlight.js能够对页面中的各种程序源代码语法着色加亮.支持的语言包括: •Python •Ruby •Perl •PHP •XML •HTML •CSS •Django •Javascript •VBScript •Delphi •Java •C++ •RenderMan (RSL and RIB) •SQL •SmallTalk •Axapta •1C •Ini •Diff •DOS .bat •Bash 2) google-code-prettify

  • JSP中的源代码泄漏问题

    摘要:在JSP技术得到广泛应用的同时,由于源代码泄漏而引起的JSP安全性也受到了广泛的关注.本文分析了几种造成源代码泄漏的因素,并针对每种因素提出了各自的解决方法. 关键词:JSP  源代码 泄漏 引言 JSP编程语言自从推出之日起,由于它的快速.平台无关.可扩展.面向对象等特性得到了越来越广泛的应用,越来越多的厂家开发出了各种各样的支持平台如IBM 公司的WebSphere.BEA公司的WebLogic等等,也有越来越多的网站开始将自己的平台架构在JSP 环境中.   但是随之而来的就是一系列

随机推荐