新型计算器 这个怎么做

题目:设计一个计算器,实现一个三维向量的加法,减法以及向量和标量的乘法和除法运算
提示:
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)
结尾无空行


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


有用的话点一下采纳