| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1351 人关注过本帖
标题:求助,各位大佬帮忙看看怎么修改,总是报错
只看楼主 加入收藏
星i辰
Rank: 2
等 级:论坛游民
威 望:1
帖 子:35
专家分:18
注 册:2016-11-1
结帖率:80%
收藏
已结贴  问题点数:15 回复次数:4 
求助,各位大佬帮忙看看怎么修改,总是报错
一个Time类,需要重载构造函数,并且进行加减运算,

Time.h:
#ifndef TIME_H
#define TIME_H
class Time
{
    int hour;
    int minute;
    int second;
public:
   
    ~Time(void);
    Time();
    Time(int,int,int);
    void init(int,int,int);
    void plus(Time& t);
    void minus(Time& t);
    void print_hms();
};
#endif;


Time.cpp:

#include "StdAfx.h"
#include "Time.h"
#include <iostream>
using namespace std;



Time::~Time(void)
{
}

Time::Time(int h,int m,int s)
{
    init(h,m,s);
}
Time::Time()
{
    hour=12;
    minute=30;
    second=30;
}
void Time::init(int h,int m,int s)
{
    second=s%60;
    minute=(m+s/60)%60;
    hour=(h+m/60)%24;
}
void Time::Plus(Time& t)
{
    hour=hour+t.hour;
    minute=minute+t.minute;
    second=second+t.second;
    init(hour,minute,second);
}
void Time::minus(Time& t)
{
    int sum=(second+60*minute+3600*hour)-(t.second+60*t.minute+3600*t.hour);
    second=sum%60;
    minute=(sum/60)%60;
    hour=(sum/3600)%24;
    init(hour,minute,second);
}
void Time::print_hms()
{
    cout<<hour<<"-"<<minute<<"-"<<second<<endl;
}


ttt2:

#include "stdafx.h"
#include <iostream>
#include "Time.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    Time time1,time2(11,22,33);
    int h,m,s,h2,m2,s2;
    cout<<"按照时分秒输出:\n";
    time1.print_hms();
    time2.print_hms();
    cout<<"请输入时间(时、分、秒):\n";
    cin>>h>>m>>s;
    time1.init(h,m,s);
    cout<<"请输入时间为:\n";
    cout<<"时分秒:";
    time1.print_hms();
    cout<<"请输入第二个时间:";
    cin>>h2>>m2>>s2;
    time2.init(h2,m2,s2);
    cout<<"请选择运算类型:";
    char i;
    cin>>i;
    cout<<"运算结果为:";
    switch(i)
    {
    case '+':
        {
            time1.plus(time2);
            time1.print_hms();
            break;
        }
    case '-':
        {
            time1.minus(time2);
            time1.print_hms();
            break;
        }
    default:
        cout<<"ERROR!!\n";
    }

    return 0;
}

下面是报错

图片附件: 游客没有浏览图片的权限,请 登录注册




各位大佬求助,怎么搞??
搜索更多相关主题的帖子: Time int minute second void 
2018-04-27 16:21
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:15 
除了 void Time::Plus(Time& t) 应该是 void Time::plus(Time& t) 外,其它能编译通过(当然,这代码烂到可以开除了)。
2018-04-27 16:54
星i辰
Rank: 2
等 级:论坛游民
威 望:1
帖 子:35
专家分:18
注 册:2016-11-1
收藏
得分:0 
我还是新手嘛,大佬这么打击我幼小的心灵··
2018-04-27 17:13
星i辰
Rank: 2
等 级:论坛游民
威 望:1
帖 子:35
专家分:18
注 册:2016-11-1
收藏
得分:0 
不过,谢谢了
2018-04-27 17:14
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
程序代码:
#include <iostream>
#include <iomanip>

class Time
{
public:
    Time() : second_()
    {
    }
    Time( unsigned hour, unsigned minute, unsigned second )
        : second_((hour%24*3600+minute%1440*60+second%86400)%86400)
    {
    }
    Time& operator+=( const Time& t )
    {
        second_ = (second_%86400+t.second_%86400)%86400;
        return *this;
    }
    Time& operator-=( const Time& t )
    {
        second_ = (86400+second_%86400-t.second_%86400)%86400;
        return *this;
    }
    friend std::istream& operator>>( std::istream& is, Time& t )
    {
        unsigned hour, minute, second;
        char interval1, interval2;
        is >> hour
           >> std::ws >> interval1
           >> minute
           >> std::ws >> interval2
           >> second;
        if( !is )
            return is;
        if( interval1!=':' || interval2!=':' )
        {
            is.setstate( std::ios::failbit );
            return is;
        }
        t = Time( hour, minute, second );
        return is;
    }
    friend std::ostream& operator<<( std::ostream& os, const Time& t )
    {
        char old_fill_char = os.fill('0');
        os << std::setw(2) << t.second_%86400/3600 << ':'
           << std::setw(2) << t.second_%3600/60 << ':'
           << std::setw(2) << t.second_%60;
        os.fill( old_fill_char );
        return os;
    }

private:
    unsigned second_;
};

using namespace std;

int main( void )
{
    Time time1;
    Time time2( 11, 22, 33 );

    cout << time1 << '\n';
    cout << time2 << '\n';

    cin >> time1;
    cout << time1 << '\n';

    cin >> time2;
    cout << time2 << '\n';

    char opt;
    cin >> std::ws >> opt;
    switch( opt )
    {
    case '+': time1+=time2; break;
    case '-': time1-=time2; break;
    default:  cout << "ERROR!\n";
    }
    cout << time1 << '\n';

    return 0;
}

输出 00:00:00
输出 11:22:33

输入 1:2:3
输出 01:02:03

输入 55:66:77
输出 08:07:17

输入 -
输出 16:54:46

2018-04-28 09:00
快速回复:求助,各位大佬帮忙看看怎么修改,总是报错
数据加载中...
 
   



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

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