中缀转后缀并计算中加入'!'问题

问题遇到的现象和发生背景

想加入‘!’(阶乘运算)

问题相关代码,请勿粘贴截图
#include <iostream>
#include <Stack>
#include <string>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

string inTopostExp(string exp);    
int prior(char c); //judge the priority of the operator
int computepostExp(string postExp);
int Auxcal(int a,int b,char ch);

int Fact(int n)
{
    if(n < 0 || n > 9)
        cout<<"阶乘参数错误!";
    else if (1 == n)
        return 1;
    else
        return n*Fact(n - 1);
}
int main()
{
    string inexp,posexp;
   for(;;)
    {
        cout<<"输入中缀表达式:(输入exit退出)";
        getline(cin,inexp);
        if(inexp=="exit")
        break; 
        posexp=inTopostExp(inexp); 
        cout<<"后缀表达式为:"<<posexp<<endl;
        cout<<"计算结果为:"<<computepostExp(posexp)<<endl;
    } 
    return 1;
}

int Auxcal(int a,int b,char ch)
{
    switch(ch)
    {
        case'+':return a+b;
        case'-':return a-b;
        case'*':return a*b;
        case'/':return a/b;
        case'%':return a%b;
        case'^':return pow(a,b);
        case'!':return Fact(a);
    }
}

int computepostExp(string postExp)
{
    char curch;
    int num1,num2;
    stack<int> numStack; 
    
    for(int i=0;i<postExp.length();i++)
    {
        curch=postExp[i];
        if(postExp[i]==32)
        {
            continue;
        }
        else if(postExp[i]<'9'&&postExp[i]>'0')
        {
            int tmpnum=int(postExp[i])-'0';
            while(postExp[i+1]<'9'&& postExp[i+1]>'0')
            {
                tmpnum=tmpnum*10+int(postExp[i+1])-'0';
                i++;
            }
            numStack.push(tmpnum);
        }
        else
        {
            num2=numStack.top();
            numStack.pop();
            num1=numStack.top();
            numStack.pop();
            numStack.push(Auxcal(num1,num2,curch));
        }
    }
    return numStack.top();
}

int prior(char c)
{
    switch(c)
    {
        case'+':case'-':return 1;
        case'*':case'/':case'%':return 2;
        case'^':case'!':return 3;
    }
}

string inTopostExp(string exp)
{
    string postExp;
    char curtoken,toptoken;
    stack<char> opStack;
    for(int i=0;i<exp.length();i++)
    {
        curtoken=exp[i];
        if(isalnum(curtoken))
        {
            postExp.append(1,' ');
            postExp.append(1,curtoken);
            for(;isalnum(exp[i+1]);i++)
            {
                postExp.append(1,exp[i+1]);
            }
            continue;
        }
        else
        {
            switch(curtoken)
            {
                case '(':
                    opStack.push(curtoken);
                    break; 
                case')':
                    while(opStack.top()!='(')
                    {
                        toptoken=opStack.top();
                        postExp.append(1,toptoken);
                        opStack.pop();
                    }
                    opStack.pop();
                    break;
                case'+':case'-':case'*':case'/':case'%':case'^':case'!': 
                    for(;;)
                    {
                        if(opStack.empty())
                        {
                            opStack.push(curtoken);
                            break;
                        }
                        else if(opStack.top()=='(')
                        {
                            opStack.push(curtoken);
                            break;
                        }
                        else if(prior(opStack.top())<prior(curtoken))
                        {
                            opStack.push(curtoken);
                            break;
                        }
                        else   
//若栈顶符号优先级大于当前符号优先级,则该符号出栈,但此时当前符号不入栈,因为还需继续判断下一个优先级,也就是说这一步只是将操作符栈栈顶的符号出栈进入后缀表达式中。 
                        {
                            toptoken=opStack.top();
                            postExp.append(1,' ');
                            postExp.append(1,toptoken);
                            opStack.pop();
                        }
                    }
                    break;
            }
        }
    }
    //最后一个字符如果是数字,那么栈内还有没出栈的符号
    while(!opStack.empty())
    {
        toptoken=opStack.top();
        postExp.append(1,' ');
        postExp.append(1,toptoken);
        opStack.pop();
    } 
    return postExp;
}


————————————————
版权声明:本文为CSDN博主「弹指间LDL」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dalong277/article/details/51249914

运行结果及报错内容

img

我的解答思路和尝试过的方法

在主函数外面单写一个阶乘运算函数

我想要达到的结果

可以满足阶乘运算 并不应影响其他功能(中缀转后缀)