#-*- coding: utf-8 -*-#Author: hkey
importredefcheck_exp(get_input):'''检查表达式中的字符是否存在不合法性,合法返回真,不合法返回假
:param get_input: 用户输入的表达式,待检测的表达式
:return: 返回真或者假'''char_set= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','+', '-', '*', '/', '(', ')', '**', '%', '//', '.', } #有效表达式的字符集合
#判断用户输入的表达式字符是否是char_set的子集,如果是则为真,否则为假
if set(get_input) <=char_set:#如果表达式以 '*, /, %, )'开头,则为假
if get_input.startswith('*') or get_input.startswith('/') or get_input.startswith('%') \or get_input.startswith(')'):print('\33[31;1m输入的表达式有误,请重新输入\33[0m')returnFalse#如果表达式以 '+, -, *, /, ('结尾,则为假
elif get_input.endswith('+') or get_input.endswith('-') or get_input.endswith('*') \or get_input.endswith('/') or get_input.endswith('('):print('\33[31;1m输入的表达式有误,请重新输入\33[0m')returnFalse#如果表达式中含有 '.., +/, +/, -*, -/, -%',则为假
elif '..' in get_input or '+*' in get_input or '+/' in get_input or '+%' inget_input \or '-*' in get_input or '-/' in get_input or '-%' in get_input or '%%' inget_input:print('\33[31;1m输入的表达式有误,请重新输入\33[0m')returnFalseelse:returnTrueelse:print('\33[31;1m表达式中包含其他字符,无法进行计算,请重新输入\33[0m')returnFalsedefsimple_exp(exp):'''简化表达式,如'+-'