簡體   English   中英

C 中的中綴到后綴轉換,多位輸入

[英]infix to postfix converstion in C with multiple digits input

我想要獲得的是一個計算器,它將采用中綴表示法,忽略無關緊要的空白字符,如“”或“@”,然后將該中綴表示法轉換為后綴表示法並進行簡單的計算,如加法、減法等。到目前為止的代碼正在以中綴表示法輸入輸入,以忽略無關緊要的空白字符並輸出后綴表示法的方式對其進行修剪。

#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 function 中執行計算,最終會導致正確的答案。 問題已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM