编写一个计算器软件 要求: (1)实现大数相加,相减功能 (2)能进行 2,8,10,16 进制数制转换 (3)能进行判断括号是否匹配、以及对表达式进行求值
时间: 2023-12-03 07:45:36 浏览: 115
设计一个模拟计算器的程序,要求能对包含加、减、乘、除、括号运算符及
以下是一个简单的计算器软件实现,支持大数相加、相减、2、8、10、16进制数制转换、括号匹配和表达式求值功能。
```python
import re
# 大数相加
def add(num1, num2):
res, carry = '', 0
num1, num2 = num1[::-1], num2[::-1]
for i in range(max(len(num1), len(num2))):
n1 = int(num1[i]) if i < len(num1) else 0
n2 = int(num2[i]) if i < len(num2) else 0
s = n1 + n2 + carry
res += str(s % 10)
carry = s // 10
if carry:
res += str(carry)
return res[::-1]
# 大数相减
def sub(num1, num2):
if len(num1) < len(num2):
num1, num2 = num2, num1
num1, num2 = num1[::-1], num2[::-1]
res, borrow = '', 0
for i in range(len(num1)):
n1, n2 = int(num1[i]), int(num2[i]) if i < len(num2) else 0
s = n1 - n2 - borrow
if s < 0:
s += 10
borrow = 1
else:
borrow = 0
res += str(s)
return res[::-1].lstrip('0')
# 10进制转其他进制
def dec2other(num, base):
num, res = int(num), ''
while num:
res += str(num % base)
num //= base
return res[::-1]
# 其他进制转10进制
def other2dec(num, base):
res = 0
for i in range(len(num)):
res += int(num[i]) * (base ** (len(num) - i - 1))
return str(res)
# 表达式求值
def calculate(expression):
expression = expression.replace(' ', '')
stack_num, stack_op = [], []
i = 0
while i < len(expression):
if expression[i].isdigit():
j = i
while j < len(expression) and expression[j].isdigit():
j += 1
num = expression[i:j]
stack_num.append(num)
i = j
elif expression[i] == '(':
stack_op.append('(')
i += 1
elif expression[i] == ')':
while stack_op[-1] != '(':
b, a = stack_num.pop(), stack_num.pop()
op = stack_op.pop()
stack_num.append(str(eval(a + op + b)))
stack_op.pop()
i += 1
else:
while stack_op and stack_op[-1] != '(' and \
((expression[i] in '+-' and stack_op[-1] in '+-') or \
(expression[i] in '*/' and stack_op[-1] in '*/')):
b, a = stack_num.pop(), stack_num.pop()
op = stack_op.pop()
stack_num.append(str(eval(a + op + b)))
stack_op.append(expression[i])
i += 1
while stack_op:
b, a = stack_num.pop(), stack_num.pop()
op = stack_op.pop()
stack_num.append(str(eval(a + op + b)))
return stack_num[0]
# 括号匹配
def brackets_match(expression):
stack = []
for ch in expression:
if ch == '(':
stack.append(ch)
elif ch == ')':
if not stack:
return False
stack.pop()
return not stack
# 主函数
def main():
while True:
print('=' * 30)
print('请选择功能:')
print('1. 大数相加')
print('2. 大数相减')
print('3. 进制转换')
print('4. 表达式求值')
print('5. 括号匹配')
print('0. 退出')
choice = input('请输入功能对应的数字:')
if choice == '0':
break
elif choice == '1':
num1 = input('请输入第一个数:')
num2 = input('请输入第二个数:')
print('结果为:', add(num1, num2))
elif choice == '2':
num1 = input('请输入第一个数:')
num2 = input('请输入第二个数:')
print('结果为:', sub(num1, num2))
elif choice == '3':
num = input('请输入要转换的数:')
src_base = int(input('请输入原数的进制:'))
dst_base = int(input('请输入目标进制:'))
if src_base != 10:
num = other2dec(num, src_base)
if dst_base == 10:
print('结果为:', num)
else:
print('结果为:', dec2other(num, dst_base))
elif choice == '4':
expression = input('请输入要求值的表达式:')
print('结果为:', calculate(expression))
elif choice == '5':
expression = input('请输入要检查的表达式:')
if brackets_match(expression):
print('括号匹配')
else:
print('括号不匹配')
else:
print('输入有误,请重新输入!')
if __name__ == '__main__':
main()
```
该计算器软件通过命令行与用户交互,并提供了简单的菜单选择功能。用户可以根据自己的需求选择相应的功能。
阅读全文