#encoding =utf-8
From __future__ Import Division
Import time
# 13-5. Geometric. Creates a point class that consists of an ordered numeric pair (x, Y) that represents the x-coordinate of a dot and the Y-seat
Header The X and Y coordinates are passed to the constructor when instantiated, and the default is the origin of the coordinates if no value is given.
Class Point (object):
def __init__ (self,x = 0,y = 0):
Self. Pointx = X
Self. Pointy = y
print ' (%d,%d) '% (self. Pointx,self. Pointy)
P1 = Point ()
P2 = point (2,3)
# 13-6. Geometric. Create a line/Line segment class. In addition to the primary data properties: A pair of coordinate values (see previous exercise),
# It also has length and slash properties. You need to overwrite the __repr__ () method (and, if necessary, the __str__ () method), making
# represents the string representation of the line (or line segment) as a tuple of a pair of tuples, that is, ((x1, y1), (x2, y2)).
Summary
# __REPR__ Displays two endpoints (start and stop) of a line as a pair of tuples
# length Returns the length of the straight segment-Do not use "Len" because it makes people misunderstand that it is an integer.
# slope returns the slope of this line segment (or None at the appropriate time)
#额外
# There are at least three ways to find the square root of Python
# 1. The simplest way is to ask for 0.5 times
# 1
# 4 * * 0.5
# 2. Using the SQRT function of the math package
# 1
# MATH.SQRT (4)
# 3. Using the SQRT function of the NumPy package
# 1
# NUMPY.SQRT (4)
#
#round (a,2) can keep a two decimal places
Class Line (object):
def __init__ (self,p1 = point (2,3), p2 = point):
Self.point1 = P1
Self.point2 = P2
def __repr__ (self):
print ' line is '
print ' ((%d,%d), (%d,%d)) '% (Self.point1.pointx,self.point1.pointy, \
Self.point2.pointx,self.point2.pointy)
def length (self):
x = Self.point1.pointx-self.point2.pointx
y = Self.point1.pointy-self.point2.pointy
TEM1 = x*x + y*y
TEM2 = tem1 * * 0.5
print ' line length is:%.2f '% round (tem2,2)
def slope (self):
x = Self.point1.pointx-self.point2.pointx
y = Self.point1.pointy-self.point2.pointy
print ' line slope is%.2f '% (x/y)
L1 = line ()
L1.__REPR__ ()
L1.length ()
L1.slope ()
L2 = line (point (1,5), point (3,4))
L2.slope ()
# 13-7. Data classes. Provides an interface to a time module that allows the user to format their own given times, such as:
# "Mm/dd/yy," "Mm/dd/yyyy," "Dd/mm/yy," "Dd/mm/yyyy," "Mon DD, YYYY," or standard
# Unix Date format: "Day Mon DD, HH:MM:SS YYYY" to see the date. Your class should maintain a date value, and
# Create an instance with the given time. If no time value is given, the current system time is taken by default when the program executes. Also package
# includes some other methods:
# Update () modifies data values at a given time or the default current system time
# display () takes a string representing the time format and displays it in the format of the given time:
# ' MDY ' ==> mm/dd/yy
# ' Mdyy ' ==> mm/dd/yyyy
# ' DMY ' ==> dd/mm/yy
# ' Dmyy ' ==> dd/mm/yyyy
# ' Modyy ' ==> Mon DD, YYYY
# If no time format is provided, the default is to use the system time or CTime () format. Additional questions: Put this class and practice
# 6-15 combined
Print Time.ctime ()
Class MyTime (object):
def update (self):
Pass
def display (self,t):
If T = = ' MDY ':
Print Time.strftime ("%m/%y/%d", Time.localtime ())
If T = = ' Dmyy ':
Print Time.strftime ("%h/%m/%s", Time.localtime ())
time1 = MyTime ()
Time1.display (' MDY ')
Time1.display (' Dmyy ')
Python class Exercises