首页 > 试题广场 >

计算表达式

[编程题]计算表达式
  • 热度指数:24721 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
对于一个不存在括号的表达式进行计算

输入描述:
存在多组数据,每组数据一行,表达式不存在空格


输出描述:
输出结果
示例1

输入

6/2+3+3*4

输出

18
while True:
    try:
        print(int(eval(input().strip())))
    except:
        break
发表于 2019-08-28 13:44:20 回复(0)
importre
whileTrue:
    try:
        string=input()
        num,op=[],""
        num=re.findall("\d+",string)
        op=re.sub("\d","",string)
        lnum,lop,i,j,temp=len(num),len(op),0,0,0
        whilej<lop:
            ifop[j]=='+'orop[j]=='-':
                i=i+1
            ifop[j]=='*':
                i=i+1
                temp=float(num[i-1])*float(num[i])
                num[i-1]=temp
                delnum[i]
                i=i-1
            ifop[j]=='/':
                i=i+1
                temp=float(num[i-1])/float(num[i])
                num[i-1]=temp
                delnum[i]
                i=i-1
            ifop[j]=='^':
                i=i+1
                temp=float(num[i-1])**float(num[i])
                num[i-1]=temp
                delnum[i]
                i=i-1
            j=j+1
        i,j,result=0,0,0
        iflen(num)==1:
            print(num[0])
        else:
            whilej<lop:
                ifop[j]=='+':
                    result=float(num[i])+float(num[i+1])
                    num[i+1]=result
                    i=i+1
                ifop[j]=='-':
                    result=float(num[i])-float(num[i+1])
                    num[i+1]=result
                    i=i+1
                j=j+1
            print(int(num[i]))
                 
    except:
        break
 
发表于 2019-02-27 13:56:00 回复(0)
虽然python有内置函数直接算,但还是挑战一下,输出为整数
把结果保存为一个栈,最后所有数相加,
加法直接压栈,减法压栈相反数,乘法取出一个数相乘后压栈,除法取出一个数相除后压栈
while True:
    try:
        string = list(input())
        result,lastSign,temp = [],'+',''   #保存上一个符号
        while string:
            if string[0].isdigit():
                temp += string[0]
            else:
                if lastSign == '+':
                    result.append(float(temp))
                elif lastSign == '-':
                    result.append(-float(temp))
                elif lastSign == '*':
                    left = result.pop()
                    result.append(left*float(temp))
                elif lastSign == '/':
                    left = result.pop()
                    result.append(left/float(temp))
                lastSign = string[0]
                temp = ''                #如果当前为符号,那么计算后数字置空
            string.pop(0)
        if lastSign == '+':
            result.append(float(temp))
        elif lastSign == '-':
            result.append(-float(temp))
        elif lastSign == '*':
            left = result.pop()
            result.append(left * float(temp))
        elif lastSign == '/':
            left = result.pop()
            result.append(left / float(temp))
        print(int(sum(result)))
    except Exception:
        break
编辑于 2018-10-13 23:47:51 回复(0)

python one line solution:


while True:
    try:
        print(int(eval(input())))
    except:
        break
发表于 2017-10-01 17:27:40 回复(1)
from __future__ import division
try:
    while 1:
        print int(input())
except:
    pass

发表于 2016-12-28 13:52:49 回复(0)