ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

练习:求字符串中最长的表达式,并计算结果--python实现

2021-01-02 15:02:23  阅读:184  来源: 互联网

标签:return op1 op2 -- list python 计算结果 exp cal


1、判断字符串长度、最长的表达式

"""
1、所有数字,计算长度不能超过long
2、如果有多个长度一样,请返回第一个表达式结果
3、数学表达式必须要是最长的,合法的
4、操作符不能是连续的,如 +--+1是不合法的

"""

import re

s = input("请输入字符串:")

#保留只有0-9 +-*字符
list1 = re.findall("[-+*0-9]+",s)
s0 = ""
for s1 in list1:
    lens = 0
    T = True
    for s2 in s1:#判断是否有连续的运算符
        if s2 in ["-","+","*"]:
            lens+=1
            if lens > 1:
                print("不合法表达式:",s1)
                T = False
                break
        else:
            lens = 0
    if T:#求出最长的表达式
        if len(s0)<len(s1):
            s0 = s1

if s0 == "":
    A=0
else:
    if s0[0] == "*":
        s0 = s0[1:]
        if s0.find("*") == 1:
            print("暂时无法计算乘法:",s0)
        else:
            A = eval(s0)
    else:
        if s0.find("*") == 1:
            print("暂时无法计算乘法:", s0)
        else:
            A = eval(s0)
print(A)

  

2、计算器方法--百度的方法

# judgment a char is a operation or not
from pip._vendor.distlib.compat import raw_input


def is_operation(oper):
    if oper == '+' or oper == '-' or oper == '*' or oper == '/':
        return True
    else:
        return False


# split expression
def mixed_operation(exp):
    exp_list = list(exp)
    temp = ''
    behavor_list = []
    i = 0
    length = len(exp_list)
    for item in exp_list:
        if is_operation(item):
            behavor_list.append(int(temp))
            behavor_list.append(item)
            temp = ''
        else:
            temp += item

        if i == length - 1:
            behavor_list.append(int(temp))
            break
        i += 1

    return behavor_list


# cal a o b
def get_aob(a, o, b):
    if o == '+':
        return a + b
    elif o == '-':
        return a - b
    elif o == '*':
        return a * b
    elif o == '/':
        return a / b


# Calculation op1 and op2('*' and '/' or '+' and '-')
def cal_op1_op2(exp_list, op1, op2):
    if len(exp_list) == 1:
        return exp_list

    i = 0
    has_op = False
    for i in range(2, len(exp_list), 2):
        a = exp_list[i - 2]
        o = exp_list[i - 1]
        b = exp_list[i]
        if o == op1 or o == op2:
            has_op = True
            exp_list[i - 2] = get_aob(a, o, b)
            del exp_list[i]
            del exp_list[i - 1]
            break

    if has_op == False:
        return exp_list

    return cal_op1_op2(exp_list, op1, op2)


# cal exp
def cal_exp(exp_list):
    exp_list = cal_op1_op2(exp_list, '*', '/')
    exp_list = cal_op1_op2(exp_list, '+', '-')

    return exp_list[0]

# while True:
expre = input('Enter your expression(0 to end):')
    # if expre == '0':
    #     break

result = mixed_operation(expre)
print('list result = ',result)
print(cal_exp(result))

#print ('END')

  

遗留问题解决方法:

计算器方法里没有判断表达式首字符是运算符情况,加个判断,再使用上计算器方法结合,就能计算合法的最长表达式结果了。

 

2021-1-2,笔记

标签:return,op1,op2,--,list,python,计算结果,exp,cal
来源: https://www.cnblogs.com/yuntimer/p/14222827.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有