标题:华为又出变态面试题!大家请看以下代码 在看懂的同时 修改错误!
只看楼主
单调黑白
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-10-4
 问题点数:0 回复次数:33 
华为又出变态面试题!大家请看以下代码 在看懂的同时 修改错误!
#include<iostream>
using namespace std;
#include <ctime>

class CLF_DateTime
{
private:

    struct tm *local;

public:
    CLF_DateTime(int year, int month, int day,int hour, int min,int sec);

    CLF_DateTime(CLF_DateTime &x);
    
    CLF_DateTime()
    {
        
    }    
    ~CLF_DateTime(void);
    void display(char * strdisplay);
    void setdisplay(char * setstrdisplay);
    int compare(CLF_DateTime* pa, CLF_DateTime* pb);

};

CLF_DateTime::CLF_DateTime(CLF_DateTime &x)
    {
        local = new tm;
        local->tm_year =x.local->tm_year;//struct tm中定义的年成员tm_year是以1900为基数的,也就是说他显示2007时tm_year值并不是2007
        local->tm_mon = x.local->tm_mon;//月份可见definition里有说明是[0,11]所以比现实小了一个月
        local->tm_mday = x.local->tm_mday;
        local->tm_hour =x.local->tm_hour;
        local->tm_min =x.local->tm_min;
        local->tm_sec =x.local->tm_sec;
        
    }
CLF_DateTime::CLF_DateTime(int year, int month, int day,int hour, int min,int sec)
    {
        local = new tm;
        local->tm_year = year-1900;//struct tm中定义的年成员tm_year是以1900为基数的,也就是说他显示2007时tm_year值并不是2007
        local->tm_mon = month-1;//月份可见definition里有说明是[0,11]所以比现实小了一个月
        local->tm_mday = day;
        local->tm_hour =hour;
        local->tm_min =min;
        local->tm_sec =sec;
   
    }

CLF_DateTime::~CLF_DateTime(void)
    {
        delete(local);
        local = NULL;
    }

void CLF_DateTime::display(char * strdisplay)
    {
        
        strftime(strdisplay,80,"%Y-%m-%d %H:%M:%S",local);
        

    }
void CLF_DateTime::setdisplay(char * setstrdisplay)
    {
        if(local->tm_sec<30&&local->tm_sec>=0)
        {   
            local->tm_sec=0;
            strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
        }
   
        else if (local->tm_sec>=30&&local->tm_sec<=60)
        {
            local->tm_min+=1;
            local->tm_sec=0;
            strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
        }
        if (local->tm_min>=60)
        {
            local->tm_hour+=1;
            local->tm_min=0;
            strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
        }
        if (local->tm_hour>=24)
        {
            local->tm_mday+=1;
            local->tm_hour=0;
            strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
    
        }
        if (local->tm_mon==1||local->tm_mon==3||local->tm_mon==5||local->tm_mon==7||local->tm_mon==8||local->tm_mon==10||local->tm_mon==12)
        {
            if(local->tm_mday>=31)
            {
                local->tm_mon+=1;
                local->tm_mday=1;
                strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
            
            }
        }
        else if (local->tm_mon==4||local->tm_mon==6||local->tm_mon==9||local->tm_mon==11)
        {
            if(local->tm_mday>=30)
            {
                local->tm_mon+=1;
                local->tm_mday=1;
                strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
                
            }
        }
        else if (local->tm_mon==2&&(local->tm_year%400==0||(local->tm_year%4==0&&local->tm_year%100!==0)))
        {
            if(local->tm_mday>=29)
            {
                local->tm_mon+=1;
                local->tm_mday=1;
                strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
                
            }
        }
        else if (local->tm_mon=2)
        {
            if(local->tm_mday>=28)
            {
                local->tm_mon+=1;
                local->tm_mday=1;
                strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
                
            }
        }
        if (local->tm_mon>=12)
        {
            local->tm_year+=1;
            local->tm_mon=1;
            strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
        
            
        }


int CLF_DateTime::compare(CLF_DateTime* pa, CLF_DateTime* pb)
    {
        
            if(pa.local->tm_year>pb.local->tm_year)
            
            {
                cout<<"1"<<endl;
            }
            else if(pa.local->tm_year<pb.local->tm_year)
            {
                cout<<"-1"<<endl;
            }
            else if(pa.local->tm_yaer==pb.local->tm_year)
            {
                if (pa.local->tm_mon>pb.local->tm_mon)
                {
                    cout<<"1"<<endl;
                }
                else if(pa.local->tm_mon<pb.local->tm_mon)
                {
                    cout<<"-1"<<endl;
                }
                else if(pa.local->tm_mon==pb.local->tm_mon)
                {
                    if(pa.local->tm_mday>pb.local->tm_mday)
                    {
                        cout<<"1"<<endl;
                    }
                    else if(pa.local->tm_mday<pb.local->tm_mday)
                    {
                        cout<<"-1"<<endl;
                    }
                    else if(pa.local->tm_mday==pb.local->tm_mday)
                    {
                        if(pa.local->tm_hour>pb.local->tm_hour)
                        {
                            cout<<"1"<<endl;
                        }
                        else if(pa.local->tm_hour<pb.local->tm_hour)
                            {
                                cout<<"-1"<<endl;
                            }
                        else if(pa.local->tm_hour==pb.local->tm_hour)
                        {
                            if(pa.local->tm_min>pb.local->tm_min)
                            {
                                cout<<"1"<<endl;
                            }
                            else if(pa.local->tm_min<pb.local->tm_min)
                            {
                                cout<<"-1"<<endl;
                            }
                            else if (pa.local->tm_min==pb.local->tm_min)
                            {
                                if(pa.local->tm_sec!=0)
                                {
                                    cout<<"1"<<endl;
                                }
                                else  
                                {
                                    cout<<"0"<<endl;
                                }
                            }
                
                
    }

void main()
{
    CLF_DateTime pa(2007,11,8,14,52,10);
    CLF_DateTime pb(pa);
    char testdisplay[100];

    cout<<"Your input date and time is: ";
    pa.display(testdisplay);
    cout<<strdisplay<<endl;
    cout<<"Provide rounding:";
    pa.setdisplay(testdisplay);
    cout <<setstrdisplay<<endl;
    cout<<"compare time:";
    (pb);
}
搜索更多相关主题的帖子: 华为 代码 面试 
2007-12-11 14:45
孤魂居士
Rank: 2
来 自:老A(中国地大)
等 级:论坛游民
威 望:4
帖 子:1142
专家分:18
注 册:2007-5-21
得分:0 
面试题目是这种
我看我就回去种地算了

准备用3年做个高级软件工程师 10年也做不成。准备用10年做成高级软件工程师 3年就成了QQ 群 45771086
欢迎版主...欢迎JAVA爱好者...
一起从深夜 到凌晨...
2007-12-12 01:30
MilesWang
Rank: 1
来 自:西安
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-12-5
得分:0 
这几个else if 就能把人搞晕!
2007-12-12 23:39
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
得分:0 
没仔细看,但是有个最明显的错误就是拷贝函数的设计~

樱花大战,  有爱.
2007-12-13 08:06
风之梦
Rank: 1
等 级:新手上路
帖 子:59
专家分:0
注 册:2007-8-31
得分:0 
值行一看呀,回去研究一下
2007-12-13 17:26
beyond0702
Rank: 1
来 自: 桂 林
等 级:新手上路
帖 子:219
专家分:0
注 册:2007-11-17
得分:0 
这真是面试题吗,真的话,俺可同意2#说的
2007-12-13 18:16
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
得分:0 
这个程序挺简单的啊,哪里变态了??
同意4楼,拷贝函数也有问题……而且
int CLF_DateTime::compare(CLF_DateTime* pa, CLF_DateTime* pb)
这个函数太失败了……效率低下语句啰嗦……
诸如打字错误和括号匹配方面的问题就不说了,整个程序使用动态内存分配就是个错误。
两百多行代码,其实在五十行内就完全可以搞定……
这是我的实现:
程序代码:
 #include <iostream>
using namespace std;
#include <ctime>

class CLF_DateTime : public tm {
    inline void carry(int& v1,int& v2,int v3){v1+=v2/v3;v2%=v3;}
public:
    CLF_DateTime(int year=1900,int month=1,int day=1,
        int hour=0,int min=0,int sec=0) {
            tm_year=year;tm_mon=month;tm_mday=day;
            tm_hour=hour;tm_min=min;tm_sec=sec;
    }
    ~CLF_DateTime(){}
    char* display(char* strdisplay) {
        strftime(strdisplay,80,"%Y-%m-%d %H:%M:%S",this);
        return strdisplay;
    }
    char* setdisplay(char* strdisplay) {
        tm_min+=tm_sec>=30; tm_sec=0;
        carry(tm_hour,tm_min,60);
        carry(tm_mday,tm_hour,24);
        switch (tm_mon) {
        case 1:case 3:case 5:case 7:case 8:
        case 10:case 12:carry(tm_mon,tm_mday,31);break;
        case 2:carry(tm_mon,tm_mday,28);break;
        default:carry(tm_mon,tm_mday,30);            
        }
        carry(tm_year,tm_mon,12);
        return display(strdisplay);
    }
    int compare(CLF_DateTime& pb) {
        int t;
        if (t=tm_year-pb.tm_year)return t>0?1:-1;
        if (t=tm_mon-pb.tm_mon)return t>0?1:-1;
        if (t=tm_mday-pb.tm_mday)return t>0?1:-1;
        if (t=tm_hour-pb.tm_hour)return t>0?1:-1;
        if (t=tm_min-pb.tm_min)return t>0?1:-1;
        return t;
    }
};

void main() {
    CLF_DateTime pa(2007,11,8,23,59,59);
    CLF_DateTime pb(pa);
    char testdisplay[100];

    cout<<"Your input date and time is: ";
    cout<<pa.display(testdisplay)<<endl;
    cout<<"Provide rounding:"<<pa.setdisplay(testdisplay)<<endl;
    cout<<"compare time:"<<(pb)<<endl;
    getchar();
}  


[[italic] 本帖最后由 StarWing83 于 2007-12-13 23:40 编辑 [/italic]]

专心编程………
飞燕算法初级群:3996098
我的Blog
2007-12-13 21:20
孤魂居士
Rank: 2
来 自:老A(中国地大)
等 级:论坛游民
威 望:4
帖 子:1142
专家分:18
注 册:2007-5-21
得分:0 

自惭形秽

准备用3年做个高级软件工程师 10年也做不成。准备用10年做成高级软件工程师 3年就成了QQ 群 45771086
欢迎版主...欢迎JAVA爱好者...
一起从深夜 到凌晨...
2007-12-15 00:51
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
得分:0 
路过...............

樱花大战,  有爱.
2007-12-15 12:30
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
得分:0 
要别人帮你改错就直接说,扯上华为干什么

一看就知道是假的,华为的题目会写得这么烂?

从BFS(Breadth First Study)到DFS(Depth First Study)
2007-12-16 14:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-191785-1-1.html




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

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