《四海小记c++学习之路》栈/r值化

写在前面的话

没什么想说的

/**************************************
Copyright       陈四海
Author:         created by 陈四海
Date:		    2020-12-20
Description:    r值化
Version:        1.0
**************************************/
#include<iostream>
#define Stack_Size 50
using namespace std;
class List
{
public:
	List();
	bool isEmpty();
	bool push(const int& n);
	bool pop( int& n);
	bool getTop(int& n);
	bool change(const int& r,int& n);
private:
	int elem[Stack_Size];
	int size;
};
//构造函数
List::List()
{
	size = -1;
}

/**************************************
Fuction:         isEmpty
Description:     判断栈内是否为空
Input:			 
Output:
Return:			 bool
Others:
**************************************/
bool List::isEmpty()
{
	return size == -1 ? false : true;
}
/**************************************
Fuction:         push
Description:     进栈
Input:			 const int& n
Output:
Return:			 bool
Others:
**************************************/
bool List::push(const int& n)
{
	if (size == Stack_Size - 1) return false;
	size++;
	elem[size] = n;
	return true;
}
/**************************************
Fuction:         pop
Description:     出栈
Input:			 int& n
Output:
Return:			 bool
Others:
**************************************/
bool List::pop( int& n)
{
	if (size == -1) return false;
	n = elem[size];
	size--;
	return true;
}
/**************************************
Fuction:         getTop
Description:     读栈顶元素
Input:			 int& n
Output:
Return:			 bool
Others:
**************************************/
bool List::getTop(int& n)
{
	if (size == -1) return false;
	n = elem[size];
	return true;
}
/**************************************
Fuction:         change
Description:     r值化
Input:			 const int& r,int& n
Output:
Return:			 bool
Others:
**************************************/
bool List::change(const int& r,int& n)
{
	int a;
	if (r < 0 && n < 0)
	{
		cout << "请勿输入负数!" << endl;
		return false;
	}
	while (n)
	{
		a = n % r;
		push(a);
		n = n / r;
	}
	while (isEmpty())
	{
		pop(a);
		cout << a;
	}
	cout << "\n" << r << "进制输出完毕!" << endl;
	return true;
}
int main()
{
	List list;
	List* plist = &list;
	int r, n;
	cout << "请输入r值化!r=";cin >> r;
	cout << "请输入数值!n=";cin >> n;
	plist->change(r, n);
	system("pause");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值