顺序栈的代码:
不再赘述:点击打开链接
//栈的应用-----进制转化 #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