-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_arry.cpp
More file actions
122 lines (102 loc) · 1.4 KB
/
Stack_arry.cpp
File metadata and controls
122 lines (102 loc) · 1.4 KB
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
#include <iostream>
#include <cstdlib>
using namespace std;
typedef int e_type;
typedef int * e_point;
class Stack
{
public:
Stack();
~Stack();
// Stack(const Stack& s);
bool pop();
bool push(const e_type &e);
int size() const;
bool is_empty() const;
e_type get_top() const;
// Stack operator =(const Stack &e);
private:
e_point base;
int size;
int increment;
int index;
e_point top;
}
Stack::Stack()
{
size = 100;
base = (e_type*)malloc(size * sizeof(e_type));
increment = 30;
top = base;
index = 0;
}
bool Stack::push(const e_type &e)
{
if (size == index)
{
base = (e_type*)realloc(base, (size + increment) * sizeof(e_type));
if (base == NULL)
return false;
size += insrement;
}
else
{
base[index++] = e;
top = &base[index];
}
}
bool Stack::pop()
{
if (top == base)
return false;
else
{
top = &base[--index];
return true;
}
}
e_type Stack::get_top() const
{
return *(top - 1);
}
bool Stack::is_empty() const
{
return base == top;
}
int Stack::size() const
{
return index;
}
Stack::~Stack()
{
delete [] base;
base = NULL;
top = NULL;
index = 0;
increment = 0;
size = 0;
}
int main()
{
int num;
int base;
while (cin >> num >> base)
{
Stack *s = new Stack();
while (num)
{
s->push(num % base);
num /= base;
}
while(true)
{
cout << s->get_top();
if (!s->pop())
{
cout << endl;
break;
}
}
}
return 0;
}