簡體   English   中英

從中綴到后綴

[英]Going from infix to postfix

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "stack.h"

#define MAX_EQU_LEN 100

static int prec(char operator)
{
    switch (operator)
    {
        case '*':
            return 5;
        case '/':
            return 4;
        case '%':
            return 3;
        case '+':
            return 2;
        case '-':
            return 1;
        default:
            break;
    }

    return 0;
}

static int isNumeric(char* num)
{
    if(atoi(num) == 0)
    {
        return 0;
    }
            return 1;
}

char* infix_to_postfix(char* infix)
{
    char* postfix = malloc(MAX_EQU_LEN);
    stack* s = create_stack();
    s->size = strlen(infix);
    node* tempPtr = s->stack;
    unsigned int i;
    char symbol,next;

    for(i = 0; i < s->size ; i++)
    {
        symbol = *((infix + i));
        tempPtr = s->stack;
        if(isNumeric(&symbol) != 1)
        {
            strcat(postfix, &symbol);
        }
        else if(symbol == '(')
        {
            push(s, symbol);
        }
        else if(symbol == ')')
        {
            while(s->size != 0 && top(s) != '(')
            {
                next = tempPtr->data;
                pop(s);
                strcat(postfix, &next);
                tempPtr = s->stack;
                if(tempPtr->data == '(')
                {
                    pop(s);
                }
            }
        }
        else
        {
            while(s->size != 0 && prec(top(s)) > prec(symbol))
            {
                next = tempPtr->data;
                pop(s);
                strcat(postfix, &next);
                push(s,next);
            }
        }
        while(s->size != 0)
        {
            next = tempPtr->data;
            pop(s);
            strcat(postfix, &next);
        }
    }
    return postfix;

}

int evaluate_postfix(char* postfix) {

    //For each token in the string
        int i,result;
        int right, left;
        char ch;
        stack* s = create_stack();
        node* tempPtr = s->stack;

        for(i=0;postfix[i] < strlen(postfix); i++){
            //if the token is numeric
            ch = postfix[i];
            if(isNumeric(&ch)){
                //convert it to an integer and push it onto the stack
                atoi(&ch);
                push(s, ch);
            }
            else
            {
                pop(&s[i]);
                pop(&s[i+1]);
                //apply the operation:
                //result = left op right
                       switch(ch)
                       {
                           case '+': push(&s[i],right + left);
                                     break;
                           case '-': push(&s[i],right - left);
                                     break;
                           case '*': push(&s[i],right * left);
                                     break;
                           case '/': push(&s[i],right / left);
                                     break;
                       }
                }
        }
        tempPtr = s->stack;
        //return the result from the stack
        return(tempPtr->data);

}

該文件是程序的一部分,該程序使用堆棧結構對輸入文件執行后綴綴。 其他功能已經過測試並且可以正常工作,但是當我嘗試添加此部分並實際執行操作時,程序分段錯誤。 調試器說它發生在infix_to_postfix函數中,但是它沒有說出哪一行,我也不知道在哪里。 有誰知道這會導致段錯誤嗎?

您做錯了幾件事:

    if(isNumeric(&symbol) != 1)

函數isNumeric()期望將一個以空終止的字符串作為輸入,而不是指向單個字符的指針。

        strcat(postfix, &symbol);

這里同樣適用。

        strcat(postfix, &next);

我猜這也是錯誤的。 如果要將單個字符轉換為字符串,可以執行以下操作:

char temp[2] = {0};

temp[0] = symbol;
strcat(postfix, temp);
static int isNumeric(char* num)
{
    if(atoi(num) == 0)
    {
        return 0;
    }
            return 1;
}

如果字符串為"0"怎么辦? 考慮改用strtol因為它提供了測試結果是否成功的更強大的方法。

無關的風格注釋:我的第一個功能看起來過於復雜。 盡管我做的方法很可能也過於復雜。

static int prec(char operator)
{
    switch (operator)
    {
        case '*':
            return 5;
        case '/':
            return 4;
        case '%':
            return 3;
        case '+':
            return 2;
        case '-':
            return 1;
        default:
            break;
    }

    return 0;
}

如果函數執行從一組到另一組的簡單映射,則通常可以更簡單(更快)地執行數組查找。 因此,我將從輸入字符的字符串開始。

char *operators = "*" "/" "%" "+" "-";

請注意,編譯器會將它們連接為帶有空終止符的單個字符串值。

int precedence[] = { 5, 4, 3, 2, 1, 0 };

然后測試char是否是運算符是:

#include <string.h>
if (strchr(operators, chr))
    ...;

獲得優先級變為:

p = precedence[strchr(operators, chr) - operators];

如果還有更多要與運算符關聯的值,我會考慮使用X-Macro生成表和一組關聯的enum值以用作符號索引。

暫無
暫無

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

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