您的位置:首页 > 其它

进制转换

2016-07-28 15:29 274 查看
数组模拟时用char
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn=100000+10;

typedef struct node
{
char *data;
int top;
int bottom;
}Stack;

Stack S;

void initstack(Stack &S)
{
S.data=new char [maxn];
S.top=S.bottom=0;
}

void creat(Stack &S, int n, int m)///建立栈
{
int x;
while(n!=0)
{
x=n%m;
if(x<=9)
S.data[S.top]='0'+x;
else
S.data[S.top]='A'+x-10;
n/=m;
S.top++;
}
S.top--;
}

void display(Stack &S)
{
while(S.top>=S.bottom)
{
cout<<S.data[S.top];
S.top--;
}
cout<<endl;
}

int main()
{
ios::sync_with_stdio(false);
int n, m;
while(cin>>n>>m)
{
if(n==0)
cout<<"0"<<endl;
else if(n<0)
{
n=-n;
cout<<"-";
initstack(S);
creat(S, n, m);
display(S);
}
else
{
initstack(S);
creat(S, n, m);
display(S);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: