开发者

How do I create this calculator program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 13 hours ago.

Improve this question

Create a five function (arithmetic function), floating point calculator function th开发者_如何学Cat supports:

addition (+) subtraction (-) multiplication (*) division (/) exponentiation (^)

Explanation def calculate(exp: str) -> float: expects a string expression (" 5.5 * -2.3 ") parses the string into 3 components, 2 operands and an operator should support a string argument for the expression that has no white space, or more than one white space before or after the operator or either of the two operands does the operation with the 2 operands and returns the result as a float rounded to 3 decimals

Restrictions:

The following built-in functions may be used: len() round() any type casting function String methods and slicing may be used, excluding the string split() method.

I dont know where to start.


This is a gui calculator in tkinter, python. This could be a good starting point, if you are having trouble writing your first lines of code, depends if you want gui or not.

try:
    import tkinter as tk
except:
    import Tkinter as tk
calc = tk.Tk()
calc.title("ZC.OS Calculator")

buttons = [
'7',  '8',  '9',  '*',  'C',
'4',  '5',  '6',  '/',  'Neg',
'1',  '2',  '3',  '-',  '$',
'0',  '.',  '=',  '+',  '@' ]

# set up GUI
row = 1
col = 0
for i in buttons:
    button_style = 'raised'
    action = lambda x = i: click_event(x)
    tk.Button(calc, text = i, width = 7, height = 7, relief = button_style, command = action) \
        .grid(row = row, column = col, sticky = 'nesw', )
    col += 1
    if col > 4:
        col = 0
        row += 1

display = tk.Entry(calc, width = 40, bg = "white")
display.grid(row = 0, column = 0, columnspan = 5)

def click_event(key):

    # = -> calculate results
    if key == '=':
        # safeguard against integer division
        if '/' in display.get() and '.' not in display.get():
            display.insert(tk.END, ".0")
            
        # attempt to evaluate results
        try:
            result = eval(display.get())
            display.insert(tk.END, " = " + str(result))
        except:
            display.insert(tk.END, "   Error, use only valid chars")
            
    # C -> clear display        
    elif key == 'C':
        display.delete(0, tk.END)
        
        
    # $ -> clear display        
    elif key == '$':
        if display.get() == "42":
            display.delete(0, tk.END)
            display.insert(tk.END, "EASTEREGG1")        
                        
            
    # @ -> clear display        
    elif key == '@':
        display.delete(0, tk.END)
        display.insert(tk.END, "ZC.OS Calculator")      

        
    # neg -> negate term
    elif key == 'neg':
        if '=' in display.get():
            display.delete(0, tk.END)
        try:
            if display.get()[0] == '-':
                display.delete(0)
            else:
                display.insert(0, '-')
        except IndexError:
            pass

    # clear display and start new input     
    else:
        if '=' in display.get():
            display.delete(0, tk.END)
        display.insert(tk.END, key)

# RUNTIME
calc.mainloop()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜