栈 —— 顺序栈 —— 二进制转换成十进制
来源:互联网 发布:黄金软件哪个好 编辑:程序博客网 时间:2024/05/22 03:06
栈
代码示例
/* function:sequence stack created by : xilong date: 2017.2.7*/#include "iostream"#include <stdlib.h>#include <math.h>using namespace std;#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define STACK_INIT_SIZE 20#define SARCKINCREMENT 10typedef int Status;typedef char Elemtype;typedef struct{ Elemtype *base; Elemtype *top; int stackSize;} sqStack;/* function: initialize the stack*/void Stack_Init(sqStack *s){ s->base = (Elemtype *)malloc(STACK_INIT_SIZE * sizeof(Elemtype)); if (!s->base) { exit(0); } s->top = s->base; // at the very begining, the top of the stack is as same as the base of the stack s->stackSize = STACK_INIT_SIZE; // size of the stack}/* function: push an element "e" into the top of the stack*/int Stack_Push(sqStack *s, Elemtype e){ if (s->top - s->base >= s->stackSize) { s->base = (Elemtype *)realloc(s->base, (s->stackSize + SARCKINCREMENT) * sizeof(Elemtype)); if (!s->base) { exit(0); } s->top = s->base + s->stackSize; // set the top of the stack because of using the function "realloc"(realloc==>malloc, copy) s->stackSize = s->stackSize + SARCKINCREMENT; // set the maximal size of the stack } *(s->top) = e; s->top++;}/* function: pop the top element*/Status Stack_Pop(sqStack *s, Elemtype *e){ if (s->top == s->base) // empty stack { return ERROR; } *e = *--(s->top); // move the pointer top down first, then take away the top element return OK;}/* function: clear the stack*/Status Stack_Clear(sqStack *s){ s->base = s->top; return OK;}/* function: destory the stack*/Status Stack_Destory(sqStack *s){ int i; for (i = 0; i < s->stackSize; i++) { free(s->base); s->base++; } s->base = s->top = NULL; s->stackSize = 0; return OK;}/* function: return the length of the stack*/int Stack_Length(sqStack s){ return(s.top - s.base);}void main(){ Elemtype c; sqStack s; int len, i, sum = 0; Stack_Init(&s); cout << "请输入二进制数,输入#符号表示结束!" << endl; scanf_s("%c", &c); while (c != '#') { Stack_Push(&s, c); scanf_s("%c", &c); } getchar(); cout << "打印当前容量:"; len = Stack_Length(s); cout << len << endl; cout << "出栈顺序为:"; for (i = 0; i < len; i++) { Stack_Pop(&s, &c); cout << c << " "; sum = sum + (c - 48) * pow(2, i); } cout << endl; cout << "二进制转换成十进制为:"; cout << sum << endl; system("pause");}
程序截图
0 0
- 顺序栈——将十进制转换为二进制
- 栈 —— 顺序栈 —— 二进制转换成十进制
- 顺序栈实现十进制转换二进制
- 十进制转换为二进制 八进制 ——用栈实现
- 用顺序栈实现十进制数转换成二进制数
- 实验三:十进制转换成二进制数(顺序栈)
- [SQL Server] 十进制转换为二进制——函数
- 【软考总结】——二进制、十进制的转换问题
- 栈-二进制转换为十进制
- 十进制转换成二进制
- 十进制转换成二进制
- 十进制转换成二进制
- 十进制转换成二进制
- 十进制转换成二进制
- 十进制转换成二进制
- 二进制转换成十进制
- 十进制转换成二进制
- 实验3:栈和队列的基本操作实现及其应用——十进制转换为二进制
- Mybatis 分页跑批处理数据简易框架
- java之数组以及使用Arrays类操作数组
- Sublime Text 3 绝对神器
- Win7,64位,Python中对图片进行 Base64 编码和解码
- Chrome for Mac键盘快捷键!
- 栈 —— 顺序栈 —— 二进制转换成十进制
- AlphaGo算法论文 神经网络加树搜索击败李世石
- 大数阶乘
- JavaWeb关于session生命周期的几种设置方法
- Super Jumping! Jumping! Jumping!
- Java集合细节(一):请为集合指定初始容量
- 视觉盛宴之INTERCATIVE DHTML
- 文章标题
- 在CentOS最小化服务器版本中如何查找网卡IP和其它详细信息?