使用逆波兰表示法和堆栈的计算器

标签 c stack

大家好,我遇到了段错误,您能帮忙吗? 如果我有这个运算符“3 5 +”,这意味着 3+5 就像“9 8 * 5 + 4 + sin”,“sin(((9*8)+5)+4)” 所以我的想法是检查第一个和第二个是否是数字并将它们插入堆栈中,然后当我有运算符时我弹出数字并进行计算,然后再次插入答案。 `

typedef struct st_node {
    float val;
    struct st_node *next;
} t_node;

typedef t_node t_stack;


// a function to allocate memory for a stack and returns the stack
t_stack* fnewCell() {
    t_stack* ret;
    ret = (t_stack*) malloc(sizeof(t_stack));
    return ret;
}
// a function to allocate memory for a stack, fills it with value v and pointer n ,    and returns the stack
t_stack* fnewCellFilled(float v, t_stack* n) {
    t_stack* ret;
    ret = fnewCell();
    ret->val = v;
    ret->next =n;
    return ret;
}

//function to initialize stack
void initstack(t_stack** stack) {
    fnewCellFilled(0,NULL);
}

// add new cell
void insrtHead(t_stack** head,float val) {
    *head = fnewCellFilled(val,*head);
}

//function to push the value v into the stack s 
void push(t_stack **s, float val) {
    insrtHead(s,val);
}

//function to pop a value from the stack and returns it
int pop(t_stack **s) {
    t_stack* tmp;
    int ret;
    tmp = (*s)->next;
    ret = (*s)->val;
    free(*s);
    (*s) = tmp;
    return ret;
}

int isempty (t_stack *t) {
    return t == NULL;
}


//function to transfer a string(str) to int (value)
//returns -1 when success , i otherwise
int str2int(char *str,int *value) {
    int i;
    *value = 0;
    int sign=(str[0]=='-' ? -1 : 1);
    for(i=(str[0]=='-' ? 1 : 0);str[i]!=0;i++) {
        if(!(str[i]>=48 && str[i]<=57)) // Ascii char 0 to 9
            return i;
        *value= *value*10+(str[i]-48);
    }
    *value = *value * sign;
    return -1;
}

//a function that takes a string, transfer it into integer and make operation using a stack
void function(t_stack *stack, char *str)
{
    char x[10]=" ";
    int y,j,i=0,z;
    printf("++\n");
    if(str[i] != '\0') {
        strcpy(x, strtok(str, " "));
        z= str2int(x, &y);
        if(z == -1) 
        {
            push(&stack,y);
            i=i+2;
        }
    } 
    while(str[i] != '\0')
    {
        strcpy(x, strtok(NULL, " "));
        z= str2int(x, &y);
        if(z == -1) 
        {
            printf("yes %d",y);
            push(&stack,y);
            i=i+2;  
        }
        else 
        { 
            y=pop(&stack);
            j=pop(&stack);

            if(x[0] == '+' ) push(&stack,y+j);
                else if (x[0] == '-' ) push(&stack,j-y);
                else if(x[0] == '*' ) push(&stack,j*y);
                else if(x[0] == '/') push (&stack ,j/y);
        } 
    }
} 

int main() {
    t_stack *s;
    initstack(&s);
    char *str="3 5 +";    
    function(s,str);
    return 0;
}

`

最佳答案

我注意到的地方:

1)

void initstack(t_stack** stack) {
    fnewCellFilled(0,NULL);
}

可能

void initstack(t_stack** stack) {
    *stack=fnewCellFilled(0,NULL);
}

2)

字符串文字被 strtok 更改

char *str="3 5 +";

应该是

char str[]="3 5 +";    

3)

while(str[i] != '\0')
{
    strcpy(x, strtok(NULL, " "));
    z= str2int(x, &y);
    if(z == -1) 
    {
        printf("yes %d",y);
        push(&stack,y);
        i=i+2;  
    }
    else 
    { 
        y=pop(&stack);
        j=pop(&stack);
        if(x[0] == '+' ) push(&stack,y+j);
        else if(x[0] == '-' ) push(&stack,j-y);
        else if(x[0] == '*' ) push(&stack,j*y);
        else if(x[0] == '/') push (&stack ,j/y);
    } 

可能

while(str[i] != '\0')
{
    strcpy(x, strtok(NULL, " "));
    z= str2int(x, &y);
    if(z == -1) 
    {
        printf("yes %d",y);
        push(&stack,y);
        i=i+2;  
    }
    else 
    { 
        y=pop(&stack);
        j=pop(&stack);
        if(x[0] == '+' ) push(&stack,y+j);
        else if(x[0] == '-' ) push(&stack,j-y);
        else if(x[0] == '*' ) push(&stack,j*y);
        else if(x[0] == '/') push (&stack ,j/y);
        i += 1;//need increment i
    } 

关于使用逆波兰表示法和堆栈的计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17396918/

相关文章:

c++ - 基于堆栈的迷宫探索

flutter :没有名称为 'overflow' 的命名参数,溢出:Overflow.visible,

c - if else 语句在 C 中不起作用

c - 在宏和主函数中打印的数字总和给了我不同的输出,我是宏的初学者

c - C 中的头文件和源文件如何工作?

c - 为 Core 2 或 Core i7 架构完全优化的 memcpy/memmove?

programming-languages - 为什么堆栈溢出仍然是一个问题?

c++ - 调试构建中简单函数的大量堆栈使用

java - java 中的 n 皇后拼图使用堆栈和回溯

c - 如何在需要 LPCOLESTR 的函数中使用 argv[]