#include <iostream>
using namespace std;
typedef int ElemType;
class SeqStack
{
public:
SeqStack(int size);
~SeqStack(){ delete[] elements; }
void PushStack(ElemType x);
ElemType PopStack(ElemType x);
void ClearStack(){top = -1;}
bool IsFullStack(){ return top == maxsize-1; }
bool IsEmptyStack();
void PrintStack();
private:
unsigned height;
int top;
int maxsize;
ElemType *elements;
};
SeqStack::SeqStack(int size)
{
height = 0;
top = -1;
maxsize = size;
elements = new ElemType[size];
}
void SeqStack::PushStack(ElemType x)
{
if(IsFullStack())
{
cout<<"栈已满";
}
else
{
elements[++top] = x;
height++;
}
}
ElemType SeqStack::PopStack(ElemType x)
{
x = elements[top];
top--;
height--;
return x;
}
bool SeqStack::IsEmptyStack()
{
return (height == 0)? true:false;
}
void SeqStack::PrintStack()
{
while(IsEmptyStack() == false)
{
cout<<elements[top]<<" ";
top--;
height--;
}
cout<<endl;
}
int main(void)
{
int n;
ElemType p;
cout<<"请输入学生人数:";
cin>>n;
SeqStack h(n),m(n),l(n);
cout<<"学生的成绩为:";
for(int i=0;i<n;i++)
{
cin>>p;
cout<<p<<" ";
if(p>100)
{ h.PushStack(p); }
else if(p >= 60 && p <= 100)
{ m.PushStack(p); }
else
{ l.PushStack(p); }
}
cout<<endl;
cout<<"小于60的成绩:";
l.PrintStack();
cout<<"小于等于100且大于等于60的成绩:";
m.PrintStack();
cout<<"大于100的成绩:";
h.PrintStack();
system("PAUSE");
return 0;
}
栈
最新推荐文章于 2014-10-06 09:47:28 发布