Lua 实现复数计算器

6 篇文章 0 订阅

Lua 实现复数计算器

要求:实现一个Complex复数结构,支持复数的加、减、乘、除四则运算
主要思路:利用元表,通过元方法重写四则运算操作符

方法1:通过元表实现

-- ***** 元表 *****
metaTable = {}

-- a+bi, c+di
-- c1[1], c1[2], c2[1], c2[2]
-- 使用元表的元方法 更改加减乘除对应运算符的操作
function metaTable.__add(c1, c2)
    res = {}
    res[1] = c1[1] + c2[1]
    res[2] = c1[2] + c2[2]
    setmetatable(res, metaTable)
    return res
end

function metaTable.__sub(c1, c2)
    res = {}
    res[1] = c1[1] - c2[1]
    res[2] = c1[2] - c2[2]
    setmetatable(res, metaTable)
    return res
end

function metaTable.__mul(c1, c2)
    res = {}
    -- ac-bd, bc+ad i
    res[1] = c1[1]*c2[1] - c1[2] * c2[2]
    res[2] = c1[2]*c2[1] + c1[1] * c2[2]
    setmetatable(res, metaTable)
    return res
    
end

function metaTable.__div(c1, c2)
    res = {}
    --:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +((bc-ad)/(c^2+d^2))i
    res[1] = (c1[1] * c2[1] + c1[2] * c2[2]) / (c2[1] * c2[1] + c2[2] * c2[2])
    res[2] = (c1[2] * c2[1] - c1[1] * c2[2]) / (c2[1] * c2[1] + c2[2] * c2[2])
    setmetatable(res, metaTable)
    return res

end

-- 使用tostring修改打印信息
function metaTable.__tostring(mytable)
    real = mytable[1] or 0
    imag = mytable[2] or 0
    if real == 0 then
        if imag ~= 0 then
            return imag..'i'
        else
            return real
        end
    elseif imag == 0 then
        return real..''
    else
        return real .. '' .. imag..'i'
    end

end


-- 复数函数
function Complex(a, b)
    a = a or 0
    b = b or 0
    mytable = {a, b}
    setmetatable(mytable, metaTable)
    return mytable
end


local c1 = Complex(3.2, -5.1)
local c2 = Complex(7, 8)
local c3 = Complex(math.pi)
local c4 = Complex()

print((c1 + c4) * (c1 / c2 + c3))
-- output: 6.7653973764431-16.927609258972i

方法2:通过复数类实现

-- ***** 元类 *****
Complex = {real = 0, imag = 0}

-- 构造函数
function Complex:new(real, imag)
    local o = o or {}


    o.real = real or 0
    o.imag = imag or 0
    setmetatable(o, self)
    self.__index = self
    return o
end


-- 重载加法运算符
function Complex:__add(c2)
    local o = Complex:new()
    o.real = self.real + c2.real
    o.imag = self.imag + c2.imag
    --print(o.real, o.imag)
    return o

end

-- 重载减法运算符
function Complex:__sub(c2)
    local o = Complex:new()
    o.real = self.real - c2.real
    o.imag = self.imag - c2.imag

    return o
end

-- 重载乘法运算符
function Complex:__mul(c2)
    local o = Complex:new()
    o.real = self.real * c2.real - self.imag * c2.imag
    o.imag = self.imag * c2.real + self.real * c2.imag
    return o



end

-- 重载除法运算符
function Complex:__div(c2)
    local o = Complex:new()
    o.real = (self.real * c2.real + self.imag * c2.imag) / 
            (c2.real * c2.real + c2.imag * c2.imag)
    o.imag = (self.imag * c2.real - self.real * c2.imag) / 
            (c2.real * c2.real + c2.imag * c2.imag)

    return o

end


-- 使用tostring修改打印信息
function Complex:__tostring()
    real = self.real or 0
    imag = self.imag or 0

    if real == 0 then
        if imag ~= 0 then
            return imag..'i'
        else
            return real
        end
    elseif imag == 0 then
        return real..''
    else
        return real .. '' .. imag..'i'
    end

end

-- 创建实例对象
local c1 = Complex:new(3.2, -5.1)
local c2 = Complex:new(7, 8)
local c3 = Complex:new(math.pi)
local c4 = Complex:new()

print((c1 + c4) * (c1 / c2 + c3))
-- output: 6.7653973764431-16.927609258972i
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值