导语
九月初家里的熊孩子终于开始上学了!
半个月过去了,小孩子每周都会带着一堆的数学作业回来,哈哈哈哈~真好,在家做作业就没时间打扰我写代码了。
很赞,鹅鹅鹅饿鹅鹅鹅~曲项向天歌~~~~开心到原地起飞。
孩子昨天回家之后吃完饭就悄咪咪的说,神神秘秘的我以为做什么?结果是班主任让他们每个人带一个计算器,平常做数学算数的时候可以在家用用,嗯哼~这还用卖嘛?
立马给孩子用Python制作了一款简直一摸一样的学生计算器~
正文
本文的学生计算器是基于tkinter做的界面化的小程序哈!
math模块中定义了一些数学函数。由于这个模块属于编译系统自带,因此它可以被无条件调用。
都是自带的所以不用安装可以直接使用。
定义各种运算,设置显示框字节等:
oot = tkinter.Tk() root.resizable(width=False, height=False) '''hypeparameter''' # 是否按下了运算符 IS_CALC = False # 存储数字 STORAGE = [] # 显示框最多显示多少个字符 MAXSHOWLEN = 18 # 当前显示的数字 CurrentShow = tkinter.StringVar() CurrentShow.set('0') '''按下数字键(0-9)''' def pressNumber(number): global IS_CALC if IS_CALC: CurrentShow.set('0') IS_CALC = False if CurrentShow.get() == '0': CurrentShow.set(number) else: if len(CurrentShow.get()) < MAXSHOWLEN: CurrentShow.set(Curren<p>本文来源gao!daima.com搞$代!码#网#</p>tShow.get() + number) '''按下小数点''' def pressDP(): global IS_CALC if IS_CALC: CurrentShow.set('0') IS_CALC = False if len(CurrentShow.get().split('.')) == 1: if len(CurrentShow.get()) < MAXSHOWLEN: CurrentShow.set(CurrentShow.get() + '.') '''清零''' def clearAll(): global STORAGE global IS_CALC STORAGE.clear() IS_CALC = False CurrentShow.set('0') '''清除当前显示框内所有数字''' def clearCurrent(): CurrentShow.set('0') '''删除显示框内最后一个数字''' def delOne(): global IS_CALC if IS_CALC: CurrentShow.set('0') IS_CALC = False if CurrentShow.get() != '0': if len(CurrentShow.get()) > 1: CurrentShow.set(CurrentShow.get()[:-1]) else: CurrentShow.set('0') '''计算答案修正''' def modifyResult(result): result = str(result) if len(result) > MAXSHOWLEN: if len(result.split('.')[0]) > MAXSHOWLEN: result = 'Overflow' else: # 直接舍去不考虑四舍五入问题 result = result[:MAXSHOWLEN] return result
按下运算符:
def pressOperator(operator): global STORAGE global IS_CALC if operator == '+/-': if CurrentShow.get().startswith('-'): CurrentShow.set(CurrentShow.get()[1:]) else: CurrentShow.set('-'+CurrentShow.get()) elif operator == '1/x': try: result = 1 / float(CurrentShow.get()) except: result = 'illegal operation' result = modifyResult(result) CurrentShow.set(result) IS_CALC = True elif operator == 'sqrt': try: result = math.sqrt(float(CurrentShow.get())) except: result = 'illegal operation' result = modifyResult(result) CurrentShow.set(result) IS_CALC = True elif operator == 'MC': STORAGE.clear() elif operator == 'MR': if IS_CALC: CurrentShow.set('0') STORAGE.append(CurrentShow.get()) expression = ''.join(STORAGE) try: result = eval(expression) except: result = 'illegal operation' result = modifyResult(result) CurrentShow.set(result) IS_CALC = True elif operator == 'MS': STORAGE.clear() STORAGE.append(CurrentShow.get()) elif operator == 'M+': STORAGE.append(CurrentShow.get()) elif operator == 'M-': if CurrentShow.get().startswith('-'): STORAGE.append(CurrentShow.get()) else: STORAGE.append('-' + CurrentShow.get()) elif operator in ['+', '-', '*', '/', '%']: STORAGE.append(CurrentShow.get()) STORAGE.append(operator) IS_CALC = True elif operator == '=': if IS_CALC: CurrentShow.set('0') STORAGE.append(CurrentShow.get()) expression = ''.join(STORAGE) try: result = eval(expression) # 除以0的情况 except: result = 'illegal operation' result = modifyResult(result) CurrentShow.set(result) STORAGE.clear() IS_CALC = True