MagicforestA 2022-07-09 12:34 采纳率: 100%
浏览 41
已结题

python 时间计算器的问题

下面是自己写的一段时间计算的代码,要求相加,相减,和乘法计算
output如下要求:
the first time interval (fti) is hours=21, minutes=58, seconds=50
the second time interval (sti) is hours=1, minutes=45, seconds=22
the expected result of addition (fti + sti) is 23:44:12
the expected result of subtraction (fti - sti) is 20:13:28
the expected result of multiplication (fti * 2) is 43:57:40
然后问题来了,根据题目要求要用到__str__(self) method, 我现在这段代码只能output后三行,像第一和第二行怎么用__str__来执行?我试过但只能print出一行,还有最后一个乘法的小时怎么变成非标准时间格式,因为用的时间格式都是"%H:%M:%S",所以最后一行小时的显示达不到要求


```python
from datetime import time, datetime
class time_interval:
    def __init__(self,hours,minutes,seconds):
        self.hours=hours
        self.minutes=minutes
        self.seconds=seconds
    #def __str__(self):
     #   return "the first time interval (fti) is hours=%d, minutes=%d, seconds=%d"%(self.hours,self.minutes,self.seconds)+"
    def __add__(self,others):
        x=self.seconds+others.seconds
        y=self.minutes+others.minutes
        z=self.hours+others.hours
        if x > 59:
            x = x-60
            y += 1
            if y > 59:
                y = y-60
                z += 1
                if z > 23:
                    z = z-24
        if y > 59:
            y = y-60
            z += 1
            if z > 23:
                z=z-24
        if z > 23:
            z=z-24
        return "the expected result of addition (fti+sti) is "+time(z,y,x).strftime("%H:%M:%S")
    def __sub__(self,others):
        x=self.seconds-others.seconds
        y=self.minutes-others.minutes
        z=self.hours-others.hours
        if x < 0:
            x = 60+x
            y -= 1
            if y < 0:
                y = 60+y
                z -= 1
                if z < 0:
                    z = 24+z
        if y < 0:
            y = 60+y
            z -= 1
            if z < 0:
                z=24+z
        if z < 0:
            z=z+24
        return "the expected result of subtraction (fti-sti) is "+time(z,y,x).strftime("%H:%M:%S")
    def __mul__(self,var):
        x=self.seconds*var
        y=self.minutes*var
        z=self.hours*var
        if x > 59:
            x = x-60
            y += 1
            if y > 59:
                y = y-60
                z += 1
                if z > 23:
                    z = z-24
        if y > 59:
            y = y-60
            z += 1
            if z > 23:
                z=z-24
        if z > 23:
            z=z-24
        return "the expected result of multiplication (fti*2) is "+time(z,y,x).strftime("%H:%M:%S")
p1=time_interval(21,58,50)
p2=time_interval(1,45,22)
print(p1.__add__(p2))
print(p1.__sub__(p2))
print(p1.__mul__(2))


```

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-07-09 12:45
    关注

    可以用静态属性记录实例化了多少个实例,然后建立一个数组来得到数字对应的英文内容
    输出非标准时间,直接输出小时,分钟和秒就行,而且注意小时不要添加大于23的天进位操作,直接累加就行

    
    '''
    下面是自己写的一段时间计算的代码,要求相加,相减,和乘法计算
    output如下要求:
    the first time interval (fti) is hours=21, minutes=58, seconds=50
    the second time interval (sti) is hours=1, minutes=45, seconds=22
    the expected result of addition (fti + sti) is 23:44:12
    the expected result of subtraction (fti - sti) is 20:13:28
    the expected result of multiplication (fti * 2) is 43:57:40
    然后问题来了,根据题目要求要用到__str__(self) method, 我现在这段代码只能output后三行,像第一和第二行怎么用__str__来执行?
    我试过但只能print出一行,还有最后一个乘法的小时怎么变成非标准时间格式,因为用的时间格式都是"%H:%M:%S",所以最后一行小时的显示达不到要求
     '''
     
    
    from datetime import time, datetime
    class time_interval:
        instancenum=0
        chrs=['first','second']#更多数组对照英文增加这里的数组
        def __init__(self,hours,minutes,seconds):
            self.hours=hours
            self.minutes=minutes
            self.seconds=seconds
            print(self.__str__())
            time_interval.instancenum+=1
        def __str__(self):
            #(time_interval.chrs[time_interval.instancenum%len(time_interval.chrs)] 这里意思是超过chrs长度后从头开始取对应
            return "the %s time interval (fti) is hours=%d, minutes=%d, seconds=%d"%(time_interval.chrs[time_interval.instancenum%len(time_interval.chrs)],self.hours,self.minutes,self.seconds)
        def __add__(self,others):
            x=self.seconds+others.seconds
            y=self.minutes+others.minutes
            z=self.hours+others.hours
            if x > 59:
                x = x-60
                y += 1
                if y > 59:
                    y = y-60
                    z += 1
                    if z > 23:
                        z = z-24
            if y > 59:
                y = y-60
                z += 1
                if z > 23:
                    z=z-24
            if z > 23:
                z=z-24
            return "the expected result of addition (fti+sti) is "+time(z,y,x).strftime("%H:%M:%S")
        def __sub__(self,others):
            x=self.seconds-others.seconds
            y=self.minutes-others.minutes
            z=self.hours-others.hours
            if x < 0:
                x = 60+x
                y -= 1
                if y < 0:
                    y = 60+y
                    z -= 1
                    if z < 0:
                        z = 24+z
            if y < 0:
                y = 60+y
                z -= 1
                if z < 0:
                    z=24+z
            if z < 0:
                z=z+24
            return "the expected result of subtraction (fti-sti) is "+time(z,y,x).strftime("%H:%M:%S")
        def __mul__(self,var):
            x=self.seconds*2
            y=self.minutes*2
            z=self.hours*2
            if x > 59:
                x = x-60
                y += 1
                if y > 59:
                    y = y-60
                    z += 1
                    #if z > 23:
                    #    z = z-24
            if y > 59:
                y = y-60
                z += 1
                #if z > 23:
                #    z=z-24
            #if z > 23:
            #    z=z-24
            return "the expected result of multiplication (fti*2) is %d:%d:%d"%(z,y,x)#.strftime("%H:%M:%S")
    
    p1=time_interval(21,58,50)
    p2=time_interval(1,45,22)
    p3=time_interval(1,45,22)
    print(p1.__add__(p2))
    print(p1.__sub__(p2))
    print(p1.__mul__(2))
     
     
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月17日
  • 已采纳回答 7月9日
  • 修改了问题 7月9日
  • 修改了问题 7月9日
  • 展开全部

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。