-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexNumber.rb
More file actions
105 lines (86 loc) · 2.45 KB
/
ComplexNumber.rb
File metadata and controls
105 lines (86 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class ComplexNumber
attr_accessor :real, :imaginary
def initialize(real, imaginary)
@real = real
@imaginary = imaginary
end
# def self.input
# print "Pls input real number : "
# real = gets.chomp
# @real = real.to_i
# print "Pls input imaginary number : "
# imaginary = gets.chomp
# @imaginary = imaginary.to_i
# complex = ComplexNumber.new(@real,@imaginary)
# end
def output
"#{self.real} + #{self.imaginary}i"
end
# def self.output(complex)
# puts "#{complex.real} + #{complex.imaginary}i"
# end
def +(t)
self.real += t.real
self.imaginary += t.imaginary
self
end
# def sum(t)
# @real += t.real
# @imaginary += t.imaginary
# self
# end
def -(t)
self.real -= t.real
self.imaginary -= t.imaginary
self
end
# def subtract(t)
# @real -= t.real
# @imaginary -= t.imaginary
# self
# end
def *(t)
self.real = (self.real * t.real) - (self.imaginary * t.imaginary)
self.imaginary = (self.real * t.imaginary) - (self.imaginary * t.real)
self
end
# def multiply(t)
# @real = (@real * t.real) - (@imaginary * t.imaginary)
# @imaginary = (@real * t.imaginary) - (@imaginary * t.real)
# self
# end
def /(t)
self.real = ((self.real * t.real) + (self.imaginary + t.imaginary))/(t.real*t.real) + (t.imaginary*t.imaginary)
self.imaginary = ((self.imaginary * t.real) - (self.real * t.imaginary))/(t.real*t.real) + (t.imaginary*t.imaginary)
self
end
# def divide(t)
# @real = ((@real * t.real) + (@imaginary + t.imaginary))/(t.real*t.real) + (t.imaginary*t.imaginary)
# @imaginary = ((@imaginary * t.real) - (@real * t.imaginary))/(t.real*t.real) + (t.imaginary*t.imaginary)
# self
# end
end
# a = ComplexNumber.input
# b = ComplexNumber.input
a = ComplexNumber.new(3, 9)
b = ComplexNumber.new(5, 15)
print "Complex number : "
#ComplexNumber.output(a)
puts a.output
puts b.output
print "Sum : "
# ComplexNumber.output(a.sum(b))
c = a.clone
puts (c+b).output
print "Subtract : "
# ComplexNumber.output(a.subtract(b))
d = a.clone
puts (d-b).output
print "Multiply : "
# ComplexNumber.output(a.multiply(b))
e = a.clone
puts (e*a).output
print "Divide : "
# ComplexNumber.output(a.divide(b))
f = a.clone
puts (f/b).output