Как ты 2021-12-20 22:22 采纳率: 100%
浏览 28
已结题

c++实现了一个计算器,中缀转后缀,但如果计算到1/3*3时就会出错,它会先计算3*3,请问该怎么修改

c++实现了一个计算器,中缀转后缀,但如果计算到1/33时就会出错,它会先计算33

.h文件
#ifndef CALCULATOR_H
#define CALCULATOR_H

enum MAXSIZE
{
    STACK_SIZE = 30,//初始容量
    STACK_ADD = 10,//每次增加10个
    BUFF = 10,
    MAX_LEN = 100//设定最长为100
};

template<typename Type_init>
class Calculator
{
public:
    struct sqStack
    {
        Type_init* base;//指向栈顶
        Type_init* top;
        int stackSize;//当前栈的最大容量
    };
    Calculator();
    ~Calculator();
    void Push(Type_init e);
    bool Pop(Type_init& e);
    void clearStack();
    int StackLen();

    double Calculation(char Postfix[]);//后缀表达式的计算
    bool Change(char Infix[], char Postfix[]);//变为后缀表达式

private:
    sqStack s;

};

#endif // CALCULATOR_H


cpp文件:
#include<iostream>
#include "CALCULATOR.h"
#include <cstring>
#include <stdio.h>
using namespace std;

template<typename Type_init>
Calculator<Type_init>::Calculator()
{
    s.base = new Type_init[STACK_SIZE];
    if (s.base == NULL)
        exit(0);
    s.top = s.base;
    s.stackSize = STACK_SIZE;
}

//销毁
template<typename Type_init>
Calculator<Type_init>::~Calculator()
{
    delete[]s.base;
}

template<typename Type_init>

void Calculator<Type_init>::Push(Type_init e)
{
    if (s.top - s.base >= s.stackSize)
    {
        s.base = (Type_init*)realloc(s.base, (s.stackSize + STACK_ADD) * sizeof(Type_init));
        //        
        if (s.base == NULL)
            exit(0);
        s.top = s.base + s.stackSize;//重新设置栈顶
        s.stackSize = s.stackSize + STACK_ADD;//当前栈的最大容量
    }
    *(s.top) = e;
    s.top++;
}

template<typename Type_init>
bool Calculator<Type_init>::Pop(Type_init& e)
{
    if (s.top == s.base)
        return false;//空栈
    e = *(--(s.top));
    return true;
}

//清空栈
template<typename Type_init>
void Calculator<Type_init>::clearStack()
{
    s.top = s.base;
}

//计算栈的当前容量
template<typename Type_init>
int Calculator<Type_init>::StackLen()
{
    return s.top - s.base;
}

template<typename Type_init>
double Calculator<Type_init>::Calculation(char Postfix[]) //表达式计算
{
    int i = 0, j;
    char c;
    char str[BUFF];
    double a = 0, b = 0;

    for (j = 0; Postfix[j] != '\0'; j++)
    {
        while ((Postfix[j] >= 48) && (Postfix[j] <= 57) || Postfix[j] == '.') //输入的是数字
        {
            str[i] = Postfix[j];
            i++;
            str[i] = '\0';
            if (i >= 10)
            {
                printf("出错,输入的数据长度过大!\n");
                return -1;
            }
            j++;
            if ((Postfix[j] == ' '))
            {
                a = atof(str); //参数 str 所指向的字符串转换为一个浮点数
                Push(a);//入栈
                i = 0;
            }

        }

        switch (Postfix[j])
        {
        case '+':
            Pop(a);
            if (!Pop(b))//防止这是符号位
            {
                Push(a);
                break;
            }
            printf("%f+%f=%f\n", b, a, b + a);
            //Pop(b);
            Push(b + a);
            break;
        case '-':
            Pop(a);
            if (!Pop(b))//
            {
                Push(-a);
                break;
            }
            printf("%f-%f=%f\n",b,a,b-a);
            Push(b - a);
            break;
        case '*':
            Pop(a);
            Pop(b);
            printf("%f*%f=%f\n",b,a,b*a);
            Push(b * a);
            break;
        case '/':
            Pop(a);
            if (a == 0)
            {
                printf("除数不能为零 !\n");
                return -1;
            }
            Pop(b);
            printf("%f/%f=%f\n",b,a,b/a);
            Push(b / a);

            break;
        default:
            break;
        }
    }
    Pop(a);
    return a;

}

template<typename Type_init>
bool Calculator<Type_init>::Change(char Infix[], char Postfix[])
{
    Calculator<char> s;
    int i = 0, j = 0;
    char e;

    printf("中缀表达式为:");
    while (Infix[j] != '\0')
    {
        while (Infix[j] >= '0' && Infix[j] <= '9')
        {
            printf("%c", Infix[j]);
            Postfix[i++] = Infix[j++];
            if (Infix[j] < '0' || Infix[j]>'9')
            {
                Postfix[i++] = ' ';
                printf(" ");
            }
        }

        switch (Infix[j])
        {

        case ')':
            s.Pop(e);
            while ('(' != e)
            {
                printf("%c ", e);
                Postfix[i++] = e;
                Postfix[i++] = ' ';
                s.Pop(e);
            }
            break;

        case '+':
        case '-':
            if (0 == s.StackLen())
                s.Push(Infix[j]);
            else
            {
                do
                {
                    s.Pop(e);
                    if ('(' == e)
                    {
                        s.Push(e);
                    }
                    else
                    {
                        printf("%c ", e);
                        Postfix[i++] = e;
                        Postfix[i++] = ' ';
                    }
                } while (s.StackLen() && '(' != e);
                s.Push(Infix[j]);
            }
            break;

        case '*':
        case '/':
        case '(':
            s.Push(Infix[j]);
            break;

        case '\0':
            break;

        default:
            printf("\n输入格式错误!\n");
            return -1;
        }

        if ('\0' == Infix[j])
            break;
        j++;
    }

    while (s.StackLen())
    {
        s.Pop(e);
        printf("%c ", e);
        Postfix[i++] = e;
        Postfix[i++] = ' ';
    }
    Postfix[i] = '\0';
    printf("\n");
    return true;
}

int main()
{
    Calculator<double> cal;
    char Infix[MAX_LEN], Postfix[MAX_LEN];
    gets_s(Infix);
    double sum;

    cal.Change(Infix, Postfix);
    sum = cal.Calculation(Postfix);
    printf("最终计算结果为:%f\n\n", sum);
    return 0;
}

最后结果

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-Time 2021-12-20 23:34
    关注

    你利用了栈的结构。但是 / * 是同级的
    a+b-c a-c+b 运行的时候你没有发现。
    栈是 先 / 后 * 而Pop时 是 先* 后 /
    所以你的最终结果不对。
    建议参考 对表达式符号的处理。
    https://blog.csdn.net/qianyayun19921028/article/details/89228263

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 12月20日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理