python 计算器实现

# -*- coding: utf-8 -*-

###########################################################################
## Python code generated with wxFormBuilder (version May 20 2018)
## http://www.wxformbuilder.org/
##
## PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc


###########################################################################
## Class MyFrame2
###########################################################################
import demo
class MyFrame2(wx.Frame):
    str = ''

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                          size=wx.Size(573, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        self.SetSizeHints(-1, -1)

        bSizer2 = wx.BoxSizer(wx.VERTICAL)

        self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"请输入数据", wx.DefaultPosition, wx.Size(-1, 25),
                                           wx.ALIGN_CENTRE | wx.ALIGN_LEFT | wx.ST_ELLIPSIZE_START)
        self.m_staticText1.Wrap(100)
        bSizer2.Add(self.m_staticText1, 0, wx.EXPAND | wx.ALL, 5)

        self.m_textCtrl4 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.m_textCtrl4, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 5)

        self.m_button2 = wx.Button(self, wx.ID_ANY, u"pow", wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_button2.Hide()

        bSizer2.Add(self.m_button2, 0, wx.ALL | wx.EXPAND, 5)

        self.m_textCtrl5 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_textCtrl5.Hide()

        bSizer2.Add(self.m_textCtrl5, 0, wx.ALL | wx.EXPAND, 5)

        fgSizer1 = wx.FlexGridSizer(0, 6, 0, 0)
        fgSizer1.SetFlexibleDirection(wx.BOTH)
        fgSizer1.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)

        self.m_button21 = wx.Button(self, wx.ID_ANY, u"+", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer1.Add(self.m_button21, 0, wx.ALL, 5)

        self.m_button3 = wx.Button(self, wx.ID_ANY, u"-", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer1.Add(self.m_button3, 0, wx.ALL, 5)

        self.m_button4 = wx.Button(self, wx.ID_ANY, u"*", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer1.Add(self.m_button4, 0, wx.ALL, 5)

        self.m_button5 = wx.Button(self, wx.ID_ANY, u"/", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer1.Add(self.m_button5, 0, wx.ALL, 5)

        self.m_button6 = wx.Button(self, wx.ID_ANY, u"cancle", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer1.Add(self.m_button6, 0, wx.ALL, 5)

        self.m_button7 = wx.Button(self, wx.ID_ANY, u"submit", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer1.Add(self.m_button7, 0, wx.ALL, 5)

        bSizer2.Add(fgSizer1, 1, wx.EXPAND, 5)

        self.SetSizer(bSizer2)
        self.Layout()

        self.Centre(wx.BOTH)

        # Connect Events
        self.m_button2.Bind(wx.EVT_BUTTON, self.m_button2OnButtonClick)
        self.m_button21.Bind(wx.EVT_BUTTON, self.m_button21OnButtonClick)
        self.m_button3.Bind(wx.EVT_BUTTON, self.m_button3OnButtonClick)
        self.m_button4.Bind(wx.EVT_BUTTON, self.m_button4OnButtonClick)
        self.m_button5.Bind(wx.EVT_BUTTON, self.m_button5OnButtonClick)
        self.m_button6.Bind(wx.EVT_BUTTON, self.m_button6OnButtonClick)
        self.m_button7.Bind(wx.EVT_BUTTON, self.m_button7OnButtonClick)

    def __del__(self):
        pass

    # Virtual event handlers, overide them in your derived class
    def m_button2OnButtonClick(self, event):
        event.Skip()

    def m_button21OnButtonClick(self, event):
        self.str = self.m_textCtrl4.GetValue() + '+'

    def m_button3OnButtonClick(self, event):
        self.str = self.m_textCtrl4.GetValue() + '-'

    def m_button4OnButtonClick(self, event):
        self.str = self.m_textCtrl4.GetValue() + '*'

    def m_button5OnButtonClick(self, event):
        self.str = self.m_textCtrl4.GetValue() + '/'

    def m_button6OnButtonClick(self, event):
        self.m_textCtrl4.SetValue('')

    def m_button7OnButtonClick(self, event):
        print(self.m_textCtrl4.GetValue())
        info = str(demo.direct_cal(self.m_textCtrl4.GetValue()))
        self.m_textCtrl4.SetValue(info)


app = wx.App(False)
frame = MyFrame2(None)
frame.Show(True)
app.MainLoop()
 
 

import re
class Stack:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return len(self.items) == 0

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        if not self.isEmpty():
            return self.items[len(self.items) - 1]

    def size(self):
        return len(self.items)


def direct_cal(infix):
    cal_class = {"*": 3, "/": 3, "+": 2, "-": 2, "(": 1}
    infix = " ".join(re.split(r'(\*|-|\+|/|\(|\)|)\s*', infix))
    list = infix.split()
    numStack = Stack()
    opStack = Stack()
    for x in list:
        if x in "0123456789101112131415161718192021222324252627282930":
            numStack.push(int(x))
        elif x == "(":
            opStack.push(x)
        elif x == ")":
            y = opStack.pop()
            while not y == "(":
                b = numStack.pop()
                a = numStack.pop()
                numStack.push(simple_cal(a, b, y))
                y = opStack.pop()
        elif x in "*/+-":
            while (not opStack.isEmpty()) and (cal_class[opStack.peek()] >= cal_class[x]):
                b = numStack.pop()
                a = numStack.pop()
                y = opStack.pop()
                numStack.push(simple_cal(a, b, y))
            opStack.push(x)
    while not opStack.isEmpty():
        b = numStack.pop()
        a = numStack.pop()
        y = opStack.pop()
        numStack.push(simple_cal(a, b, y))
    return numStack.pop()


def simple_cal(a, b, x):
    if x == "*":
        return a * b
    elif x == "/":
        return a / b
    elif x == "+":
        return a + b
    else:
        return a - b

# str = '1+(7-6)*3-10/5'


# print(direct_cal(str))





猜你喜欢

转载自blog.csdn.net/qq_33291307/article/details/80391879