一个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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    一个C++日历源码

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/30344

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午7:41
下一篇 2023年2月8日 上午7:41

相关推荐