用栈实现十进制数字转化其他进制数字(2-8)

#include<iostream>
using namespace std;
const int max = 100;

class Stack
{
public:
	Stack()
	{
		top = -1;
		a = new int[max];
	}
	~Stack()
	{
		delete []a;
	}
	void clear()
	{
		top = -1;
	}
	bool push(int x)
	{
		if (top == max - 1)
		{
			cout << "栈满" << endl;
			return false;
		}
		else
		{
			a[++top] = x;
			return true;
		}
	}
	bool pop(int s)
	{
		if (top == -1)
		{
			cout << "栈为空" << endl;
			return false;
		}			
		else
		{
			s = a[top--];
			return true;
		}
	}
	bool istop()
	{
		if (top == -1)
		{
			cout << "栈为空" << endl;
			return false;
		}
		else
			return true;
	}
	bool isempty()
	{
		if (top == -1)
			return true;
		else
			return false;
	}
	bool isfull()
	{
		if (top == max - 1)
			return true;
		else
			return false;
	}
	void print()
	{
		if(top != -1)
			for (int i = top; i >= 0; i--)
				cout << a[i] << " ";
		cout << endl;
	}
private:
	int top;
	int *a;
};

void run(int x)
{
	Stack s;
	for (int i = 2; i < 10; i++)
	{
		
		int t1 = x, t2 = 0;
		while (t1 > 0)
		{
			t2 = t1 % i;
			t1 = t1 / i;
			s.push(t2);
		}	
		s.print();
		s.clear();
	}
}

int main()
{
	int x;
	cout << "输入十进制数 " << endl;
	cin >> x;
	cout << "从上到下依次为2-8进制数" << endl;
	run(x);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值