数据结构-栈之二进制转十进制和八进制
生活随笔
收集整理的這篇文章主要介紹了
数据结构-栈之二进制转十进制和八进制
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
棧滿則后進(jìn)先出原則,我們可以利用此特性實(shí)現(xiàn)二進(jìn)制轉(zhuǎn)十進(jìn)制、二進(jìn)制轉(zhuǎn)八進(jìn)制、二進(jìn)制轉(zhuǎn)十六進(jìn)制等相關(guān)操作,廢話不多說,直接上代碼。
二進(jìn)制轉(zhuǎn)十進(jìn)制:
// // Created by Administrator on 2018/5/28. ////二進(jìn)制轉(zhuǎn)十進(jìn)制#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define ElementType int #define MaxSize 10定義棧的結(jié)構(gòu)體 typedef struct {ElementType *top;//棧頂指針(這里定義為指向棧頂元素的下一個位置,即為空)ElementType *base;//棧底指針int stackSize;//棧的容量 } Stack;/*** 初始化棧* @param s*/ void InitStack(Stack *s) {//初始化分配棧的總空間s->base = (ElementType *) malloc(MaxSize * sizeof(ElementType));if (!s->base) {//分配失敗exit(0);}s->top = s->base;s->stackSize = MaxSize; }/*** 入棧* @param s 棧* @param e 入棧元素*/ void Push(Stack *s, ElementType e) {//判斷棧是否已滿if (s->top - s->base >= s->stackSize) {//棧已滿//處理方式1.遞增空間 2.退出printf("棧已滿~\n");exit(0);}*(s->top) = e; //賦值s->top++; }/*** 出棧* @param s 棧* @return*/ ElementType Pop(Stack *s) {//判斷棧是否為空if (s->base == s->top) {//棧為空printf("不好意思,棧目前為空~\n");exit(0);}s->top--;ElementType e = *(s->top);//取值,并不是取地址return e; }/*** 釋放棧* @param s*/ void FreeStack(Stack *s) {if (!s) {printf("棧空,不需要釋放\n");} else {free(s->base);free(s);printf("棧釋放完成~\n");} }/*** 棧當(dāng)前容量* @param s* @return*/ ElementType GetLen(Stack s) {int len = (s.top - s.base);return len; }int main() {printf("二進(jìn)制轉(zhuǎn)十進(jìn)制!\n");Stack stack;InitStack(&stack);int data[] = {1, 0, 1, 1, 0, 1};int length = sizeof(data) / sizeof(data[0]); //數(shù)組占內(nèi)存總空間,除以單個元素占內(nèi)存空間大小int sum = 0;printf("二進(jìn)制數(shù)據(jù)入棧\n");for (int i = 0; i < length; i++) {Push(&stack, data[i]);int e = Pop(&stack);printf("%d", e);sum = sum + e * pow(2, i);}printf("\n十進(jìn)制結(jié)果=%d", sum);return 0; }?
二進(jìn)制轉(zhuǎn)八進(jìn)制:
// // Created by Administrator on 2018/5/28. ////二進(jìn)制轉(zhuǎn)八進(jìn)制 //注:二進(jìn)制轉(zhuǎn)八進(jìn)制是通過獲取二進(jìn)制中每三位數(shù)據(jù)計(jì)算一個八進(jìn)制值,如果高位不足三位則通過補(bǔ)0的方式湊齊 #include <stdio.h> #include <stdlib.h> #include <math.h>#define ElementType int #define MaxSize 10//定義棧的結(jié)構(gòu)體 typedef struct {ElementType *top;//棧頂指針(這里定義為指向棧頂元素的下一個位置,即為空)ElementType *base;//棧底指針int stackSize;//棧的容量 } Stack;/*** 初始化棧* @param s*/ void InitStack(Stack *s) {//初始化分配棧的總空間s->base = (ElementType *) malloc(MaxSize * sizeof(ElementType));if (!s->base) {//分配失敗exit(0);}s->top = s->base;s->stackSize = MaxSize; }/*** 入棧* @param s 棧* @param e 入棧元素*/ void Push(Stack *s, ElementType e) {//判斷棧是否已滿if (s->top - s->base >= s->stackSize) {//棧已滿//處理方式1.遞增空間 2.退出printf("棧已滿~\n");exit(0);}*(s->top) = e; //賦值s->top++; }/*** 出棧* @param s 棧* @return*/ ElementType Pop(Stack *s) {//判斷棧是否為空if (s->base == s->top) {//棧為空//printf("\n不好意思,棧目前為空~\n");return -1;}s->top--;ElementType e = *(s->top);//取值,并不是取地址return e; }/*** 釋放棧* @param s*/ void FreeStack(Stack *s) {if (!s) {printf("棧空,不需要釋放\n");} else {free(s->base);free(s);printf("棧釋放完成~\n");} }/*** 棧當(dāng)前容量* @param s* @return*/ ElementType GetLen(Stack s) {int len = (s.top - s.base);return len; }int main() {printf("二進(jìn)制轉(zhuǎn)八進(jìn)制!\n");Stack stack, stack1;InitStack(&stack);InitStack(&stack1);int data[] = {1, 0, 1, 1, 0,1,0};int length = sizeof(data) / sizeof(data[0]); //數(shù)組占內(nèi)存總空間,除以單個元素占內(nèi)存空間大小printf("二進(jìn)制數(shù)據(jù)入棧~\n");for (int i = 0; i < length; i++) {printf("%d", data[i]);Push(&stack, data[i]);}//獲取棧當(dāng)前的長度int currentLen = GetLen(stack);int sum = 0;int k = 1;if (currentLen < 3) {//如果棧當(dāng)前容量不足3位則補(bǔ)足3位currentLen = 3;} else {//如果當(dāng)前棧容量為基數(shù)即不是3的倍數(shù),則湊為3的倍數(shù)(方便接下來便利計(jì)算)int mod = currentLen % 3;if (mod != 0) {currentLen = currentLen + (3 - mod);}}for (int j = 0; j < currentLen; j++) {ElementType e = Pop(&stack);if (e == -1) {//如果二進(jìn)制中不存在數(shù)據(jù)(說明該棧不是3的倍數(shù)),通過高位補(bǔ)0的方式處理e = 0;if (k % 3 != 0) {k++;} else{sum = sum + e * pow(2, k - 1);Push(&stack1, sum);}} else {//二進(jìn)制棧中存在數(shù)據(jù),按取3位計(jì)算一次,然后入八進(jìn)制棧sum = sum + e * pow(2, k - 1);if (k % 3 == 0) {Push(&stack1, sum);sum = 0;k = 1;continue;}k++;}}int s2Len = GetLen(stack1);printf("\n八進(jìn)制結(jié)果數(shù)據(jù)出棧:\n");for (int l = 0; l < s2Len; l++) {ElementType e = Pop(&stack1);printf("%d", e);}return 0; }?
注:以上代碼為個人的拙見,存在不足之處,還請大家多多海涵!
總結(jié)
以上是生活随笔為你收集整理的数据结构-栈之二进制转十进制和八进制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构-栈在括号匹配中的应用
- 下一篇: 什么是云存储服务器