C/C++の宿題を片付けます 59代目
■ このスレッドは過去ログ倉庫に格納されています
0240デフォルトの名無しさん
2006/01/28(土) 23:45:36#include<assert.h> // assert を使うためのヘッダ
class stack
{
private:
static const int max = 100; int data[max]; int sp;
public:
void push(int); void pop(); bool empty() const;
int top() const; int size() const;
friend bool equal(const stack&,const stack&);
stack(); ~stack();
};
void stack::push(int d){ assert(sp<max); data[sp++] = d; }
void stack::pop(){ assert(0<sp); --sp; }
bool stack::empty() const{ return sp==0; }
int stack::top() const{return data[sp-1]; }
int stack::size() const{ return sp; }
bool equal(const stack& s1,const stack& s2){
bool eq = true;
if(s1.sp!=s2.sp) eq = false;
else{
for(int j=0; j<s1.sp && eq; j++)if(s1.data[j]!=s2.data[j]) eq = false;
return eq;
}
}
stack::stack(){sp = 0;}
stack::~stack(){}
このクラスを実行すると、スタックが同じ場合には1,スタックが異なる場合には56と表示されました。これで合っているのでしょうか?
■ このスレッドは過去ログ倉庫に格納されています