c - RPn 处理负数除法

标签 c calculator postfix-notation

目前,我正在制作一个计算器,输入数学表达式并使用RPn进行计算。因此,我使用中缀到后缀转换器函数来转换它。计算器的工作原理是将数字插入堆栈并检测运算符。但我的计算器有一个缺陷,它无法处理负数除法,例如 1/-1。是我对 RPn 的理解错误还是我的中缀到后缀函数有问题?

检测数字和运算符

int isOperator(char e){
    if(e == '+' || e == '-' || e == '*' || e == '/' || e == '^')
        return 1;
    else
        return 0;
}

int isNumber(char c) {
    if ((c>='0' && c<='9') || c=='.') {
        return 1;
    }
    return 0;
}

将数学表达式转换为后缀

void pushPostfix(struct postfixStack* s,int item){
    if(s->top == (100-1)){
        printf("\nSTACK FULL");
    }
    else{
        ++s->top;
        s->data[s->top]=item;
    }
}

char popPostfix(struct postfixStack* s){
    char a=(char)-1;
    if(!isEmpty(s)){
        a= s->data[s->top];
        --s->top;
    }
    return a;
}

void infixToPostfix(char* infix, char * postfix) {
    char *i, *p;
    struct postfixStack stack;
    char n1;
    emptyStack(&stack);
    i = &infix[0];
    p = &postfix[0];

    while (*i) {
        while (*i == ' ' || *i == '\t') {
            i++;
        }
        if (isNumber(*i)) {
            while (isNumber(*i)) {
                *p = *i;
                p++;
                i++;
            }
            *p = ' ';
            p++;
        }
        if (*i == '(') {
            pushPostfix(&stack, *i);
            i++;
        }
        if (*i == ')') {
            n1 = popPostfix(&stack);
            while (n1 != '(') {
                *p = n1;
                p++;
                *p = ' ';
                p++;
                n1 = popPostfix(&stack);
            }
            i++;
        }
        if (isOperator(*i)) {
            if (isEmpty(&stack)) {
                pushPostfix(&stack, *i);
            }
            else {
                n1 = popPostfix(&stack);
                while (priority(n1) >= priority(*i)) {
                    *p = n1;
                    p++;
                    *p = ' ';
                    p++;

                    n1 = popPostfix(&stack);
                }
                pushPostfix(&stack, n1);
                pushPostfix(&stack, *i);
            }
            i++;
        }
    }
    while (!isEmpty(&stack)) {
        n1 = popPostfix(&stack);
        *p = n1;
        p++;
        *p = ' ';
        p++;
    }
    *p = '\0';
}

最佳答案

你可以考虑这样的事情。

   #define PLUS +
   #define MINUS - 
   #define UMINUS _
   #define DILIM_CLOSE 1
   #define DILIM_OPEN 0
   #define OPERAND 2
   #define FACT !

   // i is the present location being parsed in the infix string
   // infix is an array holding the infix string
   // this code can go to the tokeniser
   // optr_type returns the type of operator, or even if it is an operand

   if(infix[i]==PLUS || infix[i]==MINUS)
     {
         int sign=1,st=i;

         while(infix[i]==PLUS || infix[i]==MINUS)
             if(infix[i++]==MINUS)
                 sign*=-1;

         if(sign==-1)
         {

             if((optr_type(infix[st-1])==OPERAND) ||optr_type(infix[st-1])==DILIM_CLOSE || infix[st-1]==FACT)
                 return MINUS;

             else
                 return UMINUS;
         }

         if(sign==1)
         {
             if((optr_type(infix[st-1])==OPERAND) || optr_type(infix[st-1])==DILIM_CLOSE || infix[st-1]==FACT)
                 return PLUS;
             else
                 return UPLUS;
         }
     }

当您发现在一个加号或减号之后出现了一系列减号时,请继续翻转标志。如果 sign = -1,则用单个 - 替换负号条纹。例如 --1 = 1---1 = -1。在 sign == -1 条件下,如果 st 中保存的前一个位置是操作数、右括号(任何类型)或阶乘符号,则它是二进制负号,否则它必须是一元负号。与一元加相同。

一旦将运算符标记为一元,就很容易评估,但您需要根据运算符的类型来决定。如果是二元,则弹出两次并应用二元运算符,如果是一元,则弹出一次并应用运算符。

关于c - RPn 处理负数除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50517413/

相关文章:

c - 如果我执行这个程序,我会得到 "Failed to Free"。但是在FREE函数内部,它只是正确地释放了内存

macos - 如何在 Mac OS X 中查看/打开计算器?

Java 确定哪个文本字段具有焦点

java - 使用java在中缀到后缀应用程序中获取错误输出

python - 使用 python 计算数学表达式而不使用多个函数的算法

c - 如何让我的时钟应用程序运行

c - 从文本文件中读取多行字符

c - 在给定值之后反转链表,而不创建新节点

c - 如何在 C 中为矩阵使用相同的用户定义函数?

c++ - RPN短路评估