您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)C++實(shí)現(xiàn)萬年歷的方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
具體內(nèi)容如下
#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(); //打印當(dāng)月日歷的主函數(shù) void PrintFirstNum(int mon); void Printyear(int year); void ThisYear(int p); //打印當(dāng)年日歷的主函數(shù) void Month(int n); void Printnum(int q,int mon); int Firstwday[12]; //儲(chǔ)存每個(gè)月1號(hào)的星期數(shù) struct tm *local; string ch; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //儲(chǔ)存每月的天數(shù) 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<<"當(dāng)前時(shí)間是:"<<local->tm_hour<<"時(shí)"<<local->tm_min<<"分"<<local->tm_sec <<"秒"<<local->tm_wday<<endl; fout<<"當(dāng)前時(shí)間是:"<<local->tm_hour<<"時(shí)"<<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") //根據(jù)選擇的數(shù)字確定調(diào)用哪個(gè)函數(shù) ThisMonth(); else if(flag=="2") ThisYear(p); else if(flag=="3") SomeYear(p); else if(flag=="4") break; else { cout<<"輸入錯(cuò)誤"<<endl; fout<<"輸入錯(cuò)誤"<<endl; continue; } }while(1); return 0; } string Choose() { cout<<"請(qǐng)選擇"<<"1、當(dāng)月日歷"<<endl<<" 2、當(dāng)年日歷"<<endl <<" 3、萬年歷"<<endl<<" 4、退出"<<endl; fout<<"請(qǐng)選擇"<<"1、當(dāng)月日歷"<<endl<<" 2、當(dāng)年日歷"<<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) //打印萬年歷的主函數(shù) { 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); //沒加這句話會(huì)死循環(huán),加了就ok } else break; } fout<<m; cout<<endl; fout<<endl; Printyear(m); int n=m; if(n<local->tm_year) //計(jì)算所輸年份的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) //打印標(biāo)題 { 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) //計(jì)算當(dāng)天到當(dāng)年1月1日的天數(shù) { int sum=day; for(mon--;mon!=0;mon--) sum=sum+a[mon-1]; return sum; } void FirstDay(int wday) //推算每個(gè)月1號(hào)的星期數(shù) { 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") //如果是打出當(dāng)月日歷就直接跳出循環(huán) { 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) //打印每?jī)蓚€(gè)月的日歷 { int mday[2]; //儲(chǔ)存每?jī)蓚€(gè)月當(dāng)前打印的日期 do{ int k=0; for(;k<Firstwday[mon-1];k++) { cout<<" "; fout<<" "; } k++; for(int d=1;k<8;d++,k++) //輸入每個(gè)月的第一行 { 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) //當(dāng)雙月未結(jié)束,單月輸入結(jié)束跳出時(shí)最后一行的輸出情況 { 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) //當(dāng)單月未結(jié)束,雙月輸入結(jié)束跳出時(shí)最后一行的輸出情況 { 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) //打印當(dāng)月日歷 { 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; }
感謝各位的閱讀!關(guān)于“C++實(shí)現(xiàn)萬年歷的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。