微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

类 c++ 堆栈无法识别的类方法

如何解决类 c++ 堆栈无法识别的类方法

这个类是一个堆栈,我试图让用户调用与堆栈相关的函数。但是,似乎没有进行任何更改,因为在调用 display 函数时,我的整个堆栈都填充了零。这是在将值压入堆栈之后。

#include <iostream>
using namespace std;

class Stack   {

    public:
        int array[5];
        int top;


        Stack() {
            top = -1;
            for (int i = 0; i < 5; ++i) {
                array[i] = 0;
            }
        }   
 
        bool isEmpty()     {
            if (top == -1)  {
                return true;
            }
            return false;    }

        bool isFull()   {
            if (top == 4)   {
                return true;
            }
            return false;
        }

        void push(int val)  {
            if (isFull())   {
                cout <<"stack overflow" << endl;
            }
            else  {
                top++;
                array[top] == val;
            }    
        }       

        int pop()   {
            if (isEmpty())  {
                cout << "stack underflow" << endl;
            }
            else    {
                int val = array[top];
                array[top] = 0;
                top--;
                return val;
            }
        }

        int count() {
            return(top + 1);
        }

        int peek(int pos)  {
            if (isEmpty())    {
                cout << "stack underflow";
                    return 0;
            }
            else  {
                return array[pos];
            }
        }

        void change(int pos,int val) {
            array[pos] = val;
        }

        void display()  {
            for (int i = 4; i >= 0; --i) {
                cout << array[i];
            }
        }   

};
        

int main()  {
    Stack stack;
    int option,position,value;
    do
    {

        cout << "What operation do you want to perform? Select Option number. Enter 0 to exit." << endl;
        cout << "1. Push" << endl;
        cout << "2. Pop" << endl;
        cout << "3. isEmpty()"<< endl;
        cout << "4. isFull()"<< endl;
        cout << "5. peek()"<< endl;
        cout << "6. count()"<< endl;
        cout << "7. change()"<< endl;
        cout << "8. display()"<< endl;
        cout << "9. Clear Screen"<< endl<< endl;

        cin >> option;
        switch(option)  {
            case 1:
                cout << "Enter item to push onto stack: " << endl;
                cin>>value;
                stack.push(value);
                break;
            case 2:
                cout << "Popping from stack: " << stack.pop() << endl;
                break;
            case 3:
                if (stack.isEmpty())    {
                    cout << "True" << endl;
                }
                else {
                    cout << "False" << endl;
                }
                break;
            case 4:
                if (stack.isFull()) {
                    cout << "True" << endl;
                }
                else    {
                    cout << "False" << endl;
                }
                break;
            case 5:
                cout << "Enter position to peek" << endl;
                cin >> position;
                cout << stack.peek(position) << endl;
                break;
            case 6:
                cout << stack.count() << endl;
                break;
            case 7:
                cout << "Enter position followed by value: " << endl;
                cin >> position >> value;
                cout << "Position changed" << endl;
                break;
            case 8:
                stack.display();
                break;
            case 9:
                system("cls");
                break;
            }
        } 
            while (option != 0);
            return 0;
    } 

例如,用户会按 1 调用 push() 并推送一些输入值。然后,他们输入 8 来调用 display ,它应该在堆栈中显示该输入值,但打印 00000

解决方法

您实际上从未将任何东西放入堆栈中:

        void push(int val)  {
            if (isFull())   {
                cout <<"stack overflow" << endl;
            }
            else  {
                top++;
                array[top] == val;      // Change == to =
            }    
        } 

目前,您正在进行比较。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。