找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3592|回复: 0

[C/C++][控制台] 日历程序的实现

[复制链接]

17

主题

28

回帖

598

积分

用户组: 大·技术宅

UID
140
精华
5
威望
30 点
宅币
442 个
贡献
26 次
宅之契约
0 份
在线时间
54 小时
注册时间
2014-3-22
发表于 2014-9-30 22:45:12 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
  1. // a.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include<windows.h>
  5. #include<iostream>
  6. #include<ctime>
  7. #include <conio.h>
  8. using namespace std;
  9. #define GETSYSTEMTIME 1
  10. //************************************
  11. // Method:    GetMonth
  12. // FullName:  GetMonth
  13. // Access:    public
  14. // Returns:   int
  15. // Qualifier:
  16. // Parameter: int * Month
  17. // Parameter: int m
  18. // Parameter: int First
  19. //************************************
  20. int GetMonth(const int * Month,int m,int First);
  21. bool isAutomatic = false;  //默认自动获取
  22. void init()
  23. {
  24.         int a = 1; //光标坐标,默认为1
  25.         bool isDirect = false;
  26.         CLS:
  27.         system("cls"); //首先清屏
  28.         if(a==1)
  29.         {       
  30.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_GREEN);
  31.                 cout << "1. Auto. Mode" << endl; //自动模式
  32.         }
  33.         else
  34.         {
  35.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED);
  36.                 cout << "1. Auto. Mode" << endl; //自动模式
  37.         }
  38.         if(a==2)
  39.         {       
  40.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_GREEN );
  41.                 cout << "2. Input Mode"<< endl;
  42.         }
  43.         else
  44.         {
  45.                 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED);
  46.                 cout << "2. Input Mode"<< endl;
  47.         }
  48.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED);
  49.         while(true)
  50.         {
  51.                  TCHAR key = _getch();
  52.                 TCHAR key1;
  53.                 //cout << (int)key << endl;
  54.                 if((int)key == 224)
  55.                 {
  56.                         isDirect = true;
  57.                         key1 = _getch();
  58.                         //cout << (int)key1<< endl;
  59.                         if(key1 == 72)
  60.                         {
  61.                                 a= 1;
  62.                         }
  63.                         else
  64.                                 if(key1 == 80)
  65.                                 {
  66.                                         a = 2;
  67.                                 }
  68.                         goto CLS;
  69.                 }
  70.                 else
  71.                         if(key == 13)
  72.                         {
  73.                                 a==1 ? isAutomatic = true:isAutomatic = false;
  74.                                 return;
  75.                         }
  76.         }

  77. }
  78. int main(int argc,char ** argv)
  79. {
  80.         init();
  81.         system("cls");
  82.         int year;
  83.         int DisplayMonth;
  84.         int day = 0;
  85. //#if GETSYSTEMTIME
  86. if(isAutomatic)
  87. {char * Buffer  = new char[50]; //申请50个字节的空间
  88.         time_t t = time(NULL);
  89.         cout << asctime(localtime(&t))<< endl;
  90.         strftime(Buffer,50,"%Y",localtime(&t)); //获取年份
  91.         year = atoi(Buffer); // String to Integer
  92.         strftime(Buffer,50,"%m",localtime(&t)); //获取月份

  93.         DisplayMonth = atoi (Buffer); // str2int
  94.         strftime(Buffer,50,"%d",localtime(&t));
  95.         day = atoi(Buffer);
  96.         delete [] Buffer;//释放空间
  97. }
  98.         //cout << DisplayMonth;
  99. //#else
  100. else
  101. {
  102.         cout << "Input the Year and Month:";
  103.         cin >> year >> DisplayMonth;
  104. }
  105. //#endif
  106. //#if GETSYSTEMTIME
  107.         //if(isAutomatic)
  108.         //{


  109.        
  110.         //}
  111. //#endif
  112.         //cin >> year >> DisplayMonth;
  113.         int date[6*7+1];
  114.         int FirstDate = (year-1+((year-1)/4)-((year-1)/4)-(year-1)/100+(year-1)/400)%7;
  115. /***************************************************************************************/
  116. /* 365 % 7 = 1 ,假设2000年 1.1为Monday, 则 2001 1.1 为Tuesday ,如果是闰年的话则两天                                                                     */
  117. /***************************************************************************************/
  118.          int Month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  119.          //for(int i = 0;i<12;i++)
  120.          //{
  121.         //         cout <<Month[i] << endl;

  122.         // }
  123.         int MonthFirstDay ;
  124.         bool isLeap = false;
  125.         ZeroMemory(date,sizeof(date));
  126.        
  127.         if((!year%4&&year%100)||!year%400)
  128.                 isLeap = true;
  129.         if(isLeap)
  130.         {
  131.                 Month[1] = 29;
  132.         }
  133.         MonthFirstDay = GetMonth(Month,DisplayMonth,FirstDate);
  134.         //cout << MonthFirstDay<< endl;
  135.         /////////////////////////////////////////////////////////////
  136.         ///////////////////////填充表示日期的数组///////////////////
  137.         for(int i = MonthFirstDay,a = 1;i</*playMonth*/MonthFirstDay+Month[DisplayMonth-1]/*(||a != Month[DisplayMonth - 1])*/;i++,a++)
  138.         {
  139.                 date[i] = a;
  140.        
  141.         }
  142.         //////////////////////////////////////////////////////////////////////////
  143.         /************************************************************************/
  144.         /*                        按格式输出日历,如果表示日期的元素为0,则输出“ ”来代替                                                                     */
  145.         /************************************************************************/
  146.         cout << "一"
  147.                 << "    "
  148.                 << "二"
  149.                 << "    "
  150.                 << "三"
  151.                 << "    "
  152.                 << "四"
  153.                 << "    "
  154.                 << "五"
  155.                 << "    "
  156.                 << "六"
  157.                 << "    "
  158.                 << "日"
  159.                 << endl;
  160.         for(int i = 0;i<6*7+1;i++)
  161.         {
  162.                 if(!date[i])
  163.                 {
  164.                         cout << "  ";
  165.                 }
  166.                 else
  167.                 {
  168.                         if(date[i] < 10)
  169.                                 cout << " ";
  170.                         //if(date[i] <=5)
  171.                                 //cout << " ";
  172.                         bool b= false;
  173.                         COORD c;
  174.                         DWORD d;
  175.                         if(i==day+MonthFirstDay-1)
  176.                         {
  177.                                 //cout << day<< endl;;
  178.                                 b= true;
  179.                                 CONSOLE_SCREEN_BUFFER_INFO info;
  180.                                 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info);
  181.                                 c = info.dwCursorPosition;
  182.                                 //cout << c.X<< c.Y<< endl;
  183.                                
  184.                         }
  185.                         cout << date[i];
  186.                         if(b)
  187.                         {
  188.                                 FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_RED ,5,c,&d);
  189.                         }
  190.                 }
  191.                 cout << "    ";
  192.                 if(!(i%7))
  193.                 {        cout << endl;}
  194.        
  195.         }
  196.         //////////////////////////////////////////////////////////////////////////
  197.         cin.get();
  198.         cin.get();
  199.        
  200.         }




  201. int GetMonth(const int * Month,int m,int First)  //获取First 月1日 到1月1日的天数
  202. {
  203.         int a = 0 ;
  204.         //cout<< m;
  205.         //m=m-1;
  206.         for(int i = 0;i<m-1;i++)
  207.         {
  208.                 //cout <<Month[i] << endl;
  209.                 a=a+Month[i];
  210.         }

  211.         return a%7+First;
  212. }
复制代码




QQ截图20140930224344.png
QQ截图20140930224400.png
QQ截图20140930224418.png
QQ截图20140930224438.png
回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-5-4 10:20 , Processed in 0.038766 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表