#include<iostream>
using namespace std;
#define InitSize 20
typedef int QElemType;
typedef struct {
QElemType *data;
int top,maxsize;
}LinkQueue;
void QueueInit(LinkQueue&Q)
{
Q.data = new QElemType(InitSize);
Q.top = -1;
Q.maxsize = InitSize;
}
void Push(LinkQueue&Q,QElemType val)
{
if (Q.top == Q.maxsize-1)return;
Q.data[++Q.top] = val;
}
void Pop(LinkQueue&Q, QElemType&x)
{
if (Q.top == -1)return;
x = Q.data[Q.top--];
}
int DecTrans(int val, int n)
{
LinkQueue Q;
QueueInit(Q);
while (val != 0)
{
Push(Q, val%n);
val /= n;
}
int result=0, x;
while (Q.top != -1)
{
Pop(Q, x);
result = result * 10 + x;
}
return result;
}
int main()
{
cout << DecTrans(10, 2) << endl;
cout << DecTrans(8, 2) << endl;
getchar();
}
C++利用栈实现进制转换
最新推荐文章于 2022-12-31 22:26:42 发布