国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

棧 —— 順序棧 —— 二進制轉換成十進制

2019-11-11 01:45:30
字體:
來源:轉載
供稿:網友

這里寫圖片描述

代碼示例

/* 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");}

程序截圖

這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南召县| 东源县| 永登县| 湘潭市| 嘉兴市| 彭州市| 犍为县| 扬州市| 成武县| 中西区| 准格尔旗| 赤水市| 谢通门县| 北辰区| 连南| 微博| 从江县| 双流县| 马边| 湖南省| 东丰县| 嘉义县| 泉州市| 康马县| 泰来县| 衡南县| 思茅市| 哈密市| 南和县| 大安市| 平谷区| 清水河县| 迁西县| 宁陕县| 自治县| 奉节县| 仁布县| 加查县| 阿克陶县| 洞口县| 盐城市|