[C/C++][控制台] 日历程序的实现
// a.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include<windows.h>
#include<iostream>
#include<ctime>
#include <conio.h>
using namespace std;
#define GETSYSTEMTIME 1
//************************************
// Method: GetMonth
// FullName:GetMonth
// Access: public
// Returns: int
// Qualifier:
// Parameter: int * Month
// Parameter: int m
// Parameter: int First
//************************************
int GetMonth(const int * Month,int m,int First);
bool isAutomatic = false;//默认自动获取
void init()
{
int a = 1; //光标坐标,默认为1
bool isDirect = false;
CLS:
system("cls"); //首先清屏
if(a==1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_GREEN);
cout << "1. Auto. Mode" << endl; //自动模式
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED);
cout << "1. Auto. Mode" << endl; //自动模式
}
if(a==2)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_GREEN );
cout << "2. Input Mode"<< endl;
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED);
cout << "2. Input Mode"<< endl;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED);
while(true)
{
TCHAR key = _getch();
TCHAR key1;
//cout << (int)key << endl;
if((int)key == 224)
{
isDirect = true;
key1 = _getch();
//cout << (int)key1<< endl;
if(key1 == 72)
{
a= 1;
}
else
if(key1 == 80)
{
a = 2;
}
goto CLS;
}
else
if(key == 13)
{
a==1 ? isAutomatic = true:isAutomatic = false;
return;
}
}
}
int main(int argc,char ** argv)
{
init();
system("cls");
int year;
int DisplayMonth;
int day = 0;
//#if GETSYSTEMTIME
if(isAutomatic)
{char * Buffer= new char; //申请50个字节的空间
time_t t = time(NULL);
cout << asctime(localtime(&t))<< endl;
strftime(Buffer,50,"%Y",localtime(&t)); //获取年份
year = atoi(Buffer); // String to Integer
strftime(Buffer,50,"%m",localtime(&t)); //获取月份
DisplayMonth = atoi (Buffer); // str2int
strftime(Buffer,50,"%d",localtime(&t));
day = atoi(Buffer);
delete [] Buffer;//释放空间
}
//cout << DisplayMonth;
//#else
else
{
cout << "Input the Year and Month:";
cin >> year >> DisplayMonth;
}
//#endif
//#if GETSYSTEMTIME
//if(isAutomatic)
//{
//}
//#endif
//cin >> year >> DisplayMonth;
int date;
int FirstDate = (year-1+((year-1)/4)-((year-1)/4)-(year-1)/100+(year-1)/400)%7;
/***************************************************************************************/
/* 365 % 7 = 1 ,假设2000年 1.1为Monday, 则 2001 1.1 为Tuesday ,如果是闰年的话则两天 */
/***************************************************************************************/
int Month = {31,28,31,30,31,30,31,31,30,31,30,31};
//for(int i = 0;i<12;i++)
//{
// cout <<Month << endl;
// }
int MonthFirstDay ;
bool isLeap = false;
ZeroMemory(date,sizeof(date));
if((!year%4&&year%100)||!year%400)
isLeap = true;
if(isLeap)
{
Month = 29;
}
MonthFirstDay = GetMonth(Month,DisplayMonth,FirstDate);
//cout << MonthFirstDay<< endl;
/////////////////////////////////////////////////////////////
///////////////////////填充表示日期的数组///////////////////
for(int i = MonthFirstDay,a = 1;i</*playMonth*/MonthFirstDay+Month/*(||a != Month)*/;i++,a++)
{
date = a;
}
//////////////////////////////////////////////////////////////////////////
/************************************************************************/
/* 按格式输出日历,如果表示日期的元素为0,则输出“ ”来代替 */
/************************************************************************/
cout << "一"
<< " "
<< "二"
<< " "
<< "三"
<< " "
<< "四"
<< " "
<< "五"
<< " "
<< "六"
<< " "
<< "日"
<< endl;
for(int i = 0;i<6*7+1;i++)
{
if(!date)
{
cout << "";
}
else
{
if(date < 10)
cout << " ";
//if(date <=5)
//cout << " ";
bool b= false;
COORD c;
DWORD d;
if(i==day+MonthFirstDay-1)
{
//cout << day<< endl;;
b= true;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
c = info.dwCursorPosition;
//cout << c.X<< c.Y<< endl;
}
cout << date;
if(b)
{
FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED ,5,c,&d);
}
}
cout << " ";
if(!(i%7))
{ cout << endl;}
}
//////////////////////////////////////////////////////////////////////////
cin.get();
cin.get();
}
int GetMonth(const int * Month,int m,int First)//获取First 月1日 到1月1日的天数
{
int a = 0 ;
//cout<< m;
//m=m-1;
for(int i = 0;i<m-1;i++)
{
//cout <<Month << endl;
a=a+Month;
}
return a%7+First;
}
页:
[1]