写在前面的话
没什么想说的
/**************************************
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;
}