十进制数转换成R进制数,R属于(1,36)

15 篇文章 0 订阅
11 篇文章 0 订阅
/***************************************************************************************
* File Name   : jinzhizhuanhua.cpp                                                     *
* Created by (Author, Date )  :  Renzhibo  2012/3/21                                   *
*																					   *
* Description : 十进制转换成R进制;													   *
*				1<R<36																   *
*				注意 考虑 负数														   *
*Modified Details (Modified Date/Modifier/ Modified Reason):						   *
* 1:											                                       *
* 2:																				   *
***************************************************************************************/

#include <fstream>
#include <iostream>
#include <stack>  // STL中的栈 类

using namespace std;

const int R_SYSTEM = 5;
const char ALL_DIGIT[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
						'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
						'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
						'U', 'V', 'W', 'X', 'Y', 'Z'};
stack<char> g_cStack; // 栈 容器

int ChangdecimalsystemtoRsystem(int iDecimal_number, int iRsystem);
int GetIndex(char cChar);
char GetChar(int iIndex);
void PrintStack();

int main()
{
	char cContinue = 'y';
	while ('y' == cContinue || 'Y' == cContinue)
	{
		//接收 输入的 十进制 数
		cout << "Please input a Decimal Number:";
		int iInDecimalNumber = 0;
		cin >> iInDecimalNumber;
		// 同步 输入缓冲区
		cin.sync();
		//接收 输入的 R进制
		cout << "Please input a system:";
		int iInRsystem = 0;
		cin >> iInRsystem;
		// 回显 用户 输入的 十进制数
		cout << "You input the number is:" << iInDecimalNumber << endl << "(if the number is not you input, maybe you didn't input a Decimal  number)" << endl;

		if ( 0 != ChangdecimalsystemtoRsystem(iInDecimalNumber, iInRsystem))
		{
			cout << endl << "The " << iInRsystem << " system is wrong" << endl;
		}
		else
		{
			cout << endl << "The number( " << iInDecimalNumber << " ), " << iInRsystem << "'s system" << " result is :" << endl; 
			PrintStack(); // 输出
		}
		//同步 输入 流
		cin.sync();
		cout << "Continue(Y/y),other keys quit!" << endl;
		cContinue = getchar();
	}

	return 0;
}

/******************************************************************************
* Function Name : ChangdecimalsystemtoRsystem								  *
* Description   : 转换 十进制 到 Rsystem 进制	                              *
* Date          : 2012/3/21												      *
* Parameter     : iDecimal_number: 十进制数								  *
*				  iRsystem : 表示1~36 之间 的 进制							  * 
* Return Code   : -1 表示 出错	                                              *
* Author        : Renzhibo                                                    *
******************************************************************************/
int ChangdecimalsystemtoRsystem(int iDecimal_number, int iRsystem)
{
	int iReturnvalue = 0;
	if ( iRsystem > 1 && iRsystem < 36) // 判断 iRsystem 进制 是否 在 1 ~ 36 之间
	{
		bool bIs_positive_number = true; // 标示 是否 是整数
		if (iDecimal_number < 0)
		{	
			iDecimal_number = -iDecimal_number; // 将 负数 改成 正数
			bIs_positive_number = false;
		}

		int iTemp;
		char iTempChar;
		while (0 != iDecimal_number)
		{	
			//cout << iDecimal_number % iRsystem << endl;
			iTemp = iDecimal_number % iRsystem;
			iDecimal_number /= iRsystem;
			// 将指定 字符 入栈
			iTempChar = GetChar(iTemp);
			g_cStack.push(iTempChar);
		}
		// 如果是 负数 将 - 压入栈
		if (!bIs_positive_number)
		{
			g_cStack.push('-');
		}
	}
	else
	{
		iReturnvalue = -1;
	}
	
	return iReturnvalue;
}

/******************************************************************************
* Function Name : GetIndex													  *
* Description   : 获得 指定 值 在常量数组中的下标值                           *
* Date          : 2012/3/21												      *
* Parameter     : cInchar													  *
* Return Code   : -1 false;	                                              *
* Author        : Renzhibo                                                    *
******************************************************************************/
int GetIndex(char cInchar)
{
	int iTemp = -1;
	if (cInchar > '0' && cInchar < '9')
	{
		iTemp =	cInchar - 48;
	}
	else if (cInchar >= 'A' && cInchar <= 'Z')
	{
		iTemp =	cInchar - 55;	// 将 字母的 asci码 转换成 在数组中的下标 A 是 10
	}					
	else
	{
		iTemp = -1;
	}
	return iTemp;
}

/******************************************************************************
* Function Name : GetChar													  *
* Description   : 获得 指定 常量数组下标 的值                                 *
* Date          : 2012/3/21												      *
* Parameter     : iIndex													  *
* Return Code   : -1 false;	                                              *
* Author        : Renzhibo                                                    *
******************************************************************************/
char GetChar(int iIndex)
{
	int iTemp = -1;
	if (iIndex >= 0 && iIndex <= 9)    // 又一次 忘记 了 等号, 记得要 边界值 注意啊。
	{
		iTemp =	iIndex + 48;
	}
	else if (iIndex >= 10 && iIndex <= 35)
	{
		iTemp =	iIndex + 55;	
	}				
	else
	{
		iTemp = -1;
	}
	return iTemp;
}

/******************************************************************************
* Function Name : PrintStack												  *
* Description   : 输出 栈 中的 值			                                  *
* Date          : 2012/3/21												      *
* Parameter     : 无														  *
* Return Code   : 无													      *
* Author        : Renzhibo                                                    *
******************************************************************************/
void PrintStack()
{
	while (!g_cStack.empty())
	{
		cout << g_cStack.top() << " ";
		g_cStack.pop();
	}
	cout << endl;
	return;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值