赞
踩
博客总领目录请看这篇,不看后悔
软件工程专业大学四年学什么_大学近代史学分是多少-CSDN博客https://blog.csdn.net/qq_41587612/article/details/104362661B站同名up猪,欢迎关注我的账号鸽子不二的个人空间-鸽子不二个人主页-哔哩哔哩视频哔哩哔哩鸽子不二的个人空间,提供鸽子不二分享的视频、音频、文章、动态、收藏等内容,关注鸽子不二账号,第一时间了解UP主动态。
https://space.bilibili.com/204913846
1、词频统计:输入一个文件,程序读取文件,文件内容是英文内容,不少于500个单词的内容,统计每个单词在该短文内出现的次数。
Python统计纯文本文件中英文单词出现个数的方法总结【测试可用】_python_脚本之家
https://www.cnblogs.com/Eudemines/p/9764409.html
https://www.cnblogs.com/zhuyinyinyin/p/7596217.html
https://www.cnblogs.com/lqx12/p/7602309.html
2、用Python设计一个计算器,在实现基本功能的基础上,增加三个功能按键,实现编译原理中提到的三种表达式“先验 3 5 +;中验 + 3 5;后验 + 3 5”。报告要求:需求分析,系统设计(用例图,功能框图,流程图),实现,源码
一、需求分析
1.1系统功能需求
本计算器需要根据用户指定的数字与运算符来进行简单的加减乘除等功能。在运算并显示结果的同时,还需要显示用户按下的数字与运算符键,从而方便用户来进行核对算式和结果。本计算器可在生活中应对普通的日常问题,病能够做到快速、正确、稳定的计算出输入的计算内容。
1.2系统性能要求
本程序作为一个简单的计算器程序,本程序相应并返回运算结果的时间不宜过长。因此,当用户输入完数字与运算符后,得到返回结果的延时不得超过1秒。在输入数值与运算符在合法范围内时,可以返回正确的运算结果。
二、系统设计
2.1用例图
图2.1 计算器用例图
2.2计算器功能图
图2.2计算器功能图
2.3程序流程图
图2.3 程序流程图
三、实现程序
3.1 程序源码
- import tkinter as tk
- class Calc(tk.Frame): # 创建Calc类继承Frame
- def __init__(self, parent=None):
- """简易图形用户界面计算器构造函数"""
- tk.Frame.__init__(self, parent)
- self.pack()
- self.startOfNextOperand = True # 开始输入下一个操作数
- # 创建计算过程和结果的标签
- self.expr = tk.StringVar() # 显示运算表达式
- self.expr.set('')
- self.exprLabel = tk.Label(self, font=('Helvetica', 10),
- fg='#666666', width=64, anchor='e', textvariable=self.expr)
- self.exprLabel.grid(row=0, column=0, columnspan=4)
- self.result = tk.StringVar() # 显示结果
- self.result.set(0)
- self.resultLabel = tk.Label(self, font=('Helvetica', 20), width=34, anchor='e',
- textvariable=self.result)
- self.resultLabel.grid(row=1, column=0, columnspan=4)
- # 简易计算器按钮的按钮,使用一个2D列表表示
- buttons = [['先验','','后验','**'],
- ['CE', 'C', '<-', '/'],
- ['7', '8', '9', '*'],
- ['4', '5', '6', '-'],
- ['1', '2', '3', '+'],
- ['±', '0', '.', '=']]
-
- # 然后使用嵌套的循环的方法创建和布局各个按钮
- for r in range(6):
- for c in range(4):
- # 定义事件处理函数cmd(),默认参数为按钮标签buttons[r][c]
- def cmd(key=buttons[r][c]):
- self.click(key)
- b = tk.Button(self, text=buttons[r][c], width=16, command=cmd)
- b.grid(row=r+2, column=c)# 前两行用于结果显示,按钮从第3行开始
-
- def click(self, key):
- """事件处理"""
- if key == '=': #按等号键时, 求值, 并显示结果
- resultExpr = self.expr.get() + self.result.get()
- resultNum = eval(resultExpr)
- self.result.set(resultNum)
- self.expr.set(resultExpr)
- self.startOfNextOperand = True
- elif key in '**+-/*':
- resultExpr = self.expr.get() + self.result.get() + key
- self.expr.set(resultExpr)
- self.startOfNextOperand = True
- elif key == 'C': # 全部清空, 回到初始状态
- self.expr.set('')
- self.result.set(0)
- elif key == 'CE': # 清空当前输入
- self.result.set(0)
- elif key == '<-':
- oldnum = self.result.get()
- if len(oldnum) == 1: # 只有一个字符
- newnum = 0
- else:
- newnum = oldnum[:-1]
- self.result.set(newnum)
- elif key == '±': # 正负号,切换正负号
- oldnum = self.result.get() # 获取原来的值
- if oldnum[0] == '-':
- newnum = oldnum[1:]
- else:
- newnum = '-' + oldnum
- self.result.set(newnum)
- else: # 按数字或者小数点键
- if self.startOfNextOperand:
- self.result.set(0)
- self.startOfNextOperand = False
- oldnum = self.result.get() # 获取原来的值
- if oldnum == '0':
- self.result.set(key)
- else:
- newnum = oldnum + key
- self.result.set(newnum)
-
- if __name__ == '__main__':
- root = tk.Tk()
- root.title('简易计算器')
- calc = Calc(root)
- root.mainloop()

3.2 运行截图
图3.2 运行截图
四、课程感想
本学期我们学习了Python这节课程,通过这门课我们了解到了它的语法简单,编写起来方便的优点,也使我深刻体会到学习Python的必要性。因此学好Python是我们未来工作中非常重要的事情。对于Python的其他知识还需要不断地学习,希望以后会有更多收获。
实验一 面向对象程序设计实验
一、实验目的
二、实验内容(包括分析或步骤)
1. 定义类和使用类
2. 类的继承和多态
四、实验实现
核心代码1:
- class Vehicle():
- """def __init__(self):
- self.brand
- self.type"""
-
- def __init__(self, brand, type):
- self._brand = brand
- self._type = type
-
- def __del__(self):
- print(self._brand, "is deleted")
- self._brand = ''
- # self._type = ''
-
- def set_brand(this, brand):
- this._brand = brand
-
- def out(self):
- return self._brand, self._type
-
- def main():
- v = Vehicle("audi", "A6")
- print(v.out())
- v.__del__()
- v.set_brand('Volkswagen')
- print(v.out())
- v.set_brand('audi')
- print(v.out())
-
- if __name__ == '__main__':
- main()

运行结果1:
核心代码2:
- from abc import abstractmethod, ABCMeta
-
- class Vehicle(metaclass=ABCMeta):
- @abstractmethod
- def drive(self):
- pass
-
- class Car(Vehicle):
- def drive(self):
- print("驾驶员:%s\n汽车型号:%s\n行驶年数:%d" % ('LZG', 'AE86', 3))
-
- class Airplane(Vehicle):
- def drive(self):
- print("机长:%s\n乘务员数量:%d\n乘客数量:%d" % ('lzg', 15, 66))
-
- def run(vehicle):
- print(vehicle.__class__)
- vehicle.drive()
-
- def main():
- v = [Car(), Airplane()]
- m = map(run, v)
- list(m)
-
- if __name__ == '__main__':
- main()

运行结果2:
实验二 函数式编程实验
一、实验目的
二、实验内容(包括分析或步骤)
1. 使用lambda表达式定义函数
2. 使用map()函数完成函数计算
3. 使用reduce()函数完成函数计算
4. 使用filter()函数完成函数计算
5. 使用递归函数设计完成计算
三、实验实现
核心代码:
- # <editor-fold desc="Description">
- from functools import reduce
-
- def f1():
- z1 = lambda x, y: x ** 3 + y / (x ** 3)
- z2 = lambda x: x ** 3 + x ** 2 + x + 1
- print(z1(1, 2))
- print(z2(1))
-
- def f2():
- m1 = map(lambda x, y: x * y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
- m2 = map(lambda s: s[-1::-1] == s, ['abbbabbba',
- 'asababaSa', 'asffbaffssa', 'attbabtgba'])
- print(list(m1))
- print(list(m2))
-
- def f3():
- r1 = reduce(lambda x, y: x * y, [2, 4, 6, 8, 10])
- r2 = reduce(lambda x, y: x * y if y > 0 else x,
- [2, -4, 6, -2, 10, -8, 4, -2, 5])
- print(r1)
- print(r2)
-
- def f4():
- f1 = filter(lambda s: s.count('a') < 2, [
- 'have a apple', 'you are student', 'hello world'])
- print(list(f1))
-
- def f5():
- def f5_1(n=1, pre=1, pre_pre=0):
- if n is 1:
- return pre
- else:
- return f5_1(n - 1, pre + pre_pre, pre)
-
- def f5_2(n):
- count = 0
- if n >= 2:
- count += f5_2(n - 1) # 1
- count += f5_2(n - 2) # 2
- elif n == 1:
- count += f5_2(n - 1)
- else:
- count = 1
- return count
-
- print(f5_1(50))
- print(f5_2(10))
-
- if __name__ == '__main__':
- f1()
- f2()
- f3()
- f4()
- f5()
- # </editor-fold>

运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。