Python with parentheses in the calculator

Source: Internet
Author: User

The calculator with parentheses is also the first self-feeling to finish the best after all really got one weeks although the early stage of the road is really hard to go I will try to refuel Python learn practical

Refer to two bloggers ' articles

Http://www.cnblogs.com/0zcl/p/5983236.html

http://www.cnblogs.com/loyfee/p/5764926.html#3538071

The general process is to first convert the input data into a list to remove null characters

Then find the innermost parentheses and then put the results back in the list and figure out the results.

Import Re#data= Input ("Please enter arithmetic:") def change_list (data): Data=re.sub (" ","", data) #将空格转为""# Print (data) data_list= []     forIinchdata:data_list.append (i) Num=0    Try:         while 1:            ifdata_list[0] =="-": data_list[0] = data_list[0] + data_list[1] Data_list.pop (1)            ifdata_list[num].isnumeric ():ifdata_list[num+1].isnumeric (): Data_list[num]= data_list[num]+data_list[num+1] Data_list.pop (num+1)                Else: Num+=1            Else: Num+=1except Indexerror ase: #print (E,"index out of bounds")        returnData_list #ifData_list.index (data_list[-1]) <Num: # print (data_list) # Breakdef COMPUTE (data): while 1: #判断乘除if "*" inchData or"/" inchData: forI,jinchEnumerate (data):ifj = ="*": Data[i+1] =int(data[i-1]) *int(data[i+1]) Data.pop (i-1) Data.pop (i-1)                     Break                ifj = ="/": Data[i+1] =int(data[i-1]) *int(data[i+1]) Data.pop (i-1) Data.pop (i-1)                     Break#判断加减if "+" inchData or"-" inchData: forI,jinchEnumerate (data):ifj = ="+": Data[i+1] =int(Data[i-1]) +int(Data[i +1]) Data.pop (i-1) Data.pop (i-1)                     Break                ifj = ="-": Data[i+1] =int(Data[i-1]) -int(Data[i +1]) Data.pop (i-1) Data.pop (i-1)                     Break        Else:            returnData Break#有括号和无括号运算"""If there are parentheses, the innermost "") will be used to find its first "(" using: Intercept the middle number of the operation, then return the list until there is no parentheses to jump out of the operation"""def parenth (data):if "(" inchData: while 1:            if ")" inchdata:b= Data.index (")") #表示第一个 ")" #print (b) forIinchRange (b,-1,-1): #截取最里层括号进行运算 #print (i)ifData[i] = ="(": New_list=[] new_list=data[i+1: b]byte=compute (new_list) [0] del (data[i:b+1]) Data.insert (i,byte)                         Break            Else:                returnCompute (data)Else: Compute (data) while 1: Data= Input ("Please enter the calculation:")    ifdata = ="":ContinueData=change_list (data) parenth (data) print (data[0])    ifInput"if input q exits: \ n") =="Q": Exit ()

Python with parentheses in the calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.