第五周项目4-数制转换
来源:互联网 发布:淘宝运费模板价格 编辑:程序博客网 时间:2024/05/25 01:34
问题描述及代码:[cpp] view plain copy1./* 2.*烟台大学计控学院 3.*作 者:朱建豪 4.*完成日期:2016年9月30日 5.*问题描述【项目 - 数制转换】 把十进制的整数转换为任一进制数输出。请利用栈设计算法,并实现程序。 6. 7. 8.提示:要转换为r进制,则原来的数逐次除以基数r(除完之后用商再除),直到商为0,得到的一系列余数的逆序就是转换结果。这里的“逆序”,意味着后产生的余数,会先输出,后进先出,栈的机会来了…… 9. 10.*/ (1)sqstack.h[cpp] view plain copy1.#ifndef SQSTACK_H_INCLUDED 2.#define SQSTACK_H_INCLUDED 3. 4.#define MaxSize 100 5.typedef int ElemType; 6.typedef struct 7.{ 8. ElemType data[MaxSize]; 9. int top; //栈指针 10.} SqStack; //顺序栈类型定义 11. 12.void InitStack(SqStack *&s); //初始化栈 13.void DestroyStack(SqStack *&s); //销毁栈 14.bool StackEmpty(SqStack *s); //栈是否为空 15.int StackLength(SqStack *s); //返回栈中元素个数——栈长度 16.bool Push(SqStack *&s,ElemType e); //入栈 17.bool Pop(SqStack *&s,ElemType &e); //出栈 18.bool GetTop(SqStack *s,ElemType &e); //取栈顶数据元素 19.void DispStack(SqStack *s); //输出栈 20.void MultiBaseOutput (int number,int base); 21. 22. 23. 24. 25.#endif // SQSTACK_H_INCLUDED (2)sqstack.cpp[cpp] view plain copy1.#include"sqstack.h" 2.#include<stdio.h> 3.#include<malloc.h> 4.void InitStack(SqStack *&s) 5.{ 6. s=(SqStack *)malloc(sizeof(SqStack)); 7. s->top=-1; 8.} 9.void DestroyStack(SqStack *&s) 10.{ 11. free(s); 12.} 13.int StackLength(SqStack *s) //返回栈中元素个数——栈长度 14.{ 15. return(s->top+1); 16.} 17.bool StackEmpty(SqStack *s) 18.{ 19. return(s->top==-1); 20.} 21.bool Push(SqStack *&s,ElemType e) 22.{ 23. if (s->top==MaxSize-1) //栈满的情况,即栈上溢出 24. return false; 25. s->top++; 26. s->data[s->top]=e; 27. return true; 28.} 29.bool Pop(SqStack *&s,ElemType &e) 30.{ 31. if (s->top==-1) //栈为空的情况,即栈下溢出 32. return false; 33. e=s->data[s->top]; 34. s->top--; 35. return true; 36.} 37.bool GetTop(SqStack *s,ElemType &e) 38.{ 39. if (s->top==-1) //栈为空的情况,即栈下溢出 40. return false; 41. e=s->data[s->top]; 42. return true; 43.} 44. 45.void DispStack(SqStack *s) //输出栈 46.{ 47. int i; 48. for (i=s->top;i>=0;i--) 49. printf("%c ",s->data[i]); 50. printf("\n"); 51.} 52.void MultiBaseOutput (int number,int base) 53.{ 54. //假设number是非负的十进制整数,输出等值的base进制数 55. int i; 56. SqStack *S; 57. InitStack(S); 58. while(number)//从右向左产生base进制的各位数字,并将其进栈 59. { 60. Push(S,number%base);//将将余数进栈 61. number/=base; 62. 63. } 64. while(!StackEmpty(S))//栈非空时退栈输出 65. { 66. Pop(S,i); 67. printf("%d",i); 68. 69. } 70. 71.} (3)main.cpp[cpp] view plain copy1.#include"sqstack.h" 2.#include<stdio.h> 3.int main() 4.{ 5. MultiBaseOutput(10, 2); 6. return 0; 7.} 运行结果:<img src="http://img.blog.csdn.net/20160930110043878?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />知识点总结要转换为r进制,则原来的数逐次除以基数r(除完之后用商再除),直到商为0,得到的一系列余数的逆序就是转换结果。这里的“逆序”,意味着后产生的余数,会先输出,后进先出是栈的特点。学习心得:栈的应用,从算法分析问题,再考虑栈的特点性质。
0 0
- 第五周项目4--数制转换
- 第五周 项目4-数制转换
- 第五周项目4 -数制转换
- 第五周 项目4 - 数制转换
- 第五周项目4-数制转换
- 第五周项目4-数制转换
- 第五周项目4-数制转换
- 第五周 项目4-数制转换
- 第五周-- 项目4 - 数制转换
- 第五周 项目4--数制转换
- 第五周 项目4 - 数制转换
- 第五周项目4-数制转换
- 第五周项目4-数制转换
- 第五周项目4-数制转换
- 第五周项目4 数制转换
- 【第五周项目4-数制转换】
- 第五周项目4-数制转换
- (第五周项目4)数制转换
- 软考之语言
- Appium 服务命令行参数
- 微信小程序开发:http请求
- SSH QuartZ Cron表达式
- 3.模仿qq空间背景下拉动画
- 第五周项目4-数制转换
- 最新今日头条面试题目(js闭包、原型、继承、作用域)(2016)
- 巴别时代校招面试
- Kinect .ply数据读取、可视化
- 第五周项目2-建立链栈算法库
- cpp学习笔记(二)
- Android4.2中应用接收开机广播(android.intent.action.BOOT_COMPLETED)失败的原因
- wireshark 过滤条件设置
- 关注用文章