十进制的数转换成其他进制的数
#include
#include
#define OK 1
#define ERROR -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
using namespace std;
typedef struct
{
int *base;
int *top;
int stracksize;
}SqStack;
int InitStack(SqStack &S)
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)
exit(ERROR);
S.top=S.base;
S.stracksize=STACK_INIT_SIZE;
return OK;
}
int Push(SqStack &S,int e)
{
*S.top++=e;
return OK;
}
int Pop(SqStack &S,int &e)
{
if(S.base!=S.top)
{
e=*--S.top;
return OK;
}
else
return ERROR;
}
bool StackEmpty(SqStack S)
{
if(S.top==S.base)
return true;
else
return false;
}
void conversion(int N,int d)
{
SqStack S;int e;
InitStack(S);
while(N)
{
Push(S,N%d);
N=N/d;
}
cout<<"转换成"<<d<<"进制为:";
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
}
int main()
{
int n,d;
cout<<"请输入想要转换的数: ";cin>>n;
cout<<"请输入转换的进制: ";cin>>d;
conversion(n,d);
cout<<endl;
return 0;
}