任意进制转换.
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include<iostream>
using namespace std;
#define stacksize 100
typedef int DataType;
typedef struct
{
DataType data[stacksize];
int top;
}Stack;
void Initial(Stack *s)
{
s->top=-1;
}
int Isempty(Stack *s)
{
return s->top==-1;
}
int IsFull(Stack *s)
{
return s->top==stacksize-1;
}
void Push(Stack *s,DataType x)
{
if(IsFull(s))
{
cout<<"栈上溢";
exit(1);
}
s->data[++s->top]=x;
}
DataType Pop(Stack *s)
{
if(Isempty(s))
{
cout<<"栈为空";
exit(1);
}
return s->data[s->top--];
}
//进制转换函数
void Conversion(int i,int j)
{
int k;
Stack s;
Initial(&s);
while(i)
{
Push(&s,i%j);
i=i/j;
}
while(!Isempty(&s))
{
k=Pop(&s);
cout<<k;
}
cout<<endl;
}
DataType top(Stack *s)
{
if(Isempty(s))
{
cout<<"栈为空";
exit(1);
}
return s->data[s->top];
}
int main(void)
{
int m,n;
cin>>m>>n;
Conversion(m,n);
system("pause");
return 0;
}
[[it] 本帖最后由 zjl138 于 2008-6-26 15:39 编辑 [/it]]