Πληροφορικής και Τηλεπικοινωνιών - Άρτα
Πανεπιστήμιο Ιωαννίνων
Γκόγκος Χρήστος
http://chgogos.github.io/
Εαρινό εξάμηνο 2020-2021
H Python όπως και άλλες γλώσσες (Ada, C++, Ruby) υποστηρίζει υπερφόρτωση τελεστών. Συνεπώς, γνώριμοι τελεστές όπως οι +, _, *, > , < και άλλοι μπορούν να χρησιμοποιηθούν με αντικείμενα κλάσεων που ορίζει ο προγραμματιστής.
Υλοποίηση πράξεων +, -, * για intervals (https://en.wikipedia.org/wiki/Interval_arithmetic)
class Interval:
def __init__(self, start, end):
self.start = start
self.end = end
def __str__(self):
return f'({self.start},{self.end})'
def __add__(self, other):
return Interval(self.start + other.start, self.end + other.end)
def __sub__(self, other):
return Interval(self.start - other.end, self.end - other.start)
def __mul__(self, other):
minimum = min([self.start * other.start, self.start * other.end, self.end * other.start, self.end*other.end])
maximum = max([self.start * other.start, self.start * other.end, self.end * other.start, self.end*other.end])
return Interval(minimum, maximum)
i1 = Interval(1,5)
i2 = Interval(4,6)
print(i1+i2)
print(i1-i2)
print(i2-i1)
print(i1*i2)
(5,11) (-5,1) (-1,5) (4,30)