栈的应用之进制转化

顺序栈的代码:

不再赘述:点击打开链接

							//栈的应用-----进制转化
#include"stack.h"
int main()
{
	Stack st;
	InitStack(&st);
	int select;
	int num1;     //要转化的数
	int num2;	  //转化之后各个位的数
	int flag = 1; //控制循环结束
	while(flag)
	{
		cout<<"****************进制转换*****************"<<endl;
		cout<<"*[1]十转二                     [2]十转八*"<<endl;
		cout<<"*[3]十转十六                   [4]退出  *"<<endl;
		cout<<"please choose the function num you:"<<endl;
		cin>>select;
		switch(select)
		{
		case 1: //2进制转化
			cout<<"input the num you want to transform:"<<endl;
			cin>>num1;
			while(num1)
			{
				Push(&st,num1%2);
				num1 /= 2;
			}
			cout<<"the result is :"<<endl;
			while(!IsEmpty(&st))
			{
				Pop(&st,&num2);
				cout<<num2;		
			}
			cout<<endl;
			break;
		case 2: //8进制转化
			cout<<"input the num you want to transform:"<<endl;
			cin>>num1;
			while(num1)
			{
				Push(&st,num1%8);
				num1 /= 8;
			}
			cout<<"the result is :"<<endl;
			while(!IsEmpty(&st))
			{
				Pop(&st,&num2);
				cout<<num2;		
			}
			cout<<endl;
			break;
		case 3: //16进制转化
			cout<<"input the num you want to transform:"<<endl;
			cin>>num1;
			while(num1)
			{
				Push(&st,num1%16);
				num1 /= 16;
			}
			cout<<"the result is :"<<endl;
			while(!IsEmpty(&st))
			{
				Pop(&st,&num2);
				if(num2 > 9)//A---F中的数
				{
					cout<<(char)(num2 - 10 + 'A');	//强制类型转化
				}
				else
					cout<<num2;//0----9中的数
			}
			cout<<endl;
			break;
		case 4:
			select = 0;
			break;
		default:
			break;
		}
	}
	destory(&st);
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值