Driver for class Rational. : Rational « Data Type « Python Tutorial

Python Tutorial
1. Introduction
2. Data Type
3. Statement
4. Operator
5. String
6. Tuple
7. List
8. Dictionary
9. Collections
10. Function
11. Class
12. File
13. Buildin Function
14. Buildin Module
15. Database
16. Regular Expressions
17. Thread
18. Tkinker
19. wxPython
20. XML
21. Network
22. CGI Web
23. Windows
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Python Tutorial » Data Type » Rational 
2. 15. 1. Driver for class Rational.
def gcdx, y ):
   while y:
      z = x
      x = y
      y = z % y

   return x

class Rational:
   def __init__self, top = 1, bottom = ):
      if bottom == 0:
         raise ZeroDivisionError, "Cannot have 0 denominator"


      self.numerator = abstop )
      self.denominator = absbottom )
      self.sign = top * bottom self.numerator * self.denominator )

      self.simplify()  

   def simplifyself ):
      common = gcdself.numerator, self.denominator )
      self.numerator /= common
      self.denominator /= common
         
   def __neg__self ):
      return Rational-self.sign * self.numerator, self.denominator )
      
   def __add__self, other ):
      return Rational(self.sign * self.numerator * other.denominator +
         other.sign * other.numerator * self.denominator,
         self.denominator * other.denominator )

   def __sub__self, other ):
      return self + -other )

   def __mul__self, other ):
      return Rationalself.numerator * other.numerator,
                       self.sign * self.denominator *
                       other.sign * other.denominator )

   def __div__self, other ):
      return Rationalself.numerator * other.denominator,
                       self.sign * self.denominator *
                       other.sign * other.numerator )

   def __truediv__self, other ):
      return self.__div__other )

   def __eq__self, other ):
      return self - other ).numerator == 0

   def __lt__self, other ):
      return self - other ).sign < 0

   def __gt__self, other ):
      return self - other ).sign > 0

   def __le__self, other ):
      return self < other or self == other )

   def __ge__self, other ):
      return self > other or self == other )

   def __ne__self, other ):
      return not self == other )      
   
   def __abs__self ):
      return Rationalself.numerator, self.denominator )
   
   def __str__self ):
      if self.sign == -1:
         signString = "-"
      else:
         signString = ""

      if self.numerator == 0:
         return "0"
      elif self.denominator == 1:
         return "%s%d" signString, self.numerator )
      else:
         return "%s%d/%d" signString, self.numerator, self.denominator )
            
   def __int__self ):
      return self.sign * divmodself.numerator, self.denominator )[ ]

   def __float__self ):
      return self.sign * floatself.numerator / self.denominator

   def __coerce__self, other ):
      if typeother == type):
         return self, Rationalother ) )
      else:
         return None

rational1 = Rational()  
rational2 = Rational1030 )  
rational3 = Rational-714 )  

print "rational1:", rational1
print "rational2:", rational2
print "rational3:", rational3
print

print rational1, "/", rational2, "=", rational1 / rational2
print rational3, "-", rational2, "=", rational3 - rational2
print rational2, "*", rational3, "-", rational1, "=", rational2 * rational3 - rational1

rational1 += rational2 * rational3
print "\nrational1 after adding rational2 * rational3:", rational1
print

print rational1, "<=", rational2, ":", rational1 <= rational2
print rational1, ">", rational3, ":", rational1 > rational3
print

print "The absolute value of", rational3, "is:", absrational3 )
print

print rational2, "as an integer is:"intrational2 )
print rational2, "as a float is:"floatrational2 )
print rational2, "+ 1 =", rational2 + 1
2. 15. Rational
2. 15. 1. Driver for class Rational.
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.