| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 687 人关注过本帖
标题:时间设置程序,关于所用到的句柄类问题
只看楼主 加入收藏
大葱
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-12-21
收藏
 问题点数:0 回复次数:3 
时间设置程序,关于所用到的句柄类问题
我在下面时间设置程序中用到句柄类,但最后在main函数中创建对象时,当在一个块中创建,即一起创建多个对象,输出的总是最后一个对象的数据,前面的被覆盖了!各位帮忙看一下,谢谢了
#include <ctime>
#include <iostream>
using namespace std;

class Date {

private:
    struct tm* timer;
public:
    Date();
    Date(int DDD,int YYYY);
    Date(int MM,int DD,int YY);
    Date(char* MM,int DD,int YY);
    ~Date();
    print1();
    print2();
    print3();
};

Date :: ~Date() {}

Date :: Date() {

    time_t ti = time(0);
    timer = localtime(&ti);
}

Date :: Date(int DDD,int YYYY) {

    time_t ti = time(0);
    timer = localtime(&ti);
    timer->tm_mon = 0;
    timer->tm_year = YYYY - 1900;
    timer->tm_yday = DDD - 1;
    for (int i=1;i <= 12;i++) {
    
        if(i == 2) {
        
            if ((YYYY % 4 == 0 && YYYY % 100 != 0) || (YYYY % 400 == 0)){

                if(DDD <= 29) {    
                    timer->tm_mday = DDD;
                    break;
                }else{
                    DDD -= 29;
                    timer->tm_mon++;
                }
            }else{
            
                if(DDD <= 28) {
                    timer->tm_mday = DDD;
                    break;
                }else{
                    DDD -= 28;
                    timer->tm_mon++;
                }
            }
        }else if(i==4 || i==6 || i==9 || i==11) {
        
            if(DDD <= 30) {
                timer->tm_mday = DDD;
                break;
            }else {
            
                DDD -= 30;
                timer->tm_mon++;
            }
        }else {
        
            if(DDD <= 31) {
                timer->tm_mday = DDD;
                break;
            }else {
            
                DDD -= 31;
                timer->tm_mon++;
            }
        }
    }
}

Date :: Date(int MM,int DD,int YY) {

    time_t ti = time(0);
    timer = localtime(&ti);
    timer->tm_mday = DD;
    timer->tm_mon = MM - 1;
    timer->tm_year = YY - 1900;
    timer->tm_yday = -1;
}

Date :: Date(char* MM,int DD,int YY) {

    time_t ti = time(0);
    timer = localtime(&ti);
    timer->tm_year = YY -1900;
    timer->tm_mday = DD;
    timer->tm_yday = -1;
    char* month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    for(int i=0;i <= 12;i++)
        if(!strcmp(MM,month[i])) {
            timer->tm_mon = i;
            break;
        }
}

Date :: print1() {

    if(timer->tm_yday == -1) {
    
        int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
        for(int i=0;i < timer->tm_mon;i++) timer->tm_yday += a[i];
        if((i > 1) && ((timer->tm_year % 4 == 0 && timer->tm_year % 100 != 0) || (timer->tm_year % 400 == 0)))
            timer->tm_yday += (timer->tm_mday + 1);
        else timer->tm_yday += timer->tm_mday;
    }

    char str[20];
    strftime(str,20,"%j  %Y\n",timer);
    cout << str;
}

Date :: print2() {

    char str[20];
    strftime(str,20,"%m\/%d\/%Y\n",timer);
    cout << str;
}

Date :: print3() {

    char str[20];
    strftime(str,20,"%B  %d, %Y\n",timer);
    cout << str;
}
int main(void) {

    cout << "The system time :" << endl;
    Date date1;
    date1.print1();
    date1.print2();
    date1.print3();
    cout << endl;

    Date date2(108,2008);
    date2.print1();
    date2.print2();
    date2.print3();
    cout << endl;

    Date date3(4,17,2008);
    date3.print1();
    date3.print2();
    date3.print3();
    cout << endl;

    Date date4("April",17,2008);
    date4.print1();
    date4.print2();
    date4.print3();
    cout << endl;


    Date date5(61,1640);
    date5.print1();
    date5.print2();
    date5.print3();
    cout << endl;

    Date date6(3,1,1640);
    date6.print1();
    date6.print2();
    date6.print3();
    cout << endl;

    Date date7("March",1,1640);
    Date date8(89,1341);
    Date date9(3,23,1648);
    Date date10("March",1,2341);
    date7.print1();
    date7.print2();
    date7.print3();
    cout << endl;

    //Date date8(60,1641);
    date8.print1();
    date8.print2();
    date8.print3();
    cout << endl;

    //Date date9(3,1,1641);
    date9.print1();
    date9.print2();
    date9.print3();
    cout << endl;

    //Date date10("March",1,1641);
    date10.print1();
    date10.print2();
    date10.print3();
    cout << endl;

    return 0;
}

最后
搜索更多相关主题的帖子: 句柄类 Date int include 
2008-04-20 17:12
ging450
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-4-23
收藏
得分:0 
好难。
2008-04-24 19:18
kyln
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2008-4-21
收藏
得分:0 
这个估计与localtime函数的特性有关.
gmtime, mktime, and localtime all use a single statically allocated tm structure for the conversion. Each call to one of these routines destroys the result of the previous call.(摘自MSDN 98)
每次调用localtime,都会销毁以前调用的结果.
2008-04-25 09:19
大葱
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-12-21
收藏
得分:0 
哦,多谢指教,哈哈
2008-04-25 19:36
快速回复:时间设置程序,关于所用到的句柄类问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052418 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved