给你这个
//十进制转换成任意进制——栈实现
#include
using namespace std;
#define max 20
typedef struct
{
char data[max];
int top;
}SeqStack;
SeqStack *s;
int InitStack(SeqStack *s)
{
s->top=-1;
// if ((s=(SeqStack*)sizeof(SeqStack)))==NULL)
// return 0;
// s->top=-1;
return 1;
}
int PushStack(SeqStack *s,char x)
{
if (s->top==max-1)
return 0;
s->data[++s->top]=x;
return 1;
}
void DecToN(int num,int n,SeqStack *s)
{
int t=num;
int t2=0;
char c;
while (t!=0)
{
t2=t%n;
if (t2>=10)
c=t2-10+'A';
else
c=t2+'0';
t/=n;
PushStack(s,c);
}
}
void OutPut(int num,int n,SeqStack *s)
{
cout << "十进制数 " << num << " 转换成" << n << " 进制后为:" ;
for (int i=s->top; i>=0; --i)
cout << s->data[i] ;
cout << endl;
}
int main()
{
int num;
int n;
cout << "输入要转换的十进制数(不超过整型的大小)和转换成的进制:\n";
cin >> num >> n;
SeqStack *s;
InitStack(s);
DecToN(num,n,s);
OutPut(num,n,s);
system("pause");
return 0;
}