中缀到具有多个数字输入的 C 中的后缀转换

问题描述

我想要得到的是一个计算器,它会采用中缀表示法,忽略无意义的空白字符,如“”或“@”,然后将该中缀表示法转换为后缀表示法并进行简单的计算,如加法、减法等。所以到目前为止,代码以中缀表示法输入输入,以忽略无关紧要的空白字符并输出后缀表示法的方式对其进行修剪。

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>;
#include <ctype.h>;

#define MAX_LENGTH  100

//Functions
void push(char x);
char pop();
void trimstring(char string[],char newString[]);
void inputTorpn(char trimmedExp[],char rpnExp[]);
int calculaterpn(char rpnExp[]);


char stack[MAX_LENGTH];
char resstack[MAX_LENGTH];
int top = -1;
int resTop = -1;
int index = 0;

int main() {
    int res;

    char exp[MAX_LENGTH] = "10 +2";
    char trimmedExpression[MAX_LENGTH];
    char rpnExpression[MAX_LENGTH];

    // Input commented out as per suggestion in comments
    //printf("Enter expression : ");
    //fgets(exp,100,stdin);
    printf("Infix expression: %s \n",exp);
    trimstring(exp,trimmedExpression);
    printf("\n");
    inputTorpn(trimmedExpression,rpnExpression);
    res = calculaterpn(rpnExpression);

    //printf("Result of calculation: %d",res);
    return 0;
}

void push(char x) {
    stack[++top] = x;
}

char pop() {
    if (top == -1)
        return -1;
    else
        return stack[top--];
}

int priority(char x) {
    if (x == '(')
        return 0;
    if (x == '+' || x == '-')
        return 1;
    if (x == '*' || x == '/')
        return 2;
    return 0;
}

void trimstring(char string[],char newString[]) {
    int i = 0,j = 0;
    while (string[i] != '\0' && string[i] != 10) {
        // Range of significant characters
        if (string[i] >= '(' && string[i] <= '9') {
            newString[j] = string[i];
            i++,j++;
        }
        else {
            i++;
        }
    }
    newString[j] = 0;
}

void inputTorpn(char trimmedExp[],char rpnExp[]) {
    char* e,x;
    e = trimmedExp;

    while (*e != '\0') {
        // Add to rpn if character is alphanumeric
        if (isalnum(*e)) {
            rpnExp[index] = *e;
            index++;
        }
        // Add to stack if is an open brace
        else if (*e == '(')
            push(*e);
        // Add all operators to the expression until finding open braces
        else if (*e == ')') {
            while ((x = pop()) != '(') {
                rpnExp[index] = x;
                index++;
            }
        }
        // If none of the above,that is an operator - check it's priority.
        // If it's priority is less that that of the one on top of the stack add the operator from the top of the stack to the expression; untill it's priority is higher.
        // At the end add current operator to the stack.
        else {
            while (priority(stack[top]) >= priority(*e)) {
                rpnExp[index] = pop();
                index++;
            }
            push(*e);
        }
        e++;
    }

    while (top != -1) {
        rpnExp[index] = pop();
        index++;
    }

    // Terminating character at the end of the string
    rpnExp[index] = 0;
}

void pushRes(char x) {
    printf("pushing: %c \n",x);
    resstack[++resTop] = x;
}

char popRes() {
    printf("poping \n");
    if (resTop == -1)
        return -1;
    else
        return resstack[resTop--];
}

int isValidOperator(char c) {
    if (c == '/' || c == '*' || c == '+' || c == '-')
        return 1;
    else
        return 0;
}

int calculaterpn(char rpnExp[]) {
    // Doesnt do anything yet,just prints out the compiled reverse polish notation
    char* c;
    int result = 0;
    c = rpnExp;

    printf("Postfix expression: %s",rpnExp);

    return result;
}

我偶然发现的问题是,当中缀输入有多个数字时,比如 10+2,代码将单独处理每个数字。因此在计算结果时整个表达式将无效。我几乎可以肯定问题出在这代码中:

// Add to rpn if character is alphanumeric
if (isalnum(*e)) {
    rpnExp[index] = *e;
    index++;
}

尽管我不知道在将多个数字添加到表达式时应该如何处理它们,因为输入是字符形式,并且可能有 N 个数字具有相应的 ascii 值,范围从 0- 9.期待您的回答。

编辑:使代码编译并且输入被硬编码。

解决方法

好的,多亏了 Bodos 的建议,我已经解决了这个问题。在本节中添加一个 while 循环:

  if (isalnum(*e)) {
        rpnExp[index] = *e;
        index++;
    }

使我能够在每个数字(包括 N 位数字)后添加一个字符。 多亏了这一点,我后来能够在 calculateRPN 函数中执行计算,最终会导致正确的 answear。 问题已解决。