YoujiaZhang / commit-Utterances

1 stars 1 forks source link

%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/mnist/ #3

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

MNIST · 手写识别计算器 - Yj-Zhang's Blog

首先扩展MNIST数据集,然后针对图片上的字符进行图像分割,最后使用TensorFlow构建模型识别分割后的字符。

https://youjiazhang.github.io/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/mnist/

Celoria commented 2 years ago

您好,我在参考您的代码时,发现您的计算部分可能有些问题,形如9+10÷2这样的算式,在我本地的计算结果好像不太正确,且我不太看得懂您的前缀表达式的写法quq。这是我在您原代码的基础上修改的后缀表达式的代码,并完善了(可能的)计算错误的问题。

def Infix2Prefix(self, infixstrs):
        """
        strs: 中缀式字符串
        return: 转换后的前缀式
        """
        infixstrs = infixstrs.replace(' ','')
        stack = []
        result = []
        # 判断算式格式是否正确
        if self.isFormula(infixstrs) is not True:
            return "请输入正确格式的算式!!!"

        infixstrs = infixstrs.replace(' ', '')
        intflag = 0
        decimalflag = 0
        strs = infixstrs
        # print("trans::", strs)
        for i in strs:
            # print("test::", stack, ' ', result)
            if i.isdigit():
                if intflag == 0:
                    intflag = 1
                decimalflag = decimalflag * 10 + int(i)
            elif i == ')':
                if intflag != 0:
                    stack.append(decimalflag)
                    intflag = 0
                    decimalflag = 0
                while stack[-1] != '(':
                    result.append(stack.pop())
                stack.pop()
            else:
                if intflag != 0:
                    result.append(decimalflag)
                    intflag = 0
                    decimalflag = 0
                if i != '(':
                    while len(stack) > 0 and self.Operator[stack[-1]] >= self.Operator[i]:
                        result.append(stack.pop())
                stack.append(i)
        if intflag != 0:
            result.append(decimalflag)
        while len(stack) > 0:
                result.append(stack.pop())
        return result

    def calculate(self, strs):
        # 根据转换后的式子计算最终的结果
        result = []  
        if strs == '请输入正确格式的算式!!!':
            return ['Input error']
        # print("cal::", strs)
        for i in strs :
            if i in self.Operator.keys():
                operandB = result.pop()
                operandA = result.pop()
                calres = self.operate(operandA, operandB, i)
                result.append(calres)
            else:
                result.append(i)
            # print("res::", result)
        return result[0]