-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp80_12.十进制转换为二至九进制.cpp
140 lines (125 loc) · 2.19 KB
/
p80_12.十进制转换为二至九进制.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<iostream>
using namespace std;
template <class T>
class Stack
{
public:
Stack(){}
~Stack(){}
void Clear(); //清空栈
bool Push( const T item); //栈的压入操作
bool Pop(T & item); //读取栈顶元素
bool Top(T & item); //读取栈顶元素的值但不删除
bool IsEmpty(); //判断栈是否为空
bool IsFUll(); //判断栈是否已满
/* data */
};
template <class T>
class LinkNode
{
public:
T data;
LinkNode<T> * link;
LinkNode(const T & el, LinkNode<T> * ptr = 0)
{
data = el;
link = ptr;
}
};
template <class T>
class LinkStack:public Stack<T>
{
private:
LinkNode <T> * top;
int size;
public:
LinkStack(int s = 0)
{
top = NULL;
size = 0;
}
~LinkStack()
{
Clear();
}
void Clear()
{
while ( top != NULL )
{
LinkNode<T> *tmp = top;
top = top->link;
delete tmp;
}
size = 0;
}
bool Push(const T item)
{
LinkNode<T> * tmp = new LinkNode<T>(item,top);
top = tmp;
size++;
return true;
}
bool Pop(T & item)
{
LinkNode<T> *tmp;
if(size == 0)
{
cout<<"栈为空,不能执行出栈操作"<<endl;
return false;
}
item = top->data;
tmp = top->link;
delete top;
top = tmp;
size--;
return true;
}
bool Top(T & item)
{
if(size == 0 )
{
cout<<"栈为空,不能读取栈顶元素" <<endl;
return false;
}
item = top->data;
return true;
}
bool IsEmpty()
{
if( size == 0)
{
return true;
}
else
{
return false;
}
}
};
void Change(int num,int x)
{
LinkStack<int> p;
int temp;
while( num >= x )
{
temp = num % x;
num = num / x;
p.Push(temp);
}
cout<<num;
while(!p.IsEmpty())
{
p.Pop(temp);
cout<<temp;
}
}
int main()
{
int a,b;
cout<<"输入一个十进制数:"<<endl;
cin>>a;
cout<<"输入你希望转换成的进制:"<<endl;
cin>>b;
Change(a,b);
return 0;
}