基于栈的数制转换
#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef int Elemtype;
typedef struct
{
Elemtype elem[MAXSIZE];
int top;
}Seqstack;
void Initstack(Seqstack &s);
bool Isempty(Seqstack s);
bool Isfull(Seqstack s);
void Push(Seqstack &s, Elemtype e);
void Pop(Seqstack &s, Elemtype &e);
int main()
{
Seqstack s;
Initstack(s);
Elemtype e;
int m, n;
cout << "请输入要转换的进制:";
cin >> n;
cout << "请输入要转换的十进制数:";
cin >> m;
while (m)
{
Push(s, m%n);
m /= n;
}
while (!Isempty(s))
{
Pop(s, e);
if (e == 10)cout << "A";
else if (e == 11)cout << "B";
else if (e == 12)cout << "C";
else if (e == 13)cout << "D";
else if (e == 14)cout << "E";
else if (e == 15)cout << "F";
else cout << e;
}
cout << endl;
return 0;
}
void Initstack(Seqstack &s)
{
s.top = -1;
}
bool Isempty(Seqstack s)
{
if (s.top == -1)
{
return true;
}
else
{
return false;
}
}
bool Isfull(Seqstack s)
{
if (s.top == MAXSIZE - 1)
{
return true;
}
else
{
return false;
}
}
void Push(Seqstack &s, Elemtype e)
{
if (Isfull(s))
{
cout << "顺序栈已满!" << endl;
}
else
{
s.top++;
s.elem[s.top] = e;
}
}
void Pop(Seqstack &s, Elemtype &e)
{
if (Isempty(s))
{
cout << "顺序栈为空!" << endl;
}
else
{
e = s.elem[s.top];
s.top--;
}
}