/*
从键盘上输入一个十进制数n,试编写一个算法,
将其转换成对应的p进制输出(p为2.8.16)
*/
复制代码
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# include <iostream> # include <stdlib.h> # include <stdio.h> using namespace std; const int maxn = 1000 + 10; typedef struct Stack { int *base; int *top; int stacksize; }sqstack; void createsqstack(sqstack &s) { s.base = (int*) malloc (maxn * sizeof(int)); s.top = s.base; s.stacksize = maxn; } void push(sqstack &s, int n) { *s.top++ = n; } int pop(sqstack &s) { return *--s.top; } bool is_stackempty(sqstack s)//判断栈是否为空 { if(s.base == s.top) return true; else return false; } void converison(sqstack &s, int n, int base) { while(n) { push(s, n%base); n /= base; } while(!is_stackempty(s)) { int x = pop(s); printf("%d ", x); } printf("n"); } int main() { sqstack s; createsqstack (s); int n, base; printf("请输入要转换的数字和进制数:n"); scanf("%d%d", &n, &base); converison(s, n, base); return 0; }
最后
以上就是兴奋金针菇最近收集整理的关于数据结构例23.将十进制转换成对应进制的全部内容,更多相关数据结构例23内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复