进制的转换

#include <iostream>
using namespace std;

template <class T>
class node
{
public:
T data;
node<T>* link;

node(const T& el, node<T>* ptr = NULL){
data = el;
link = ptr;
}
};
template <class T>
class pstack
{
private:
node<T>* top;
int size;
public:
pstack(T s =0){
top = NULL;
size = 0;
}
~pstack(){
clear();
}
void clear()
{
while (top != NULL)
{
node<T>* temp = top;
top = top->link;
delete temp;
}
size = 0;
}
void push(const T item)
{
node<T>* tmp = new node<T>(item, top);
top = tmp;
size++;
}
T pop()
{
T temp;
if (size == 0){
cout << "栈为空,不能进行删除!" << endl;
exit(0);
}


else
{

temp = top->data;
node<T>* tmp = top;
top = top->link;
delete tmp;
size--;
}
return temp;
}
T Top()
{
T temp;
if (size == 0){
cout << "栈为空,不能进行删除!" << endl;
exit(0);
}
else
{

temp = top->data;
}
return temp;
}
bool isempty()
{
if (size == 0)
return true;
else
return false;
}
};

int main()
{
int a,b,i = 1,d;
pstack<int> c;
while (i != 2)
{
cout << "输入要转换的十进制数:";
cin >> a;
cout << "输入转换为的进制:";
cin >> b;
if (b >= 2 && b <= 9)
{
while (a)
{
d = a%b;
c.push(d);
a = a / b;

}
while (!c.isempty())
{
cout << c.pop();
}
cout << endl;
}
else
{
cout << "输入错误!!" << endl;
}
cout << "#######################\n";
cout << "#### 1、继续 ####\n";
cout << "#### 2、退出 ####\n";
cout << "#######################\n";
cout << "请选择。。。";
cin >> i;
system("cls");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值