首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

帮忙看下是什么原因,多谢了!一直存在有关问题~

2012-03-09 
帮忙看下是什么原因,谢谢了!一直存在问题~~//是关于堆栈的一个小类,有POP PUSH EMPTY 和TOP 四个函数.但不

帮忙看下是什么原因,谢谢了!一直存在问题~~
//是关于堆栈的一个小类,有POP PUSH EMPTY 和TOP 四个函数.但不知怎么的就一直有错!

#ifndef STACK_H
#define STACK_H

const maxstack = 10;

typedef int Stack_entry;

enum Error_code 
{
success, overflow,underflow
};

class Stack
{
public:
Stack();
bool empty() const;
Error_code pop();
Error_code top(Stack_entry &item) const;
Error_code push(const Stack_entry &item);
private:
int count;
Stack_entry entry[maxstack];

};
#endif
//以上是头文件!
//下面是CPP文件
#include<iostream>
#include"stack.h"
using namespace std;

Error_code Stack::push(const Stack_entry &item)
{
Error_code outcome = success;
if(count >= maxstack)
outcome=overflow;
else
entry[count++] = item;
return outcome;
}
///////////////////////////////////////////////
Error_code Stack::pop()
{
Error_code outcome = success;
if(count == 0)
outcome = underflow;
else --count;
return outcome;
}
//////////////////////////////////////////////

Error_code Stack::top(Stack_entry &item) const
{
Error_code outcome = success;
if(count == 0)
outcome = underflow;
else
{
item = entry[count-1];
//cout<<"The max number you type is "<<item<<" ";

return outcome;
}
//////////////////////////////////////////////
bool Stack::empty()const
{
bool outcome = true;
if(count > 0)
outcome = false;
return outcome;
}

//////////////////////////////////////////////
Stack::Stack()
{
count = 0;
}

[解决办法]

Error_code Stack::push(const Stack_entry &item) 

Error_code outcome = success; 
if(count >= maxstack-1) 
outcome=overflow; 
else 
entry[count++] = item; 
return outcome; 


[解决办法]
////////////////////////////////////////////// 
Stack::Stack() 

count = 0; 
}

Error_code Stack::push(const Stack_entry &item) 

Error_code outcome = success; 
if(count >= maxstack) 
outcome=overflow; 
else 
entry[count++] = item; 
return outcome; 

/////////////////////////////////////////////// 
Error_code Stack::pop() 

Error_code outcome = success; 
if(count == 0) 
outcome = underflow; 
else --count; 
return outcome; 

////////////////////////////////////////////// 

Error_code Stack::top(Stack_entry &item) const 

Error_code outcome = success; 
if(count == 0) 
outcome = underflow; 
else 

item = entry[count-1]; 
// cout < <"The max number you type is " < <item < <" "; 
}
return outcome; 

////////////////////////////////////////////// 
bool Stack::empty() const 

bool outcome = true; 
if(count > 0) 
outcome = false; 
return outcome; 
}

热点排行