当前位置 : 主页 > 编程语言 > c语言 >

C++实现万年历源代码

来源:互联网 收集:自由互联 发布时间:2021-05-12
本文实例为大家分享了C++实现万年历的具体代码,供大家参考,具体内容如下 #includeiostream#includestring#includefstream#includeiomanipusing namespace std;#includetime.hofstream fout("日历.txt");void Printtitle

本文实例为大家分享了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;
}

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

网友评论