飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 4277|回复: 6

[C/C++] 万年历的框架(ing)还没完成,只是一个类

[复制链接]

该用户从未签到

发表于 2010-12-4 15:04:31 | 显示全部楼层 |阅读模式
本帖最后由 popo2008 于 2010-12-4 15:14 编辑

原来没写说类  还一个函数没写完,先保存一下~~在VS2010编译


  1. /********************************************************************************************************
  2. * PROGRAM      : Calendar 0.11(DEC.3 0.1)
  3. * DATE - TIME  : Methew DEC. 4,2010
  4. * AUTHOR       : Methew
  5. * FILENAME     : Calendar.h
  6. * LICENSE      :
  7. * COMMENTARY   :
  8. ********************************************************************************************************/
  9. #pragma once
  10. class CCalendar
  11. {
  12. public:
  13.         CCalendar(void);
  14.         virtual ~CCalendar(void);
  15.         // 判断闰年
  16.         int isBissextile(int iYear);
  17.         // 当年天数
  18.         int fallday(int iYear, int iMonth, int iDay);
  19.         // 当年的1月1日星期几
  20.         int week(int iYear);
  21.         // TC的模拟
  22.         void gotoxy(short x, short y);
  23.         // 日历的格式化
  24.         void FormatDate(int FstWeek);
  25. };
复制代码

Calendar.cpp

  1. #include "StdAfx.h"
  2. #include "Calendar.h"





  3. CCalendar::CCalendar(void)
  4. {
  5. }


  6. CCalendar::~CCalendar(void)
  7. {
  8. }


  9. // 判断闰年
  10. int CCalendar::isBissextile(int iYear)
  11. {
  12.         return iYear%(iYear%100?4:400)?0:1;
  13. }


  14. // 当年天数
  15. int CCalendar::fallday(int iYear, int iMonth, int iDay)
  16. {
  17.         int a,allday;
  18.         allday =0;
  19.         for(a = 1;a <= iMonth-1;a++)
  20.         {
  21.                 if(a == 1 || a == 3 ||a == 5 ||a == 7 ||a == 8 ||a == 10 ||a == 12)
  22.                 {
  23.                         allday += 31;
  24.                 }
  25.                 else if(a == 2)
  26.                 {
  27.                         if (isBissextile(iYear))
  28.                         {
  29.                                 allday +=29;
  30.                         }
  31.                         else
  32.                         {
  33.                                 allday +=28;
  34.                         }
  35.                 }
  36.                 else
  37.                 {
  38.                         allday += 30;
  39.                 }
  40.         }
  41.         allday += iDay;
  42.         return allday;
  43. }


  44. // 当年的1月1日星期几
  45. int CCalendar::week(int iYear)
  46. {
  47.         char aday,week;
  48.         int n,i;
  49.         week = 1;
  50.         n = iYear - 1900;
  51.         for(i = 1;i <= n ;i++)
  52.         {
  53.                 if(isBissextile(iYear+i))
  54.                 {
  55.                         aday = 2;
  56.                 }
  57.                 else
  58.                 {
  59.                         aday = 1;
  60.                 }
  61.                 week += aday;
  62.                 if(week >7){week = week%7;}
  63.         }
  64.         return week;
  65. }


  66. // TC的模拟
  67. void CCalendar::gotoxy(short x, short y)
  68. {
  69.         HANDLE hOut;
  70.         hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  71.         COORD pos = {0,0};
  72.         pos.X = x;
  73.         pos.Y = y;
  74.         //SetConsoleTextAttribute(hOut, 0x01|0x05|FOREGROUND_INTENSITY );
  75.         SetConsoleCursorPosition(hOut, pos);
  76.         //CloseHandle(hOut);
  77. }


  78. // 日历的格式化
  79. void CCalendar::FormatDate(int FstWeek)
  80. {
  81.         int a[37];
  82.         int k = 1;
  83.         for (int i = 1;i < 37;i++)
  84.         {
  85.                 if ( i < FstWeek)
  86.                 {
  87.                         a[i-1] = 1
  88.                 }
  89.                 else
  90.                 {
  91.                         a[i-1] = k;
  92.                         k++;
  93.                 }
  94.         }
  95. }
复制代码


DateLi.cpp
  1. #include "stdafx.h"
  2. #include "Calendar.h"

  3. int _tmain(int argc, _TCHAR* argv[])
  4. {
  5.         int j,k;
  6.         CCalendar calendar;
  7.        

  8.         j = calendar.fallday(2010,12,3);
  9.         calendar.gotoxy(20,3);
  10.         k = calendar.week(2010);
  11.         k = j + k-1;
  12.         k = k % 7;
  13.         printf("%d",k);
  14.        
  15.         return 0;
  16. }
复制代码


  1. // stdafx.h : 标准系统包含文件的包含文件,
  2. // 或是经常使用但不常更改的
  3. // 特定于项目的包含文件
  4. //

  5. #pragma once

  6. #include "targetver.h"

  7. #include <stdio.h>
  8. #include <tchar.h>
  9. #include <Windows.h>


  10. // TODO: 在此处引用程序需要的其他头文件
复制代码
PYG19周年生日快乐!
  • TA的每日心情
    郁闷
    2017-1-19 11:18
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    发表于 2010-12-4 15:08:18 | 显示全部楼层
    你这析构函数没做事情可以不写的,让类调用默认的析构函数就可以了
    PYG19周年生日快乐!

    该用户从未签到

     楼主| 发表于 2010-12-4 15:18:04 | 显示全部楼层
    回复 2# komany


        谢谢ls,我把它删除了,我只是刚开始写的,忘记删除了~~
        这个其实是原来写的是c的版本,我把原来的函数弄成了类·~~还有个函数没写完·~写出来代码太多~~本身代码就很多~~·看看能简化不~~哈哈~~水平很菜,谢谢了
    PYG19周年生日快乐!
  • TA的每日心情
    郁闷
    2017-1-19 11:18
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    发表于 2010-12-4 21:35:04 | 显示全部楼层
    本帖最后由 komany 于 2010-12-4 21:38 编辑

    //sousce
    1. #include "Head.h"
    2. int main()
    3. {
    4. int month=0;
    5. Calendar a;
    6. long int year=0;
    7. system("cls");

    8. do

    9. {
    10. cout<<"\n\nPlease input one integer number as 'year'(0~3000):\n";

    11. cin>>year;

    12. if (year<0||year>3000) printf ("ERROR,please input again!");

    13. }

    14. while (year<0||year>3000);

    15. cout<<"\n\n";
    16. do

    17. {
    18. cout<<"please input the month(1~12)\n\n"<<endl;

    19. cin>>month;

    20. if (month<1||month>12) printf ("please input again!");

    21. }

    22. while (month<1|month>=12);
    23. //printf ("\n\n\n");
    24. cout<<endl;
    25. cout<<endl;
    26. cout<<endl;
    27. //printf("\t\t\t\t%ld\t%d\n\n",year,month);
    28. cout<<"\t"<<"\t"<<"\t"<<year<<"\t"<<"\t"<<month<<endl;
    29. cout<<"\n********************************************************************************\n"<<endl;

    30. cout<<"\t\t Sun\t Mon\t Tue\t Wen\t Thu\t Fri\t Sat"<<endl;
    31. a.Calendar::monthprint(year,month);
    32. printf("\n\n");
    33. return 0;
    34. }
    复制代码
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <iostream>
    4. using namespace std;
    复制代码
    1. #include "Headech.h"
    2. class Calendar
    3. {
    4. public:
    5. int leap_year(int y);
    6. int count_leap(long year);
    7. int monthdays(int month,long year);
    8. int monthprint(long year,int month);

    9. };
    10. int Calendar:: leap_year(int y)
    11. {
    12. return (y%4==0&&y%100!=0||y%400==0);
    13. }
    14. int Calendar:: count_leap(long year)

    15. {

    16. int i=0,j,min,max;

    17. if(year>2006) {min=2006;max=year;}

    18. else {min=year+1;max=2006;}

    19. for(j=min;j<max;j++)

    20. if(leap_year(j)) i++;

    21. return i;

    22. }
    23. int Calendar::monthdays(int month,long year)

    24. {

    25. int sum=0,i,j;

    26. if(year>=2006)

    27. { int t[12]={31,0,31,30,31,30,31,31,30,31,30,31};

    28. j=month-1;

    29. if(leap_year(year)) t[1]=29;

    30. else t[1]=28;

    31. for(i=0;i<j;i++)

    32. sum=sum+t[i];

    33. }

    34. else

    35. { int t1[12]={31,30,31,30,31,31,30,31,30,31,0,31};

    36. j=12-month;

    37. if(leap_year(year)) t1[10]=29;

    38. else t1[10]=28;

    39. for(i=0;i<=j;i++)

    40. sum=sum+t1[i];

    41. }
    42. return sum;
    43. }
    44. int Calendar::monthprint(long year,int month)

    45. {

    46. int t[12]={31,0,31,30,31,30,31,31,30,31,30,31};

    47. int i,y,weekday=0;

    48. long days=0;

    49. if(leap_year(year)) t[1]=29;

    50. else t[1]=28;

    51. y=t[month-1];

    52. if(year>2006)

    53. {

    54. days=(year-2006)*365+count_leap(year)+monthdays(month,year);

    55. weekday=days%7;

    56. }

    57. else if(year<2006)

    58. {
    59. days=(2005-year)*365+count_leap(year)+monthdays(month,year);

    60. weekday=7-days%7;

    61. }

    62. else

    63. {
    64. days=monthdays(month,year);weekday=days%7;
    65. }
    66. for (i=1;i<=weekday+2;i++)

    67. cout<<"\t";

    68. for (i=1;i<=y;i++)

    69. {
    70. if ((i+weekday-1)%7==0) printf ("\n\n\t\t%3d\t",i);

    71. else printf ("%3d\t",i);
    72. };

    73. cout<<"\n********************************************************************************\n\n"<<endl;
    74. return 0;
    75. }
    复制代码
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2010-12-6 10:12:45 | 显示全部楼层
    // TC的模拟

    void CCalendar::gotoxy(short x, short y)

    {

            HANDLE hOut;

            hOut = GetStdHandle(STD_OUTPUT_HANDLE);

            COORD pos = {0,0};

            pos.X = x;

            pos.Y = y;

            //SetConsoleTextAttribute(hOut, 0x01|0x05|FOREGROUND_INTENSITY );

            SetConsoleCursorPosition(hOut, pos);

            //CloseHandle(hOut);

    }

    这个的设计逻辑上 有待改进

    handle 从程序开始到结束 是不会变的
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2010-12-21 15:17:36 | 显示全部楼层
    学习中..很好的转变
    PYG19周年生日快乐!

    该用户从未签到

    发表于 2011-2-22 12:54:40 | 显示全部楼层

       

    叹一个...字数字数字数字数












    燃文
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

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