本文实例为大家分享了C++实现万年历的具体代码,供大家参考,具体内容如下
#include#include #include #include using namespace std; #include 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<<"今天是:"< tm_year<<"年"< tm_mon <<"月"< tm_mday<<"日,"; fout<<"今天是:"< tm_year<<"年"< tm_mon <<"月"< tm_mday<<"日,"; Month(local->tm_wday); cout< tm_hour<<"时"< tm_min<<"分"< tm_sec <<"秒"< tm_wday< tm_hour<<"时"< tm_min<<"分"< tm_sec <<"秒"< tm_wday< 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<<"输入错误"< >ch; fout< 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"< tm_year) //计算所输年份的1月1日星期几 { for(;n 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< 6) Firstwday[n+1]=Firstwday[n+1]-7; } } int OrEndl(int n) { if(ch=="1") //如果是打出当月日历就直接跳出循环 { cout< 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



