c - 警告 : passing argument 1 of 'pushPost' makes pointer from integer without a cast [-Wint-conversion] in my infix to postfix progam

标签 c

所以这两个错误是 警告:传递“pushPost”的参数 1 使指针来自整数而不进行强制转换 [-Wint-conversion] 注意:预期为“char *”,但参数的类型为“char”

我一直在论坛上查找,但我仍然不明白我的问题是什么。请帮忙。

说明:

我有两个堆栈。堆栈和发布。 Stack 是我存储运算符的地方,Post 是我存储后缀的地方。

错误发生在任何带有pushStack 或pushPost 的行中。但对于我的一生,我无法弄清楚他们出了什么问题。

#include <stdio.h>
#include <stdlib.h>

//max size of the stack
#define MAXSIZE 256

//create Stack and topStack
char Stack[MAXSIZE];
int topStack = -1;

//
//

//create Post and topPost
char Post[MAXSIZE];
int topPost = -1;


//push for Stack
void pushStack(char *x)
{
    char data;
    x = &data;
    if(topStack >= MAXSIZE -1)
        printf("Stack overflow");
    else
        Stack[++topStack] = data;
}

//pop for Stack
int popStack()
{
    if (topStack < 0)
    {
        printf("Stack underflow\n");
        return -9999;
    } else
        return Stack[topStack--];
}

//peek for Stack
int peekStack()
{
    return Stack[topStack];
}

//isEmpty for Stack
int isEmptyStack()
{
    if (topStack < 0)
    {
        printf("Stack is empty\n");
        return 1;
    } else {
        return 0;
    }
}

//isFull for Stack
int isFullStack()
{
    if (topStack >= MAXSIZE -1)
    {
        printf("Stack is full\n");
        return 1;
    } else{
        return 0;
    }
}

//
//
//
//

//push for Post
void pushPost(char *x)
{
    char data;
    x = &data;

    if(topPost >= MAXSIZE -1)
        printf("Post overflow");
    else
        Post[++topPost] = data;
}

//pop for Post
int popPost()
{
    if (topPost < 0)
    {
        printf("Post underflow\n");
        return -9999;
    } else
        return Post[topPost--];
}

//peek for Post
int peekPost()
{
    return Post[topPost];
}

//isEmpty for Post
int isEmptyPost()
{
    if (topPost < 0)
    {
        printf("Post is empty\n");
        return 1;
    } else {
        return 0;
    }
}

//isFull for Post
int isFullPost()
{
    if (topPost >= MAXSIZE -1)
    {
        printf("Post is full\n");
        return 1;
    } else{
        return 0;
    }
}

//
//
//
//
//

//start of main
int main()
{
    //listing out all the variables and arrays needed
    char answer = 'n';
    char number[256];
    char infix[256];
    char parR = ')';
    char parL = '(';
    char mod = '%';
    char mult = '*';
    char div = '/';
    char sub = '-';
    char add = '+';
    char exp = '^';
    int c = 0;
    int i = 0;
    int x = 0;
    int num;

    //collect the infix loop
    //if the control variable at the end isn't set to y or Y, it loops
    while (answer == 'n' || answer == 'N')
    {
        //collect the infix
        printf("Please enter the equation: ");
        scanf(" %s", infix);
        //check for accuracy
        printf("\nThe entered equation is: %s\n", infix);
        printf("Is this correct? (y/n): ");
        scanf(" %c", &answer);

        //I added this to make the loop typo-proof
        //I split it into two if statements so it wasn't as long
        //it catches all the letters around n
        if (answer == 'b' || answer == 'B' || answer == 'm' || answer == 'M')
            answer = 'n';
        if (answer == 'h' || answer == 'H' || answer == 'j' || answer == 'J')
            answer = 'n';
    }

    //the headings for the table that displays everything
    //printf("Scanned     Stack     Postfix\n");

    //the great big loop that does everything
    while(c <= 2)
    {

        //if it is a digit, it moves the digit to postfix
        if (isdigit(infix[c]) != 0)
        {
            pushPost(infix[c]);


            /* This code is my attempt to read double digit numbers, but it has problems so I've temporarily set it aside
            //declare x = 0 for when the whole thing loops again
            x = 0;

            //this loop is designed to catch double digit numbers
            while (isdigit(infix[c+x]) != 0)
            {
                x++;
            }

            //this loop pushes all the digit elements to postfix
            //what I want to do is put all the digits in a variable
            //then push that to postfix
            for (i = 0; i <= x; i++)
            {
                //number is filled with digit values
                number[i] = infix[c+i];
                //copy number[] to num
                sscanf(number, "%d", &num);
                //number is pushed to the top of the stack
                pushStack(num);
            }

            //all of the digits we just messed with are deleted
            for (i = 0; i <= x; i++)
                popStack();
            */

        //if it isn't a digit it checks what kind of operator it is
        } else {

            //dealing with left parenthesis
            if (infix[c] == parL);
                pushStack(infix[c]);

            //dealing with percent sign
            if (infix[c] == mod);
            {
                //a loop to make it pop all the same or higher priority operators
                while (peekStack() == mult || peekStack() == div || peekStack() == mod)
                {
                    //if a mult is encountered, because mult has the same priority as mod
                    //mult is pushed to postfix then popped from stack
                    if (peekStack() == mult)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a div is encountered, because div has the same priority as mod
                    //div is pushed to postfix then popped from stack
                    if (peekStack() == div)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a mod is encountered, because mod has the same priority as mod
                    //mod is pushed to postfix then popped from stack
                    if (peekStack() == mod)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an add is encountered, because add has a lower priority than mod
                    //mod is pushed to stack
                    if (peekStack() == add)
                        pushStack(infix[c]);

                    //if an sub is encountered, because sub has a lower priority than mod
                    //mod is pushed to stack
                    if (peekStack() == sub)
                        pushStack(infix[c]);

                    //if an exp is encountered, because exp has a lower priority than mod
                    //mod is pushed to stack
                    if (peekStack() == exp)
                        pushStack(infix[c]);

                    //if a parL is encountered, just push mod to stack
                    if (peekStack() == parL)
                        pushStack(infix[c]);
                }
            }

            //dealing with multiplication sign
            if (infix[c] == mult);
            {
                //a loop to make it pop all the same or higher priority operators
                while (peekStack() == mult || peekStack() == div || peekStack() == mod)
                {
                    //if a mult is encountered, because mult has the same priority as mult
                    //mult is pushed to postfix then popped from stack
                    if (peekStack() == mult)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a div is encountered, because div has the same priority as mult
                    //div is pushed to postfix then popped from stack
                    if (peekStack() == div)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a mod is encountered, because mod has the same priority as mult
                    //mod is pushed to postfix then popped from stack
                    if (peekStack() == mod)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an add is encountered, because add has a lower priority than mult
                    //mult is pushed to stack
                    if (peekStack() == add)
                        pushStack(infix[c]);

                    //if an sub is encountered, because sub has a lower priority than mult
                    //mult is pushed to stack
                    if (peekStack() == sub)
                        pushStack(infix[c]);

                    //if an exp is encountered, because exp has a lower priority than mult
                    //mult is pushed to stack
                    if (peekStack() == exp)
                        pushStack(infix[c]);

                    //if a parL is encountered, just push mult to stack
                    if (peekStack() == parL)
                        pushStack(infix[c]);
                }
            }

            //dealing with division sign
            if (infix[c] == div);
            {
                //a loop to make it pop all the same or higher priority operators
                while (peekStack() == mult || peekStack() == div || peekStack() == mod)
                {
                    //if a mult is encountered, because mult has the same priority as div
                    //mult is pushed to postfix then popped from stack
                    if (peekStack() == mult)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a div is encountered, because div has the same priority as div
                    //div is pushed to postfix then popped from stack
                    if (peekStack() == div)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a mod is encountered, because mod has the same priority as div
                    //mod is pushed to postfix then popped from stack
                    if (peekStack() == mod)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an add is encountered, because add has a lower priority than div
                    //div is pushed to stack
                    if (peekStack() == add)
                        pushStack(infix[c]);

                    //if an sub is encountered, because sub has a lower priority than div
                    //div is pushed to stack
                    if (peekStack() == sub)
                        pushStack(infix[c]);

                    //if an exp is encountered, because exp has a lower priority than div
                    //div is pushed to stack
                    if (peekStack() == exp)
                        pushStack(infix[c]);

                    //if a parL is encountered, just push div to stack
                    if (peekStack() == parL)
                        pushStack(infix[c]);
                }
            }

            //dealing with subtraction sign
            if (infix[c] == sub);
            {
                //a loop to make it pop all the same or higher priority operators
                while (peekStack() == mult || peekStack() == div || peekStack() == mod || peekStack() == add || peekStack() == sub)
                {
                    //if a mult is encountered, because mult has higher priority than sub
                    //mult is pushed to postfix then popped from stack
                    if (peekStack() == mult)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a div is encountered, because div has higher priority than sub
                    //div is pushed to postfix then popped from stack
                    if (peekStack() == div)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a mod is encountered, because mod has higher priority than sub
                    //mod is pushed to postfix then popped from stack
                    if (peekStack() == mod)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an add is encountered, because add has the same priority as sub
                    //sub is pushed to stack
                    if (peekStack() == add)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an sub is encountered, because sub has the same priority as sub
                    //sub is pushed to stack
                    if (peekStack() == sub)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an exp is encountered, because exp has a lower priority than sub
                    //sub is pushed to stack
                    if (peekStack() == exp)
                        pushStack(infix[c]);

                    //if a parL is encountered, just push sub to stack
                    if (peekStack() == parL)
                        pushStack(infix[c]);
                }
            }

            //dealing with addition sign
            if (infix[c] == add);
            {
                //a loop to make it pop all the same or higher priority operators
                while (peekStack() == mult || peekStack() == div || peekStack() == mod || peekStack() == add || peekStack() == sub)
                {
                    //if a mult is encountered, because mult has higher priority than add
                    //mult is pushed to postfix then popped from stack
                    if (peekStack() == mult)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a div is encountered, because div has higher priority than add
                    //div is pushed to postfix then popped from stack
                    if (peekStack() == div)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a mod is encountered, because mod has higher priority than add
                    //mod is pushed to postfix then popped from stack
                    if (peekStack() == mod)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an add is encountered, because add has the same priority as add
                    //add is pushed to stack
                    if (peekStack() == add)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an sub is encountered, because sub has the same priority as add
                    //add is pushed to stack
                    if (peekStack() == sub)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an exp is encountered, because exp has a lower priority than add
                    //add is pushed to stack
                    if (peekStack() == exp)
                        pushStack(infix[c]);

                    //if a parL is encountered, just push add to stack
                    if (peekStack() == parL)
                        pushStack(infix[c]);
                }
            }

            //dealing with exponent sign
            if (infix[c] == exp);
            {
                //a loop to make it pop all the same or higher priority operators
                while (peekStack() == mult || peekStack() == div || peekStack() == mod || peekStack() == add || peekStack() == sub || peekStack() == exp)
                {
                    //if a mult is encountered, because mult has higher priority than exp
                    //mult is pushed to postfix then popped from stack
                    if (peekStack() == mult)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a div is encountered, because div has higher priority than exp
                    //div is pushed to postfix then popped from stack
                    if (peekStack() == div)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a mod is encountered, because mod has higher priority than exp
                    //mod is pushed to postfix then popped from stack
                    if (peekStack() == mod)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an add is encountered, because add has higher priority than exp
                    //add is pushed to postfix then popped from stack
                    if (peekStack() == add)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an sub is encountered, because sub has higher priority than exp
                    //sub is pushed to postfix then popped from stack
                    if (peekStack() == sub)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if an exp is encountered, because exp has the priority ass exp
                    //exp is pushed to stack
                    if (peekStack() == exp)
                    {
                        pushPost(peekStack);
                        popStack();
                    }

                    //if a parL is encountered, just push exp to stack
                    if (peekStack() == parL)
                        pushStack(infix[c]);
                }
            }

            //dealing with right parenthesis
            if (infix[c] == parR);
            {
                //while the top of stack isn't a '(' it pushes the top of stack to postfix
                while (Stack[peekStack()] != parL)
                {
                    pushPost(peekStack());
                    popStack();
                }
            }
        }

        c++;
    }

    return 0;
}

最佳答案

您的函数void pushPost(char *x)采用指针参数。这对于该功能来说不是必需的,您可以修改它。

void pushPost(char data)
{
    if(topPost >= MAXSIZE -1)
        printf("Post overflow");
    else
        Post[++topPost] = data;
}

pushStack类似

void pushStack(char data)
{
    if(topStack >= MAXSIZE -1)
        printf("Stack overflow");
    else
        Stack[++topStack] = data;
}

您还可以像这样调用该函数.. pushPost(peekStack); 其中 peekStack 本身就是一个返回 int 的函数。您实际上是使用函数指针调用函数pushPost,在这种情况下这是不允许或不需要的。

需要修改为pushPost(peekStack());

此外,pushPostpeekStack 在代码中定义了两次。您需要删除不需要的定义。

函数isDigit需要包含库ctype.h

关于c - 警告 : passing argument 1 of 'pushPost' makes pointer from integer without a cast [-Wint-conversion] in my infix to postfix progam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54836391/

相关文章:

c - 如何将结构转换为 uint8_t(错误 : conversion to non-scalar type requested)

c - C程序中的内存分配

c - 为什么这个变量没有被设置?

c - 如何调试 GNU flex 生成的程序?

c - C编程中字符串中的特定字母和数字

c++ - C/C++ 头文件和代码文件中的 ASCII 艺术注释

c - 当前导值为 8-f 时,为什么将 %x 值存储到变量会做一些奇怪的事情?

c - Gtk2 使用同一按钮从 2 个条目获取数据

c - 两个无符号整数相减得到结果绝对值

c - 仅删除用户输入,而不是整个屏幕