一个C++日历源码
2011-08-11 22:04
363 查看
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;
}
蓝莓友情提醒:梅超风来喽,大家注意关好门窗哈!唉,白天的雷声响的我都不敢开电脑。。
#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;
}
蓝莓友情提醒:梅超风来喽,大家注意关好门窗哈!唉,白天的雷声响的我都不敢开电脑。。
相关文章推荐
- 问题:利用 C++的各种控制语句编写一个万年历程序。 要求:显示任何年份的日历,日历以月份顺序排列,每月以星期顺序排列,类似于一 般挂历上的格式。
- 用C++写一个日历程序,要求输入年份,显示全年的日历
- Code::Blocks 是一个开放源码的全功能的跨平台C/C++集成开发环境.
- 一个很好的js日历,提供源码下载。并附上截图
- 【源码】一个动态日历的JS
- 一个超酷的 javascript日历控件带时分秒 源码
- c++的一个程序源码记录
- c++ 多线程写日志的一个很实用的日志类源码(支持 c++ builder)
- 看到了一个 蒙特卡洛方法 随机数得出 圆周率的c++ 源码
- 一个C++游戏框架开源项目部分源码(仅供分享)
- 一个c++拼图游戏源码
- 一个C++实现的md5源码
- 一个日历问题的C语言,C++(boost),python,Javascript,Java和Matlab的实现
- 一个C++开发的即时通软件源码 .
- 一个c++转c的工具(cfront源码)
- 分享一个C++源码(有助于MFC学习)
- 一个C++实现的md5源码(转)
- 条款三十四:如何在同一个程序中结合C++和C
- c++ 模版函数的定义和实现放在一个文件中
- C++:输出日历的算法