#include<iostream>
using namespace std;
const int max = 100;
class Stack
{
public:
Stack()
{
top = -1;
a = new int[max];
}
~Stack()
{
delete []a;
}
void clear()
{
top = -1;
}
bool push(int x)
{
if (top == max - 1)
{
cout << "栈满" << endl;
return false;
}
else
{
a[++top] = x;
return true;
}
}
bool pop(int s)
{
if (top == -1)
{
cout << "栈为空" << endl;
return false;
}
else
{
s = a[top--];
return true;
}
}
bool istop()
{
if (top == -1)
{
cout << "栈为空" << endl;
return false;
}
else
return true;
}
bool isempty()
{
if (top == -1)
return true;
else
return false;
}
bool isfull()
{
if (top == max - 1)
return true;
else
return false;
}
void print()
{
if(top != -1)
for (int i = top; i >= 0; i--)
cout << a[i] << " ";
cout << endl;
}
private:
int top;
int *a;
};
void run(int x)
{
Stack s;
for (int i = 2; i < 10; i++)
{
int t1 = x, t2 = 0;
while (t1 > 0)
{
t2 = t1 % i;
t1 = t1 / i;
s.push(t2);
}
s.print();
s.clear();
}
}
int main()
{
int x;
cout << "输入十进制数 " << endl;
cin >> x;
cout << "从上到下依次为2-8进制数" << endl;
run(x);
return 0;
}
用栈实现十进制数字转化其他进制数字(2-8)
最新推荐文章于 2022-11-01 16:06:58 发布