#include <iostream>
using namespace std;
template<class T>
class LinearStack
{
public:
LinearStack(int LSMaxSize);
~LinearStack();
bool IsEmpty();
bool IsFull();
bool Push(const T& x);
bool Top(T& x);
bool Pop(T& x);
void OutPut(ostream& out)const;
private:
int top;
int MaxSize;
T* element;
};
template<class T>
LinearStack<T>::LinearStack(int LSMaxSize)
{
MaxSize = LSMaxSize;
element = new T[LSMaxSize];
top = -1;
}
template<class T>
LinearStack<T>::~LinearStack()
{
delete []element;
}
template<class T>
bool LinearStack<T>::IsEmpty()
{
return top == -1;
}
template<class T>
bool LinearStack<T>::IsFull()
{
return top + 1 == MaxSize;
}
template<class T>
bool LinearStack<T>::Push(const T& x)
{
if (IsFull())
return false;
else {
top++;
element[top] = x;
return true;
}
}
template<class T>
bool LinearStack<T>::Top(T& x)
{
if (IsEmpty)
return false;
else
{
x = element[top];
return true;
}
}
template<class T>
bool LinearStack<T>::Pop(T& x)
{
if (IsEmpty())
return false;
else
{
x = element[top];
top--;
return true;
}
}
template<class T>
void LinearStack<T>::OutPut(ostream& out) const
{
for (int i = 0; i <= top; i++)
out << element[i] << endl;
}
template<class T>
ostream& operator<<(ostream& out, const LinearStack<T>& x)
{
x.OutPut(out);
return out;
}
void conversion(int n, int base) {
int x, y;
y = n;
LinearStack<int> s(100);
while (y) {
s.Push(y % base);
y = y / base;
}
cout << "十进制数" << n << "转换为" << base << "进制为:\n";
while (!s.IsEmpty()) {
s.Pop(x);
cout << x;
}
}
int main() {
LinearStack<int> s(10);
int n, base;
cout << "请输入十进制数和要转换的进制基数:" << endl;
cin >> n >> base;
conversion(n, base);
cout << endl;
return 0;
}