1

我正在实现算术计算器,但出现错误:

错误:')' 标记之前的预期主表达式错误:'->' 标记之前的预期主表达式

我发布包含错误的行。

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    /*creating stack*/        
    typedef struct stack
    {
    int top;
    char *array;
    int max_size;
    }S;
    /*pushing character to it*/ 
    void push(S *st ,char ch)
    {
    if(st->top==st->max_size)
    {printf("already full..delete some items :)");return ;}
        printf("st->top=%d ",st->top);
        strcpy(&st->array[st->top],&ch);
        st->top++;
        printf("push=%s ",st->array[st->top-1]);
    }
    /*deleting character*/ 
    void pop(S *st)
    {
    if(st->top==0)
    {printf("it's empty..push some items :)");return ;}

        st->top--;
    }
    void fun(S *stack,S *post,char a)
    {
        while(strcmp((&stack->array[stack->top]),&a)!=0)
            {
                pop(stack);
                push(post,stack->array[stack->top+1]);

            }
            pop(stack);
    }
    int main()
    {
    int i,j;
    char str[10000];
    /*initialize 3 stacks*/
    S *st =init(10000);
    S *post=init(10000);
    S *ans=init(10000);
    / *actually code is very big so i am
    giving only lines in which there is error*/
    //some code...
    fun(stack,post,a);
    //some code...
             while(precedence(str[i])>precedence(stack->array[stack->top]))
    //some code...
    push(post,stack->array[stack->top]);
    pop(stack);
    //more code......
    }
4

2 回答 2

3

你有一个同名的结构和一个变量stack。这非常糟糕,让您和编译器感到困惑。结构/类名使用大写字母。

于 2013-01-18T14:40:38.263 回答
1

在您的void fun(S *stack,S *post,char a)功能中,您正在使用

strcmp((&stack->array[stack->top]),&a)!=0

比较两个chars。strcmp()用于比较字符串,因此请改用:

stack->array[stack->top] != a
于 2013-01-18T14:41:56.640 回答