-
Notifications
You must be signed in to change notification settings - Fork 9
/
栈的应用_进制转换_.cpp
103 lines (94 loc) · 1.35 KB
/
栈的应用_进制转换_.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
#include<stdio.h>
#include<stdlib.h>
#define Max 100
#define False -1
#define True 1
typedef struct Stack
{
int data[Max];
int top;
}stack;
int init_stack(stack &s)
{
s.top=-1;
return 0;
}
int push_stack(stack &s,int e)
{
if(s.top>Max-1)
{
return False;
}
s.data[++s.top]=e;
//printf("pop-->%d\n",e);
return True;
}
int pop_stack(stack &s,int e)
{
if(s.top>-1)
{
e=s.data[s.top--];
//printf("push-->%d\n",e);
return e;
}
return False;
}
int top_stack(stack s)
{
if(s.top>-1)
{
return s.data[s.top];
}
return False;
}
int print_stack(stack s,int e)
{
while(s.top!=-1)
{
e=s.data[s.top--];
printf("push-->%d\n",e);
}
return True;
}
int len_stack(stack s)
{
int m=0;
while(s.top!=-1)
{
s.data[s.top--];
//printf("push-->%d\n",s.data[s.top--]);
m++;
}
return m;
}
int empty_stack(stack s)
{
if(s.top==-1)
{
return NULL;
}
return True;
}
int main()
{
stack s;
init_stack(s);
int N,e,t;
printf("请输入转换的数字:\n");
scanf("%d",&N);
printf("请输入转换为几进制:\n");
scanf("%d",&e);
while(N)
{
push_stack(s,N%e);
N=N/e;
}
printf("转换为的%d进制数为:\n",e);
while(empty_stack(s)!=NULL)
{
//printf("---------------\n");
t=pop_stack(s,t);
printf("%d ",t);
}
return 0;
}