这段利用栈计算中缀表达式的代码中,是main函数哪里错了吗,为什么报错呢?

#include <iostream>
#include <math.h>
#define maxSize 100
#define MIN 0.000001

using namespace std;

int calsub(float opond1,char op,float opond2,float &result)
{
    if(op=='+') result=opond1+opond2;
    else if(op=='-') result=opond1-opond2;
    else if(op=='*') result=opond1*opond2;
    else if(op=='/')
    {
        if(fabs(opond2)<MIN)
        return 0;
        else 
        result=opond1/opond2;
    }
    return 1;
}


int getpriority(char op)
{
    if(op=='+' || op=='-')
    return 0;
    else 
    return 1;
}

int calStackTopTwo(float s1[],int &top1,char s2[],int &top2)
{
    float opnd1;
    float opnd2;
    float result;
    char op;
    int flag;
    opnd2=s1[top1--];
    opnd1=s1[top1--];
    op=s2[top2--];
    flag=calsub(opnd1,op,opnd2,result);
    if(flag==0)
    cout<<"error";
    else
    s1[++top1]=result;
    
}
float calinfix(char infix[])
{
    float s1[maxSize]; int top1=-1;
    char s2[maxSize]; int top2=-1;
    int i=0;
    while(infix[i]!='\0')
    {
        if('0'<=infix[i]&&infix[i]<='9')
        {
            s1[++top1]=infix[i]-'0';
            ++i;
        }
        else if(infix[i]=='(')
        {
            s2[++top2]='(';
            ++i;
        }
        else if(infix[i]=='+' ||
                infix[i]=='-' ||
                infix[i]=='*' ||
                infix[i]=='/')
        {
            if(top2==-1 || s2[top2]=='(' || getpriority(infix[i])>getpriority(s2[top2]))
            {
                s2[++top2]=infix[i];
                ++i;
            }
            else
            {
                calStackTopTwo(s1,top1,s2,top2);
            }
        }
        else if(infix[i]==')')
        {
            while(s2[top2]!='(')
            {
                calStackTopTwo(s1,top1,s2,top2);
            }
        }
    }
    while(top2!='0')
    {
        calStackTopTwo(s1,top1,s2,top2);
    }

    return s1[top1];
}

int main()
{
    char test[maxSize]={'(','1','+','1',')','/','3','\0'};
    cout<<calinfix(test);
    return 0;
}

 

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^