www.hwczws.com:帮我修改下这个程序 谢谢了哈

来源:百度文库 编辑:高考问答 时间:2024/04/24 13:02:22
#include<iostream.h>
#include<stdlib.h>

template<class T, int size>
class STACK{
public:
STACK()
{
top=0;
}
~STACK()
{
// delete data;
}
T get_top()
{
if( is_empty()){
cout<<"The stack is EMPTY!"<<endl;
exit(1);
}
return data[top];
}
int push(T element)
{
top=top+1;
if(top>size){
cout<<"The stack is FULL!"<<endl;
exit(1);
}else data[top]=element;
return 1;
}
void pop()
{
if(is_empty())return;
else top=top-1;
}
int is_empty()
{
if(top=0)return 1;
else return 0;
}
private:
T data[size+1];
int top;
};

int main()
{
int a=5;
STACK<int, 8> int_stack;
//STACK<int ,a> error_stack;
int_stack.push(a); a=a+1;

int_stack.push(a); a=a+1;
int_stack.push(7);

cout<<int_stack.get_top(); int_stack.pop();
cout<<int_stack.get_top(); int_stack.pop();
cout<<int_stack.get_top(); int_stack.pop();
cout<<endl;
return 1;
}

/*大哥你的这个程序找错费了我20分钟,你不给我加点分太不厚道了,我以后不到编程这块来了,问题往这一放别人答了也不给加分,这叫什么啊!!
*/
#include<iostream.h>
#include<stdlib.h>

template<class T, int size>
class STACK{

public:
STACK()
{
top=0;
}
~STACK()
{
// delete data;
}
T get_top()
{
if( is_empty()){
cout<<"The stack is EMPTY!"<<endl;
exit(1);
}

return data[top];
}
int push(T element)
{
top=top+1;
if(top>size){
cout<<"The stack is FULL!"<<endl;
exit(1);
}else data[top]=element;
return 1;
}
void pop()
{
if(is_empty())return;
else top=top-1;
}
int is_empty()
{
if(top==0)return 1; //害人啊 == 不是 = 赋值啊??
else return 0;
}
private:
T data[size+1];
int top;

};

int main()
{
int a=5;
STACK<int, 8> int_stack;
//STACK<int ,a> error_stack;
int_stack.push(a); a=a+1;

int_stack.push(a); a=a+1;
int_stack.push(7);

cout<<int_stack.get_top(); int_stack.pop();
cout<<int_stack.get_top(); int_stack.pop();
cout<<int_stack.get_top(); int_stack.pop();
cout<<endl;
return 1;
}