/***链栈实现数制的转换***/
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct SNode{
int data;
struct SNode *next;
}SNode,*LinkStack;
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct SNode{
int data;
struct SNode *next;
}SNode,*LinkStack;
Status InitStack(LinkStack &S)
{
S = NULL;
return OK;
}
bool StackEmpty(LinkStack S)
{
if(!S)
return true;
return false;
}
Status Push(LinkStack &S,int e)
{
SNode *p = new SNode;
if(!p)
{
return OVERFLOW;
}
p->data = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack &S,int &e)
{
SNode *p;
if(!S)
return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
{
S = NULL;
return OK;
}
bool StackEmpty(LinkStack S)
{
if(!S)
return true;
return false;
}
Status Push(LinkStack &S,int e)
{
SNode *p = new SNode;
if(!p)
{
return OVERFLOW;
}
p->data = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack &S,int &e)
{
SNode *p;
if(!S)
return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
//算法3.8 数制的转换(链栈实现)
/*void conversion (int n) {
//对于输入的任意一个非负十进制数,打印输出与其等值的八进制数
//请填写完整
LinkStack S;
InitStack(S);
cout<<"输入要转化的数字:";
int N,e;
cin>>N;
while(N)
{
Push(S,N%n);
N=N/n;
}
while(!StackEmpty(S))
{
Pop(S,e);
cout<<e;
}
cout<<endl;
}*/
void conversion(int n,int N){
if(n==0) return;
else
{
conversion(n/N,N);
cout<<n%N;
}
}
int main()
{ cout<<"输入要转化的数:";
int n,N;
cin>>n;
cout<<"要转化为几进制:";
cin>>N;
cout<<"结果为:";
conversion(n,N);
cout<<endl;
return 0;
}
/*void conversion (int n) {
//对于输入的任意一个非负十进制数,打印输出与其等值的八进制数
//请填写完整
LinkStack S;
InitStack(S);
cout<<"输入要转化的数字:";
int N,e;
cin>>N;
while(N)
{
Push(S,N%n);
N=N/n;
}
while(!StackEmpty(S))
{
Pop(S,e);
cout<<e;
}
cout<<endl;
}*/
void conversion(int n,int N){
if(n==0) return;
else
{
conversion(n/N,N);
cout<<n%N;
}
}
int main()
{ cout<<"输入要转化的数:";
int n,N;
cin>>n;
cout<<"要转化为几进制:";
cin>>N;
cout<<"结果为:";
conversion(n,N);
cout<<endl;
return 0;
}