利用栈先进后出的原理帮助实现将十进制数转换为二至九进制数的功能。
源代码:
#include <iostream>
using namespace std;
const int maxsize=100;
class Sstack
{
public:
Sstack(){top=-1;}
~Sstack(){};
void push(int x);
int getpop(){if(top!=-1)return data[top];};
int pop();
int empty();
void decimaltor(int n,int m);
private:
int data[maxsize];
int top;
};
void Sstack::push(int x)
{
if(top==maxsize)throw"上溢";
else{
data[++top]=x;
}
}
int Sstack::pop()
{
if(top==-1)throw"下溢";
else{
int x;
x=data[top--];
return x;
}
}
int Sstack::empty()
{
if(top==-1){return 1;}
else{return 0;}
}
void Sstack::decimaltor(int n,int m)
{
top=-1;
int t;
while (n!=0)
{