暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

原创 tony 2022-06-22
1055

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

"""

1、所有数字,计算长度不能超过long

2、如果有多个长度一样,请返回第一个表达式结果

3、数学表达式必须要是最长的,合法的

4、操作符不能是连续的,如 +--+1是不合法的

 

"""

 

import re

 

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

 

#保留只有0-9 +-*字符

list1 = re.findall("[-+*0-9]+",s)

s0 = ""

for s1 in list1:

    lens = 0

    = True

    for s2 in s1:#判断是否有连续的运算符

        if s2 in ["-","+","*"]:

            lens+=1

            if lens > 1:

                print("不合法表达式:",s1)

                = 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:

            = eval(s0)

    else:

        if s0.find("*"== 1:

            print("暂时无法计算乘法:", s0)

        else:

            = eval(s0)

print(A)

  

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

# 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 = []

    = 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 == length - 1:

            behavor_list.append(int(temp))

            break

        += 1

 

    return behavor_list

 

 

# cal a o b

def get_aob(a, o, b):

    if == '+':

        return + b

    elif == '-':

        return - b

    elif == '*':

        return * b

    elif == '/':

        return / b

 

 

# Calculation op1 and op2('*' and '/' or '+' and '-')

def cal_op1_op2(exp_list, op1, op2):

    if len(exp_list) == 1:

        return exp_list

 

    = 0

    has_op = False

    for in range(2len(exp_list), 2):

        = exp_list[i - 2]

        = exp_list[i - 1]

        = exp_list[i]

        if == op1 or == 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')

  

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论