面向对象编程-C ++练习

时间:2019-03-11 15:34:19

标签: c++ oop stack

#include <iostream> 
#include <string> 
#include <cstdlib> 
#define MAX_SIZE 100
int pos=-1;
using namespace std; 

class StackElement { 
private: 
    int data; 
    string name; 
public: 
    StackElement() { 
        data=0;
        name="brak";
        } 
    StackElement(int d, string n) { 
        data = d;
        name = n;
        } 
    void getData() { 
        cin>>data;
        } 
    void getName() {
        cin>>name;
        } 
};
class Stack {
private: 
    int stackSize; 
    StackElement element[]; 
    public:
    Stack() {
        stackSize = 0;
        cout<<"Stack is empty now."<<endl;
        }
    Stack(int s) {
        stackSize = s;
        }
    void push() {
        element[pos+1].getData();
        cout<<"Enter element name:"<<endl;
        element[pos+1].getName();
        pos++;
        }
    void pop() {
        pos--;
        }
    bool isEmpty() {
        if(stackSize==0)
             return true;
        }
    bool isFull() {
        if(pos==MAX_SIZE)
             return true;
        }
 }; 
int main() {
    Stack stack(MAX_SIZE);
    int choice;
    cout<<"1. Push in stack.\n2. Pop from stack.\n3. Display stack.\n4. Exit."<<endl;
    if(stack.isEmpty())
        cout<<endl<<"Stack is empty."<<endl<<endl;
    do { 
        cout<<"Enter your choice"<<endl;
        cin>>choice;
        switch(choice) {
            case 1:
                 cout<<"Enter value:"<<endl;
                 stack.push();
                 break;
            case 2:
                 stack.pop();
                 break;
            case 3:
                cout<<"Soon-to-be displayer"<<endl;
                 break;
            case 4:
                cout<<"Exit"<<endl;
                break;
            default:
                cout<<"Invalid choice"<<endl;
        }
        if(stack.isFull()) {
            cout<<"Stack is full!"<<endl;
            break;
        }
    } while (choice != 4);
return 0; 
}

练习如下: 创建类Stack和StackElement。 堆栈元素必须包含:一个选择的2个私有池,一个/多个getter和两个构造函数-默认一个和参数化的一个(带有2个参数)。 堆栈必须包含:StackElement类型的元素,私有池 size ,一个默认值和一个参数化(一个参数)构造函数,方法 push,pop,isEmpty,isFull

我的问题是,当我运行程序时,在尝试将元素放入堆栈后它便崩溃了。

2 个答案:

答案 0 :(得分:2)

push函数以

开头
element[pos+1].getData();

pos是一个全局变量,从-1开始,因此从0开始。 您需要先查看堆栈的大小,然后再尝试从不存在的元素中获取数据。

StackElement element[];需要调整大小或这样声明:

StackElement element[MAX_SIZE]; 

答案 1 :(得分:0)

Okey,我知道了。如果有人需要看类似的东西,请看下面的代码:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class StackElement {
private:
    int data;
    string name;
public:
    StackElement() {
        data=0;
        name="-";
        }
    StackElement(int d, string n) {
        data = d;
        name = n;
        }
    void getElement() {
        cin>>data;
        cout<<"Enter name: "<<endl;
        cin>>name;
        }
};
class Stack {
private:
    int stackSize;
    int pos;
    StackElement *element;
public:
    Stack() {
        stackSize = 10;
        element = new StackElement[stackSize];
        }
    Stack(int s) {
        stackSize = s;
        element = new StackElement[stackSize];
        }
    void push() {
        element[pos++].getElement();
        ++stackSize;
        }
    StackElement pop() {
        return element[--pos];
        }
    bool isEmpty() {
        return pos == 0;
        }
    bool isFull() {
        return stackSize == pos;
        }
    void position() {
        pos=0;
    }
};

int main() {
    Stack stack(10);
    stack.position();
    int choice;
    cout<<"1. Push in stack.\n2. Pop from stack.\n3. Exit."<<endl;
    if(stack.isEmpty())
        cout<<endl<<"Stack is empty."<<endl<<endl;
    do {
        cout<<"Enter your choice"<<endl;
        cin>>choice;
        switch(choice) {
            case 1:
                cout<<"Enter value:"<<endl;
                stack.push();
                break;
            case 2:
                if(!stack.isEmpty())
                    stack.pop();
                else
                    cout<<"Stack is empty already."<<endl;
                break;
            case 3:
                cout<<"Exit"<<endl;
                break;
            default:
                cout<<"Invalid choice"<<endl;
        }
        if(stack.isFull()) {
            cout<<"Stack is full!"<<endl;
            break;
        }
    } while (choice != 4);
return 0;
}

PS。我知道它缺乏释放内存和析构函数的功能,我可能会在不久的将来对其进行处理:)