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

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

说明:

我有两个堆栈。堆叠和张贴。堆栈是我存储运算符的位置,邮政是我存储后缀的位置。

在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 - 警告:传递'pushPost'的参数1使指针从整数转换为后缀progam时不进行强制转换[-Wint-conversion],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54836391/

10-10 14:52