呃呃,看看就好,不用多说......
代码实现
#include <iostream>
#define ElemType int
#define MAXSIZE 100
using namespace std;
struct likestack
{
int top;
ElemType date[MAXSIZE];
};
void initstack(likestack *S)
{
S->top=-1;
}
bool push(likestack *S,ElemType e)
{
if(S->top==MAXSIZE-1)
{
return false;
}
S->top++;
S->date[S->top]=e;
return true;
}
bool stackempty(likestack *S)
{
if(S->top==-1)
{
return true;
}
else
{
return false;
}
}
bool pop(likestack *S,ElemType *e)
{
if(S->top==-1)
{
return false;
}
*e=S->date[S->top];
S->top--;
return true;
}
int main()
{
int n,m,f;
char c='A';
likestack *S=new likestack;
initstack(S);
cin>>n>>m;
while(n!=0)
{
f=n%m;
push(S,f);
n=n/m;
}
while(stackempty(S)!=true)
{
pop(S,&f);
if(f>=10)
cout<<char(c+f-10);//这注意一下,还是有点巧妙的
else
cout<<f;
}
return 0;
}
运行结果


本文介绍了一个使用栈数据结构来实现十进制数到其他进制数转换的C++程序。该程序首先将十进制数不断除以目标进制取余数并压入栈中,再依次弹出栈中的元素得到最终的转换结果。巧妙地利用了栈的特性完成了进制转换的任务。
1928

被折叠的 条评论
为什么被折叠?



