这里就是省略了判空操作
代码如下:
#include<iostream>
using namespace std;
const int Stacksize=10;
template<class Datatype>
class Seqstack
{
public:
Seqstack() //构造函数 初始化
{
top=-1;
}
~Seqstack(){} //析构函数
void Push(Datatype x) //入栈操作
{
if(top==Stacksize-1)throw"上溢";
top++;
data[top]=x;
}
void pop() //出栈操作
{
Datatype x;
if(top==-1) throw"下溢";
while(top!=-1)
{
cout<<data[top--];
}
}
void conversion(int a[]) //进制转换
{
int num,length = 0;
cout<<"请输入十进制数:"<<endl;
cin >> num;
while (num)
{
a[length] = num % 2;
length++;
num = num / 2;
}
}
private:
int data[Stacksize];
int top;
};
int main()
{
int a[8]={0};
Seqstack<int>S;
S.conversion(a);
for(int i=0;i<8;i++)
{
S.Push(a[i]);
}
cout<<"转换为二进制为:"<<endl;
S.pop();
}
运行结果: