自定义类模拟三维向量及其运算(python)

定义一个三维向量类,并定义相应的特殊方法实现两个该类对象之间的加、减运算(要求支持运算符+、-),实现该类对象与标量的乘、除运算(要求支持运算符*、/),以及向量长度的计算(要求使用属性实现)。

class Vector3:
    # 构造方法,初始化,定义向量坐标
    def __init__(self, x, y, z):
        self.__x = x
        self.__y = y
        self.__z = z

    # 与两一个向量相加,对应分量相加,返回新向量
    def __add__(self, anotherPoint):
        x = self.__x + anotherPoint.__x
        y = self.__y + anotherPoint.__y
        z = self.__z + anotherPoint.__z
        return Vector3(x, y, z)

    # 减去另一个向量,对应分量相减,返回新向量
    def __sub__(self, anotherPoint):
        x = self.__x - anotherPoint.__x
        y = self.__y - anotherPoint.__y
        z = self.__z - anotherPoint.__z
        return Vector3(x, y, z)

    # 向量与一个数字相乘,各分量乘以同一个数字,返回新向量
    def __mul__(self, n):
        x, y, z = self.__x*n, self.__y*n, self.__z*n
        return Vector3(x, y, z)

    # 向量除以一个数字,各分量除以同一个数字,返回新向量
    def __truediv__(self, n):
        x, y, z = self.__x/n, self.__y/n,
1、 一个三维向量类定义相应特殊方法实现两个该类对象之间运算(要求支持运算符+、-),实现该类对象与标量的乘、除运算(要求支持运算符*、/),以及向量长度的计算(要求使用属性实现)。 2、 编程实现如下功能: (1)定义一个抽象类Shape,在抽象类 Shape中定义求面积 getArea()和周长 getPerimeter()的抽象方法。 (2)分别定义继承抽象类Shape的3个子类即Triangle、Rectangle、和Circle,在这3个子类中重写 Shape中的方法getArea()和 getPerimeter( )。 (3)创建类Triangle、 Rectangle、 Circle的对象,对3个类中的方法进行调用测试。 3、使用第六章(王雷春版)介绍的知识设计一个“书籍出租管理系统”,该系统包括以下功能。 (1) 菜单项“1”:显示书籍(包括书籍名称、价格和借出状态)(2) 菜单项“2”:增书籍(包括书籍名称和价格)(3) 菜单项“3”:借出书籍(包括借出书籍名称和借出天数)(4) 菜单项“4”:归还书籍(包括归还书籍名称和应付的租书费)(5) 菜单项“5”:统计书籍(包括借出书籍册数、未借出书籍册数和总册数)(6) 菜单项“-1”:退出系统。 体会利用面向对象编程的思想。 4、设计一个“超市进销存管理系统”,要求如下: (1)系统包括7种操作,分别是:1查询所有商品;2添商品;3修改商品;4.删除商品;5卖出端口;6.汇总;-1.退出系统。 (2)选择操作序号“1”,显示所有商品 (3)选择操作序号“2”,添新的商品(包括商品名称、数量和进货价格)。 (4)选择操作序号“3”,修改商品 (5)选择操作序号“4”,删除商品 (6)选择操作序号“5”,卖出商品(包括商品名称、数量和售出价格)。 (7)选择操作序号“6”,汇总当天卖出商品,包括每种销售商品名称、数量、进货总价、销售总价等。 (8)选择操作序号“-1”,退出系统。
Python 中,使用自定义类模拟三维向量及其运算可通过定义一个类实现相应特殊方法达成。以下为两种实现方式: ### 方式一 定义一个 `Vector3` 类,通过特殊方法实现运算符重载,支持向量间的运算,以及向量与标量的乘、除运算,同时使用属性计算向量长度。 ```python class Vector3: # 构造方法,初始化,定义向量坐标 def __init__(self, x, y, z): self.__x = x self.__y = y self.__z = z # 与另一个向量,对应分量相,返回新向量 def __add__(self, anotherPoint): x = self.__x + anotherPoint.__x y = self.__y + anotherPoint.__y z = self.__z + anotherPoint.__z return Vector3(x, y, z) # 去另一个向量,对应分量相,返回新向量 def __sub__(self, anotherPoint): x = self.__x - anotherPoint.__x y = self.__y - anotherPoint.__y z = self.__z - anotherPoint.__z return Vector3(x, y, z) # 向量与一个数字相乘,各分量乘以同一个数字,返回新向量 def __mul__(self, n): x, y, z = self.__x * n, self.__y * n, self.__z * n return Vector3(x, y, z) # 向量除以一个数字,各分量除以同一个数字,返回新向量 def __truediv__(self, n): x, y, z = self.__x / n, self.__y / n, self.__z / n return Vector3(x, y, z) # 查看向量长度,所有分量平方和的平方根 @property def length(self): return (self.__x**2 + self.__y**2 + self.__z**2)**0.5 def __str__(self): return 'Vector3({},{},{})'.format(self.__x, self.__y, self.__z) ``` 使用示例: ```python v1 = Vector3(1, 2, 3) v2 = Vector3(4, 5, 6) print(v1 + v2) print(v1 - v2) print(v1 * 2) print(v1 / 2) print(v1.length) ``` ### 方式二 定义一个 `MyArray` 类,通过普通方法实现向量间的运算,以及向量与标量的乘、除运算计算向量长度,同时在方法中直接输出结果。 ```python class MyArray: def __init__(self, x, y, z): self.__x = x self.__y = y self.__z = z def add(self, a): x = self.__x + a.__x y = self.__y + a.__y z = self.__z + a.__z print("和=({},{},{})".format(x, y, z)) def sub(self, a): x = self.__x - a.__x y = self.__y - a.__y z = self.__z - a.__z print("差=({},{},{})".format(x, y, z)) def mul(self, a): x = self.__x * a y = self.__y * a z = self.__z * a print("乘积=({},{},{})".format(x, y, z)) def truediv(self, a): x = self.__x / a y = self.__y / a z = self.__z / a print("商=({},{},{})".format(x, y, z)) def length(self): a = pow(pow(self.__x, 2) + pow(self.__y, 2) + pow(self.__z, 2), 0.5) print("长度为:{}".format(round(a, 3))) print('请输入六个数a,b,c,d,e,f:') a, b, c, d, e, f = map(int, input().split()) print('N1:', (a, b, c)) print('N2:', (d, e, f)) n1 = MyArray(a, b, c) n2 = MyArray(d, e, f) n1.add(n2) n1.sub(n2) n1.mul(2) n1.truediv(2) n1.length() ``` 这两种方式都能实现使用自定义类模拟三维向量及其运算的功能,方式一借助运算符重载,代码更简洁直观;方式二则通过普通方法输出结果,适合需要直接展示运算结果的场景 [^1][^2][^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值