要求:十进制转八进制
//其它进制的话,只需做下稍微的修改
#include <iostream>
using namespace std;
typedef struct StackNode
{
int data;
struct StackNode *next;
}StackNode,*LinkStack;
int InitStack(LinkStack &S)
{
S=NULL;
return 0;
}
int Push(LinkStack &S,int e)
{
StackNode *p;
p=new StackNode;
p->data=e;
p->next=S;
S=p;
return 0;
}
int Pop(LinkStack &S,int &e)
{
StackNode *p;
if(S==NULL)cout<<"栈为空!"<<endl;
e=S->data;
p=S;
S=S->next;
delete p;
return 0;
}
void conversion(LinkStack &S,int N)
{
int e;
InitStack(S);
while (N)
{
Push(S,N%8);
N=N/8;
}
while(S!=NULL)
{
Pop(S,e);
cout<<e;
}
}
int main ()
{
LinkStack S;int num;
cout<<"欢迎使用进制转换(10进制转8进制):"<<endl;
cout<<"请输入任意十进制数字:"<<endl;
cin>>num;
cout<<"转换后的8进制数为:"<<endl;
conversion(S,num);
cout<<endl;
return 0;
}