首页 > 其他 > 详细

栈的应用之进制转化

时间:2015-05-09 16:30:32      阅读:126      评论:0      收藏:0      [点我收藏+]

顺序栈的代码:

不再赘述:点击打开链接

							//栈的应用-----进制转化
#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;
}
技术分享



栈的应用之进制转化

原文:http://blog.csdn.net/zongyinhu/article/details/45602109

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!