c - 从中缀到后缀

标签 c data-structures stack infix-notation

#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);

关于c - 从中缀到后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482825/

相关文章:

java - Java 的 Set 是否实现了 UnionFind 算法?

Java:使用弱引用堆栈

assembly - x87 FP 堆栈仍然相关吗?

c++ - 使用位操作优化检查

ios - 在 Objective C 中动态设置 C 结构数组的大小?

c - P线程和信号

java - 这个整数堆栈的 Java 链表表示有什么问题?

c++ - 使用 Loadlibrary ("cmd.exe")但不起作用

data-structures - 使用二次探测实现哈希表的原因

java - 在不排序的情况下找到中间值