RosettaCodeData/Task/Vector/Unicon/vector.unicon
2026-04-30 12:34:36 -04:00

21 lines
815 B
Text

procedure main()
v1 := vector(5,7)
v2 := vector(2,3)
write(v1.tostring(),"+",v2.tostring()," = ",(v1 + v2).tostring())
write(v1.tostring(),"-",v2.tostring()," = ",(v1 - v2).tostring())
write(v1.tostring(),"*123 = ",(v1*123).tostring())
write(v1.tostring(),"/0.5 = ",(v1*0.5).tostring())
end
class vector(x,y)
method tostring();return "["||x||","||y||"]"; end
method add(v); return vector(x+v.x, y+v.y); end
method minus(v); return vector(x-v.x, y-v.y); end
method mult(z); return vector(x*z, y*z); end
method div(z); return vector(x/z, y/z); end
# The next four provide operator overloading, if enabled.
method __add__(v); return add(v); end
method __minus__(v); return minus(v); end
method __mult__(z); return mult(z); end
method __div__(z); return div(z); end
end