幾何計算器(類的繼承,模組,函式的呼叫)

2021-08-21 13:41:55 字數 4315 閱讀 9715

#計算圓形,扇形,正方形,長方形,三角形的面積和周長

#模組一:定義圖形類

class shape():

def __init__(self):

self.name=''

self.perimeter=0.0

self.area=0.0

def show_name(self):

pass

def colleage_data(self):

pass

def operation(self):

pass

def operating_results(self):

print('周長:'.format(self.perimeter))

print('面積:'.format(self.area))

def main(self):

self.show_name()

shape.print_line()

self.colleage_data()

shape.print_line()

self.operation()

self.operating_results()

shape.print_line()

@staticmethod

def print_line():

print('-'*40)

@staticmethod

def print_double_line():

print('='*40)

#模組二:定義圓形類,繼承圖形類部分屬性

from shape import shape

import math

class circle(shape):

def __init__(self):

super (circle,self).__init__()

self.name='圓形'

self.r=0

def colleage_data(self):

self.r=int(input('請輸入半徑:'))

def operation(self):

self.perimeter=math.pi*2*self.r

self.area=math.pi*self.r*self.r

if __name__ == '__main__':

circle=circle()

circle.main()

#模組三:定義扇形類,繼承圓形類部分屬性

from circle import circle

class sector(circle):

def __init__(self):

super(sector,self).__init__()

self.name='扇形'

self.angle=0

def colleage_data(self):

super(sector, self).colleage_data()

self.angle=int(input('請輸入圓心角:'))

def operation(self):

super(sector, self).operation()

self.perimeter=self.perimeter*self.angle/360+2*self.r

self.area= self.area*self.angle/360

if __name__ == '__main__':

s=sector()

s.main()

#模組四:定義正方形類,繼承圖形類部分屬性

from shape import shape

class square(shape):

def __init__(self):

super(square,self).__init__()

self.name='正方形'

self.side=0

def colleage_data(self):

self.side=float(input('請輸入邊長:'))

def operation(self):

self.perimeter=self.side*4

self.area=self.side*self.side

if __name__ == '__main__':

s = square()

s.main()

#模組五:定義長方形類,繼承正方形類部分屬性

from square import square

class rectangle(square):

def __init__(self):

super(rectangle,self).__init__()

self.name='長方形'

self.high=0

def colleage_data(self):

super(rectangle,self).colleage_data()

self.high=float(input('請輸入高:'))

def operation(self):

self.perimeter=(self.side+self.high)*2

self.area=self.side*self.high

if __name__ == '__main__':

r=rectangle()

r.main()

#模組六:定義三角形類,繼承長方形部分屬性

from rectangle import rectangle

class ********(rectangle):

def __init__(self):

super(********,self).__init__()

self.name='三角形'

self.length1=0

self.length2=0

def colleage_data(self):

super(********,self).colleage_data()

self.length1=float(input('請輸入第二條邊長:'))

self.length2= float(input('請輸入第三條邊長:'))

def operation(self):

self.perimeter=self.side+self.length1+self.length2

self.area=self.side*self.high

if __name__ == '__main__':

t=********()

t.main()

模組七:呼叫以上模組,定義應用程式類,編寫應用程式邏輯框架

from circle import circle

from sector import sector

from square import square

from rectangle import rectangle

from ******** import ********

from shape import shape

def __init__(self):

self.list=[circle(),sector(),square(),rectangle(),********()]

self.version='v0.1'

shape.print_double_line()

def show_shape(self):

for x,shape in enumerate(self.list):

print('{}'.format(x+1,shape.name))

shape.print_line()

def calc(self):

while true:

self.show_shape()

idx=input('請輸入圖形序號(輸入q退出):')

if idx == 'q':

print('歡迎下次使用')

break

while int(idx)<1 or int(idx)>5:

idx = input('輸入有誤,請重新輸入圖形序號:')

w=self.list[int(idx)-1]

w.main()

def main(self):

print('*****幾何計算器*****')

print('{}'.format(self.version))

self.calc()

if __name__ == '__main__':

a.main()

比簡易計算器更簡便的計算器???

看到了乙個大佬博主發的用c語言做的簡便計算器,看了之後覺得還是很麻煩,太多行了。後來就自己試著做了乙個,減了幾十行 不包括視覺優化 萌新勿噴,謝謝。那個大佬博主用了好多我還不太會打的 但是我覺得完全沒有必要。不知道是不是我想的太簡單,但是我打的 和他打的 效果完全沒有任何區別。好吧但是人家可以炫技我...

簡單的計算器

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...

計算器的實現

計算器?不是非常簡單嗎?宣告兩個float型變數,再加上操作符,自動計算結果出來!public static float calc float a,float b,int operator 呵呵,這麼簡單,那就沒必要寫成文章了 這裡要說的是 程式計算表示式的值,比如 1 2 3 5 9 就是簡單兩個...