MAXXXXXXX 2016-10-20 12:51 采纳率: 0%
浏览 1140

求助,C++ primer plus第六版第14章 程序清单14.16结果出错

照着书上敲的,运行结果应该是in数组元素的一个出栈序列,类似
Please enter stack size:5
2:Kiki Ishtar
1:Hank Gilgamesh
3:Betty Rocker
5:Wolfgang Kibble
4:Ian Flagranti
7:Joy Almondo
9:Juan Moore
8:Xaverie Paprika
6:Portia Koop
10:Misha Mache
这样

但是编译后运行,除非栈只有一个元素,无论输入多大的栈尺寸,输出都会出错:
Please enter stack size: 5
Kiki Ishtar
Kiki Ishtar
ki Ishtar
i Ishtar
iki Ishtar
Ishtar
: Kiki Ishtar
Ishtar
2: Kiki Ishtar
tar
Bye

是内存出错了吗?为什么会出现这种结果?求大神指点。

头文件

 //stcktp1.h
#ifndef STCKPT1_H_
#define STCKPT1_H_

template <typename Type>
class Stack
{
private:
    enum {SIZE=10}; //default size
    int stacksize;
    Type *items;    //holds stack items
    int top;        //index for top stack item
public:
    explicit Stack(int ss=SIZE);
    Stack(const Stack &st);
    ~Stack() {delete [] items;}
    bool isempty() {return top==0;}
    bool isfull() {return top==stacksize;}
    bool push(const Type &item);    //add item to stack
    bool pop(Type &item);           //pop top into item
    Stack &operator=(const Stack &st);
};

template <typename Type>
Stack<Type>::Stack(int ss):stacksize(ss),top(0)
{
    items=new Type[stacksize];
}

template <typename Type>
Stack<Type>::Stack(const Stack &st)
{
    stacksize=st.stacksize;
    top=st.top;
    items=new Type[stacksize];
    for(int i=0;i<top;i++)
        items[i]=st.items[i];
}

template <typename Type>
bool Stack<Type>::push(const Type &item)
{
    if(top<stacksize)
    {
        items[top++]=item;
        return true;
    }
    else
        return false;
}

template <typename Type>
bool Stack<Type>::pop(Type &item)
{
    if(top>0)
    {
        item=items[--top];
        return true;
    }
    else
        return false;
}

template <typename Type>
Stack<Type> &Stack<Type>::operator=(const Stack<Type> &st)
{
    if(this==&st)
        return *this;
    delete [] items;
    stacksize=st.stacksize;
    top=st.top;
    items=new Type[stacksize];
    for(int i=0;i<top;i++)
        items[i]=st.items[i];
    return *this;
} 

#endif

测试程序

 //stkoptr1.cpp
#include <iostream>
#include <cstdlib>  //for rand(),srand()
#include <ctime>    //for time()
#include "stcktp1.h"

const int Num=10;

int main()
{
    std::srand(std::time(0));   //randomize rand()
    std::cout<<"Please enter stack size: ";
    int stacksize;
    std::cin>>stacksize;
//create an empty stack with stacksize slots
    Stack<const char *> st(stacksize);

//in basket
    const char *in[Num]={
            "1: Hank Gilgamesh","2: Kiki Ishtar",
            "3: Betty Rocker","4: Ian Flagranti",
            "5: Wolfgang Kibble","6: Portia Koop",
            "7: Joy Almondo","8: Xaverie Paprika",
            "9: Juan Moore","10: Misha Mache"
            };
//out basket
    const char *out[Num];

    int processed=0;
    int nextin=0;
    while(processed<Num)
    {
        if(st.isempty())
            st.push(in[nextin++]);
        else if(st.isfull())
            st.pop(out[processed++]);
        else if(std::rand()%2 && nextin<Num)    //50-50 chance
            st.push(in[nextin]++);
        else
            st.pop(out[processed++]);
    }
    for(int i=0;i<Num;i++)
        std::cout<<out[i]<<std::endl;
    std::cout<<"Bye\n";
    return 0;
}
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2016-10-20 16:07
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键