麻烦你帮我改一下我的程序,谢谢 - 爱问答

(爱问答)

麻烦你帮我改一下我的程序,谢谢

/*5.设计一个时间类Time,要求:

(1)包含时(hour)、分(minute)和秒(second)私有数据成员。

(2)包含构造函数,重载关于一时间加上另一时间的加法运算符+、

重载关于一时间减去另一时间的减加运算符-、重载输出运算符<<与输入运算符>>等。*/

#include <iostream>

using namespace std;

class Time

{

 private:

  int hour;

  int minute;

  int second;

 public:

  Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}

  void SetHour(int h){hour=h;}

  void SetMinute(int m){minute=m;}

  void SetSecond(int s){second=s;}

  int GetHour() const {return hour;}

  int GetMinute() const {return minute;}

  int GetSecond() const {return second;}

  Time operator+(int seconds);

  Time operator-(int seconds);

  //static int GetHourSeconds(int h);

  //static int GetMinuteSeconds(const Time &s);

  static int TimeToNum(const Time &s);

  static Time NumToSecond(int n);

};

  ostream &operator << (ostream &out,const Time &s);

  istream &operator >> (istream &in,Time &s);

  Time Time::operator+(int seconds)

  {

      int n=TimeToNum(*this) + seconds;

      hour=seconds/3600;

      minute=(seconds-hour*3600)/60;

      second=seconds%60;

      return NumToTime(n);

  }

   Time Time::operator-(int seconds)

  {

      int n=TimeToNum(*this) - seconds;

      hour=seconds/3600;

      minute=(seconds-hour*3600)/60;

      seconds=seconds%60;

      return NumToTime(n);

  }

  int Time::TimeToNum(const Time &s)

  {

      int h,n=0;

      for(h=1;h<s.GetHour();h++)

         n+=GetHourSeconds(h);

      for(int m=1;m<s.GetMinute();m++)

         n+=GetMinuteSeconds(Time(h,m,1));

      n+=s.GetSecond();

      return n;

  }

  Time Time::NumToTime(int n)

  {

      int h,m,s,rest=n;

      for(h=1,rest=n;rest>GetHourSeconds(h);h++)

         rest-=GetHourSeconds(h);

      for(m=1;rest>GetMinuteSeconds(Time(h,m,1));m++)

         rest-=GetMinuteSeconds(Time(h,m,1));

      s=rest;

      return Time(h,m,s);

  }

  ostream &operator << (ostream &out,const Time &s)

  {

      out<<s.GetHour()<<"时"<<s.GetMinute()<<"分"<<s.GetSecond()<<"秒";

      return out;

  }

  istream &operator >> (istream &in,Time &s)

  {

      int hour,minute,second;

      cin>>hour>>minute>>second;

      s=Time(hour,minute,second);

      return in;

  }

  int main(void)

  {

    Time s(8,0,0);

    cout<<"时间:"<<s<<endl;

    cout<<"时间+10:"<<s+10<<endl;

    cout<<"时间-10:"<<s-10<<endl;

    cout<<"输入时间:";

    cin<<"时间:"<<s<<endl;

    system("PAUSE");

    return 0;

  }


程序错误较多,不知你原来的程序基础是什么

对一般程序员,改别人的程序比自己重新写一个更加复杂

(别人写了片断,你要拼起来,还要理解别人的思路)

所以你的这段最是要你自己修改

几个问题(语法上的)

一.NumToTime没有声明

在你的类中要加一个

二.你的二个注释掉的声明是用到的(为什么要注释掉?)

 static int GetHourSeconds(int h);

  static int GetMinuteSeconds(const Time &s);

三  cin<<"时间:"<<s<<endl; //不可能输入常量的

这个应该是你手误了,应该是输出的,也就是

cout<<"时间:"<<s<<endl; 

这样语法上没有问题了,只是你的

 static int GetHourSeconds(int h) ;

  static int GetMinuteSeconds(const Time &s) ;

两个函数没有实现.我没有分析这两个函数的意义


这个是需要付费的

相关标签:谢谢

下一篇:因为想耍地下城与勇士游戏,所以想把电脑主机升级

上一篇:求大神解答,怎么装和拆显卡,需要详细步骤!

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?