//十进制转换别的进制
#include<iostream>
#include<cstdlib>
using namespace std;
#define max 10
struct storage
{
int a[max];
int top;
};
//初始化
void initstorage(struct storage* S)
{
S->top = -1;
}
void push(struct storage* S, int b)//进栈
{
if (S->top != max - 1)
{
S->top++;
S->a[S->top] = b;
}
}
void pop(struct storage* S, int* b)//出栈
{
int c = S->top;
if (S->top != -1)
{
for (size_t i = 0; i <= c; i++)
{
b[i] = S->a[S->top];
S->top--;
}
}
}
int main()
{
struct storage* S;
S = new storage;
int i, a, count, result[10] = { 0 };
cout << "=====欢迎使用进制转换======" << endl;
begin:cout << "请输入数(十进制):";
cin >> i;
initstorage(S);
cout << "请输入需要转换的进制(1-10之间):";
cin >> a;
//计算进制转换的数
while (i != 0)
{
push(S, i % a);
i = i / a;
}
cout << "转换的" << a << "进制为:";
count = S->top;
if (S->top != -1)
{
pop(S, result);
}
for (size_t i = 0; i <= count; i++)
{
cout << result[i];
}
cout << endl;
goto begin;
return 0;
}
栈 十进制转换别的进制
最新推荐文章于 2022-03-27 16:32:13 发布