小甲鱼 发表于 2024-12-31 11:54:46

已有 1 人购买  本主题需向作者支付 5 鱼币 才能浏览 购买主题

不二如是 发表于 2024-12-31 12:21:35

{:10_256:}{:10_256:}第一

tommyyu 发表于 2024-12-31 18:02:57

from decimal import Decimal

exchange_rate = {
    "CNYUSD": Decimal(7.299),
    "USDCNY": Decimal(0.137),
    "CNYEUR": Decimal(7.625),
    "EURCNY": Decimal(0.131),
    "CNYJPY": Decimal(0.046),
    "JPYCNY": Decimal(21.59),
}

class Money:
    def __init__(self, value=0, kind='CNY'):
      if kind not in ['CNY', 'USD', 'JPY']:
            raise TypeError('请输入正确货币类型!')
      self.value = Decimal(value)
      self.kind= kind

    def exchange(self, kind):
      if kind not in ['CNY', 'USD', 'JPY']:
            raise TypeError('请输入正确货币类型!')
      return Money(value = self.value*exchange_rate, kind = kind)

    def __add__(self, other):
      if self.kind == other.kind:
            return Money(self.value+other.value, self.kind)
      return self+other.exchange(self.kind)

    def __sub__(self, other):
      if self.kind == other.kind:
            return Money(self.value-other.value, self.kind)
      return self+other.exchange(self.kind)

    def __str__(self):
      return str(float(self.value)) + ' ' + self.kind

某一个“天” 发表于 2024-12-31 21:18:40

{:10_256:}

某一个“天” 发表于 2024-12-31 21:19:35

加上爬虫获取实时汇率

岳军任 发表于 2025-1-1 20:04:39

11
页: [1]
查看完整版本: 【II】实现一个简单的货币运算系统