一个C++日历源码

includes #include <iostream> #include <iomanip> #include <cmath> #include <windows.h> #include <cstdlib> using namespace std; void clrscr(); void gotoxy(int,int); class calender { public: calender(int d=1,int m=1,int y=2001); virtual ~calender() {} void printcalender(); private: bool isleapyear(); int checkday(int); int whatdayisfirstofmonth(); int howmanydays(); int day; int month; int year; }; calender::calender(int d,int m,int y) { if (y<1582) { cout<<endl<<"The year "<<y<<" is before the gregorian calender was accepted by the church." <<"Setting to 2001."<<endl; year=2001; } else { year=y; } if ((m>=1) && (m<=12)) { month=m; } else { cout<<endl<<"The month "<<m<<" is invalid. Setting to month 1"<<endl; month=1; } day=checkday(d); } bool calender::isleapyear() { if ((year%400==0) || ((year %4==0) && (year%100 !=0))) { return true; } else { return false; } } int calender::checkday(int testday) { if ((testday>0) && (testday<= howmanydays())) { return testday; } cout<<endl<<"Invalid day entered "<<testday<<"/"<<month<<"/"<<year <<". Setting to the first of the month."<<endl; return 1; } int calender::howmanydays() { if((month==2)&& isleapyear()) { return 29; } static const int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31}; return daysinmonth[month-1]; } int calender::whatdayisfirstofmonth() { int c=year/100; int d=year%100; int m=(month+10)%12; int k=1; if ((month==1)||(month==2)) { if (d==0) { d=99; c-=1; } else { d-=1; } } float g=(k + (floor(((13*m)-1)/5)) + d + (floor(d/4)) + (floor(c/4)) - (2*c)); int f=static_cast<int>(g)%7; if (f<0) { f+=7; } return f; } void calender::printcalender() { clrscr(); cout<<"Date entered was :- "<<day<<"/"<<month<<"/"<<year<<endl; cout<<endl<<setw(8)<<"SUNDAY"<<setw(8)<<"MONDAY"<<setw(9)<<"TUESDAY"<<setw(11)<<"WEDNESDAY"<<setw(10) <<"THURSDAY"<<setw(8)<<"FRIDAY"<<setw(10)<<"SATURDAY"<<endl; int startday=whatdayisfirstofmonth(); int endday=howmanydays(); for (int i=0;i<startday;i++) { if (i==0) { gotoxy(4,4); cout<<"-"; } if (i==1) { gotoxy(12,4); cout<<"-"; } if (i==2) { gotoxy(21,4); cout<<"-"; } if (i==3) { gotoxy(31,4); cout<<"-"; } if (i==4) { gotoxy(42,4); cout<<"-"; } if (i==5) { gotoxy(50,4); cout<<"-"; } } int rows=4; int count=1; for(int j=startday;j<(startday+endday);j++) { if(j%7==0) { rows+=2; gotoxy(4,rows); } if(j%7==1) gotoxy(12,rows); if(j%7==2) gotoxy(21,rows); if(j%7==3) gotoxy(31,rows); if(j%7==4) gotoxy(42,rows); if(j%7==5) gotoxy(50,rows); if(j%7==6) gotoxy(60,rows); if(count==day) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED); } else { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN); } cout<<count; count ++; } cout<<endl<<endl<<endl; } void clrscr() { COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); } void gotoxy(int x, int y) { COORD point; point.X = x; point.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point); } int main() { while (1) { clrscr(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN); cout<<"Welcome to the gregorian calender calculator. v.1.00"<<endl<<endl <<"Use UK format.... day/month/full year. Use the '/' char to delimit your entry..." <<endl<<endl; cout<<"Please enter the date you would like to see the calender for :- "; char input_day[4]; char input_month[4]; char input_year[6]; cin.getline(input_day,3,'/'); cin.getline(input_month,3,'/'); cin.getline(input_year,5,'\n'); cout<<endl; int d=atoi(input_day); int m=atoi(input_month); int y=atoi(input_year); calender date(d,m,y); system("PAUSE"); date.printcalender(); cout<<"Another calender (Y/N) ? "; char input[5]; cin.getline(input,4); if ((input[0]=='N') || (input[0]=='n')) { break; } } return 0; } 蓝莓友情提醒:梅超风来喽,大家注意关好门窗哈!唉,白天的雷声响的我都不敢开电脑。。

转载于:https://www.cnblogs.com/mrjim/archive/2011/08/11/4475236.html

#include int main (void) { int year,month,j,i,day1,day2,one1,one2,w; printf("enter a year:"); scanf("%d",&year); printf("\n\n"); printf("---------------------%d---------------------\n\n",year); one1=((year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400+1)%7; for(month=1;month<=12;month+=2) { printf(" ",month,year); printf(" \n",month+1,year); printf("-------------------- --------------------\n"); printf("日 一 二 三 四 五 六 日 一 二 三 四 五 六\n"); switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day1=31;break; case 4: case 6: case 9: case 11:day1=30;break; default:if(!(year%4)&&(year0)||!(year%400)) day1=29; else day1=28; } for(j=1;j<=one1;j++) { if(one1==7) break; else printf(" "); } for(i=1;i<=7-one1;i++) printf("%2d ",i); printf(" "); switch(month+1) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day2=31;break; case 4: case 6: case 9: case 11:day2=30;break; default:if(!(year%4)&&(year0)||!(year%400)) day2=29; else day2=28; } one2=(one1+day1)%7; for(j=1;j<=one2;j++) { if(one2==7) break; if(j!=7) printf(" "); } for(i=1;i<=7-one2;i++) printf("%2d ",i); printf("\n"); for(i=8-one1;i<=14-one1;i++) printf("%2d ",i); printf(" "); for(i=8-one2;i<=14-one2;i++) printf("%2d ",i); printf("\n"); for(i=15-one1;i<=21-one1;i++) printf("%2d ",i); printf(" "); for(i=15-one2;i<=21-one2;i++) printf("%2d ",i); printf("\n"); for(i=22-one1;i<=28-one1;i++) printf("%2d ",i); printf(" "); for(i=22-one2;i<=28-one2;i++) printf("%2d ",i); printf("\n"); for(i=29-one1;i<=35-one1&&i<=day1;i++) printf("%2d ",i); printf(" "); for(w=1;w<=35-day1-one1;w++) printf(" "); for(i=29-one2;i<=35-one2&&i<=day2;i++) printf("%2d ",i); printf("\n"); for(i=36-one1;i<=day1;i++) printf("%2d ",i); for(w=1;w<=35-day1-one1;w++) printf(" "); if(day1==31&&(one1==4||one1==3||one1==2||one1==1||one1==7)) printf(" "); if(day1==30&&(one1==4||one1==3||one1==2||one1==1||one1==7)) printf(" "); for(i=36-one2;i<=day2;i++) printf("%2d ",i); printf("\n-------------------- --------------------\n\n"); printf("\n"); one1=(one2+day2)%7; } printf("---------------------%d---------------------\n",year); getchar(); printf("按任意键退出"); getchar(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值