weixin_51790202 2021-12-25 14:28 采纳率: 88.2%
浏览 218
已结题

新型计算器 这个怎么做

题目:设计一个计算器,实现一个三维向量的加法,减法以及向量和标量的乘法和除法运算
提示:
1、定义类名为 VecCal,设计构造函数创建三维向量对象: def init(self, x=0,y=0,z=0) 用x,y,z指代三个维度的值

2、重写加法(+),减法(-),乘法(* )和整除除法(//)运算,实现向量的加减乘除

3、除法运算作异常处理,当输入标量数字是0时,除法结果为 (0,0,0)

加法示例:

def add(self, n): # 加法
result = VecCal() # 定义结果变量,也是一个三维向量,通过构造函数创建
result.X = self.X + n.X
result.Y = self.Y + n.Y
result.Z = self.Z + n.Z
return result # 返回 执行加法运算后的向量结果
输入格式:
第一行输入一个三维向量,逗号分隔,如:1,2,3

第二行输入另一个三维向量,逗号分隔:如:4,5,6

第三行输入一个数字, 如:3

输出格式:
(1, 2, 3) + (4, 5, 6) = (5, 7, 9)

(1, 2, 3) - (4, 5, 6) = (-3, -3, -3)

(1, 2, 3) * 3 = (3, 6, 9)

(1, 2, 3) / 3 = (0, 0, 1)

输入样例:
在这里给出一组输入。例如:

1,2,3
4,5,6
3
结尾无空行
输出样例:
在这里给出相应的输出。例如:

(1, 2, 3) + (4, 5, 6) = (5, 7, 9)
(1, 2, 3) - (4, 5, 6) = (-3, -3, -3)
(1, 2, 3) * 3 = (3, 6, 9)
(1, 2, 3) / 3 = (0, 0, 1)
结尾无空行

  • 写回答

1条回答 默认 最新

  • 鸡蛋酱$ 2021-12-25 16:28
    关注
    
    class VecCal:
        def __init__(self, x=0, y=0, z=0):
            self.x = x
            self.y = y
            self.z = z
    
        def add(self, n):  # 加法
            result = VecCal()
            result.x = self.x + n.x
            result.y = self.y + n.y
            result.z = self.z + n.z
            return result
    
        def subduction(self, n):  # 减法
            result = VecCal()
            result.x = self.x - n.x
            result.y = self.y - n.y
            result.z = self.z - n.z
            return result
    
        def multiplication(self, number):  # 乘法
            result = VecCal()
            result.x = self.x * number
            result.y = self.y * number
            result.z = self.z * number
            return result
    
        def division(self, number):  # 除法
            result = VecCal()
            result.x = self.x // number
            result.y = self.y // number
            result.z = self.z // number
            return result
    
        def display(self):
            return f'({self.x}, {self.y}, {self.z})'
    
    
    if __name__ == '__main__':
        data1 = [eval(i) for i in input("第一行输入一个三维向量,逗号(英文)分隔:").split(',')]
        data2 = [eval(i) for i in input("第二行输入另一个三维向量,逗号(英文)分隔:").split(',')]
        num = eval(input("请输入一个数字:"))
        the_VecCal = VecCal(data1[0], data1[1], data1[2])
        the_n = VecCal(data2[0], data2[1], data2[2])
        add = the_VecCal.add(n=the_n)
        subduction = the_VecCal.subduction(n=the_n)
        multiplication = the_VecCal.multiplication(number=num)
        division = the_VecCal.division(number=num)
        print("{0} + {1} = {2}".format(the_VecCal.display(), the_n.display(), add.display()))
        print("{0} - {1} = {2}".format(the_VecCal.display(), the_n.display(), subduction.display()))
        print("{0} * {1} = {2}".format(the_VecCal.display(), num, multiplication.display()))
        print("{0} / {1} = {2}".format(the_VecCal.display(), num, division.display()))
    
    

    img


    有用的话点一下采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月4日
  • 已采纳回答 12月27日
  • 创建了问题 12月25日

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'