课程:《Python程序设计》
班级: 2243
姓名: 徐志力
学号:20224323
实验教师:王志强
实验日期:2023年3月23日
必修/选修: 公选课
自己设计计算机系统
其中含有加减乘除阶乘三角函数指数对数的运算
代码如下:
def sum(a,b): return (a+b) def sub(a,b): return (a-b) def mul(a,b): return (a*b) def div(a,b): return (a/b) def power(a,b): return (a**b) def log1(a,b): return math.log(b,a) def factorial2023(n): result = 1 for i in range(1,n+1): result = result*i return result def sin1(n): return math.sin(n) def cos1(n): return math.cos(n) def tan1(n): return math.tan(n) flag = 1 while True: a = int(input("请输入一个整数a:")) b = int(input("请输入一个整数b: ")) operation = input("请问您需要什么操作:!+-*/ ** log sin cos tan") if operation == '+': print("a+b=",sum(a,b)) elif operation == '-': print("a-b=",sub(a,b)) elif operation == '*': print("a*b=",mul(a,b)) elif operation == '/': if b == 0: print("除数不能为0") else: print("a/b=",div(a,b)) elif operation == '**': print("a**b=",power(a,b)) elif operation == 'log': print("log a b=",log1(b,a)) elif operation == '!': print("a!=", factorial2023(a), "b!=", factorial2023(b)) elif operation == 'sin': print("sin(a)=", sin1(a), "sin(b)=", sin1(b)) elif operation == 'cos': print("cos(a)=", cos1(a), "cos(b)", cos1(b)) elif operation == 'tan': print("tan(a)=", tan1(a), "tan(b)=", tan1(b)) flag = int(input("请问你是否继续?继续:1,结束:0\n")) if flag == 0: break
在这次实验中,我们学习了函数的写法和运用
我们先从加法开始出发,然后增加了循环系统,在此基础上,再加上了减减乘除阶乘三角函数指数对数的运算
加法:
循环部分:
def main(): flag = 1 while True: a = int(input("请输入一个整数a:")) b = int(input("请输入一个整数b:")) operation = input("请问您需要什么操作?! - * / ** log sin cos tan") if operation == '+': print("a + b =", sum(a, b)) elif operation == '-': print("a - b =", sub(a, b)) elif operation == '*': print("a * b =", mul(a, b)) elif operation == '/': if b == 0: print("除数不能为0") else: print("a / b =", div(a, b)) elif operation == '**': print("a ** b =", power(a, b)) elif operation == 'log': print("log a b =", log1(b, a)) elif operation == '!': print("a! =", factorial2023(a), "b! =", factorial2023(b)) elif operation == 'sin': print("sin(a) =", sin1(a), "sin(b) =", sin1(b)) elif operation == 'cos': print("cos(a) =", cos1(a), "cos(b) =", cos1(b)) elif operation == 'tan': print("tan(a) =", tan1(a), "tan(b) =", tan1(b)) flag = int(input("请问你是否继续?继续:1,结束:0\n")) if flag == 0: break if __name__ == '__main__': main()
循环部分运用flag的值来选择是否继续
自己建立的函数运算:
def log1(a,b): return math.log(b,a) def factorial2023(n): result = 1 for i in range(1,n+1): result = result*i return result def sin1(n): return math.sin(n) def cos1(n): return math.cos(n) def tan1(n): return math.tan(n)
再次过程中,主要运用了math库中的函数来解决
最后,将文件上传到gitee
如图:
运用函数可以实现重复使用
import abc #利用abc模块实现抽象类
class All_file(metaclass=abc.ABCMeta):
all_type='file'
@abc.abstractmethod #定义抽象方法,无需实现功能
def read(self):
'子类必须定义读功能'
with open('filaname') as f:
pass
@abc.abstractmethod #定义抽象方法,无需实现功能
def write(self):
'子类必须定义写功能'
pass
class Txt(All_file): #子类继承抽象类,但是必须定义read和write方法
def read(self):
print('文本数据的读取方法')
def write(self):
print('文本数据的读取方法')
class Sata(All_file): #子类继承抽象类,但是必须定义read和write方法
def read(self):
print('硬盘数据的读取方法')
def write(self):
print('硬盘数据的读取方法')
class Process(All_file): #子类继承抽象类,但是必须定义read和write方法
def read(self):
print('进程数据的读取方法')
def write(self):
print('进程数据的读取方法')
wenbenwenjian=Txt()
yingpanwenjian=Sata()
jinchengwenjian=Process()
#这样大家都是被归一化了,也就是一切皆文件的思想
wenbenwenjian.read()
yingpanwenjian.write()
jinchengwenjian.read()
print(wenbenwenjian.all_type)
print(yingpanwenjian.all_type)
print(jinchengwenjian.all_type)
# 抽象类 : 规范
# 一般情况下 单继承 能实现的功能都是一样的,所以在父类中可以有一些简单的基础实现
# 多继承的情况 由于功能比较复杂,所以不容易抽象出相同的功能的具体实现写在父类中
# 抽象类还是接口类 : 面向对象的开发规范 所有的接口类和抽象类都不能实例化
# java :
# java里的所有类的继承都是单继承,所以抽象类完美的解决了单继承需求中的规范问题
# 但对于多继承的需求,由于java本身语法的不支持,所以创建了接口Interface这个概念来解决多继承的规范问题
# python
# python中没有接口类 :
# python中自带多继承 所以我们直接用class来实现了接口类
# python中支持抽象类 : 一般情况下 单继承 不能实例化
# 且可以实现python代码