C++實現(xiàn)萬年歷源代碼
更新時間:2020年03月20日 08:04:48 作者:jxccy888
這篇文章主要介紹了C++實現(xiàn)萬年歷源代碼,可以直接在VC6.0編譯運行,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++實現(xiàn)萬年歷的具體代碼,供大家參考,具體內容如下
#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(); //打印當月日歷的主函數(shù) void PrintFirstNum(int mon); void Printyear(int year); void ThisYear(int p); //打印當年日歷的主函數(shù) void Month(int n); void Printnum(int q,int mon); int Firstwday[12]; //儲存每個月1號的星期數(shù) struct tm *local; string ch; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //儲存每月的天數(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<<"當前時間是:"<<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") //根據(jù)選擇的數(shù)字確定調用哪個函數(shù) 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) //打印萬年歷的主函數(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án),加了就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日的天數(shù) { int sum=day; for(mon--;mon!=0;mon--) sum=sum+a[mon-1]; return sum; } void FirstDay(int wday) //推算每個月1號的星期數(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") //如果是打出當月日歷就直接跳出循環(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) //打印每兩個月的日歷 { 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; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。