栈应用:实现二进制转八进制、十进制、十六进制
生活随笔
收集整理的這篇文章主要介紹了
栈应用:实现二进制转八进制、十进制、十六进制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
進制轉換原理
二進制轉十進制
二進制是計算機數據的存儲形式,它是由一串0和1組成,每個二進制數轉換成相應的十進制數方法為:(XnXn-1Xn-2...X3X2X1)2 = X1*2^0+X2*^1+...Xn*2^(n-1)。
二進制轉八進制
利用二進制轉十進制原理,從低位起將每3位二進制轉為1位十進制 然后進行替換即可。二進制轉十六進制
利用二進制轉十進制原理,從低位起將每4二進制位轉為1位十進制然后進行替換即可,不過在16進制中用 a、b、c、d、e、f 代替10、11、12、13、14、15。如何用棧實現進制轉換?
如果對棧這種數據結構不熟悉的,可以先看?棧的順序存儲及實現(二)、棧的順序存儲及實現(一)。
二進制轉十進制
首先我們來看二進制轉十進制。利用棧先進后出的原理,將輸入的二進制數壓入棧中,高位在最下面,低位在上面,然后進行彈棧,將彈出的數轉換為相應的十進制 進行累加即可得到對應的十進制。局部代碼:
//二進制數棧SeqStack binaryStack;InitStack(&binaryStack);while (1){char num = 0;scanf("%c", &num);if (num == '\n'){break;}//壓棧push(&binaryStack, num);}int i = 0;int dec = 0;//十進制數while (!IsEmptyStack(&binaryStack)){char num = 0;pop(&binaryStack, &num);dec += (num - '0')*pow(2, i);i++;}
二進制轉八進制
同樣是先把二進制數壓棧,然后 彈棧,不過 的是 3位二進制才轉為1位八進制,這樣就要把轉出來的八進制放到另一棧中,此時八進制數的低位在棧底,高位在棧頂,再次利用棧的先進后出的原理進行彈棧,高位先彈出來,依次進行輸出 就得到相應的八進制了。
局部代碼:
SeqStack binaryStack;//二進制數棧SeqStack octStack;//八進制數棧InitStack(&binaryStack);InitStack(&octStack);while (1){char num = 0;scanf("%c", &num);if (num == '\n'){break;}//壓棧push(&binaryStack, num);}while (1){char num = 0;int oct = 0;//8進制數for (int i = 0; i < 3; i++){if (IsEmptyStack(&binaryStack)){break;}pop(&binaryStack, &num);oct += (num - '0')*pow(2, i);}//將八進制數壓入到棧中push(&octStack, oct + '0');if (IsEmptyStack(&binaryStack)){break;}}
二進制轉十六進制
和二進制轉八進制一模一樣,不過的是需要4位二進制位轉1位十六進制,其余都是一樣。
局部代碼:
SeqStack binaryStack;//二進制數棧SeqStack hexStack;//十六進制數棧InitStack(&binaryStack);InitStack(&hexStack);while (1){char num = 0;scanf("%c", &num);if (num == '\n'){break;}//壓棧push(&binaryStack, num);}while (1){char num = 0;int oct = 0;//十六進制數for (int i = 0; i < 4; i++){if (IsEmptyStack(&binaryStack)){break;}pop(&binaryStack, &num);oct += (num - '0')*pow(2, i);}//將十六進制數壓入到棧中if (oct<10){push(&hexStack, oct + '0');}else{push(&hexStack, oct-10 + 'a');}if (IsEmptyStack(&binaryStack)){break;}}
完整代碼
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define STACK_INIT_SIZE 5 #define STACK_INCREMENT 5 typedef int Status; typedef char EleType;typedef struct SeqStack {EleType* top;//棧頂指針EleType* base;//棧底指針int stackSize;//棧容量 }SeqStack; //初始化棧 Status InitStack(SeqStack* stack) {//開辟空間stack->base = stack->top = (EleType*)malloc(STACK_INIT_SIZE * sizeof(EleType));if (!stack->base){exit(0);}return OK; } //壓棧 Status push(SeqStack* stack, EleType e) {if (stack == NULL){return ERROR;}//壓棧之前檢測容量是否足夠if (stack->top - stack->base == stack->stackSize){//超出容量 進行擴容,使用realloc函數,會拷貝原內存內容stack->base = (SeqStack*)realloc(stack->base, stack->stackSize + STACK_INCREMENT);if (!stack->base){exit(0);}stack->top = stack->base + stack->stackSize;stack->stackSize += STACK_INCREMENT;}*stack->top = e;stack->top++;return OK; } //彈棧 Status pop(SeqStack* stack, EleType *e) {if (stack == NULL || e == NULL){return ERROR;}//空棧if (stack->top == stack->base){return ERROR;}*stack->top--;*e = *stack->top;return OK; } /* 判斷棧是否為空 */ int IsEmptyStack(SeqStack* stack) {if (NULL == stack) {return ERROR;}if (stack->top == stack->base) {return TRUE;}return FALSE; } /* 銷毀棧 */ Status DestroyStack(SeqStack* stack) {if (NULL == stack) {return ERROR;}//銷毀棧 是釋放棧在內存中占用的空間資源if (!stack->base){free(stack->base);}stack->top = stack->base = NULL;stack->stackSize = 0;return OK; } //二進制 轉 十進制 void BinaryToDeci() {printf("請輸入二進制:");//二進制數棧SeqStack binaryStack;InitStack(&binaryStack);while (1){char num = 0;scanf("%c", &num);if (num == '\n'){break;}//壓棧push(&binaryStack, num);}int i = 0;int dec = 0;//十進制數while (!IsEmptyStack(&binaryStack)){char num = 0;pop(&binaryStack, &num);dec += (num - '0')*pow(2, i);i++;}printf("對應的十進制:%d\n", dec);DestroyStack(&binaryStack); } //二進制 轉 八進制 void BinaryToOct() {printf("請輸入二進制:");SeqStack binaryStack;//二進制數棧SeqStack octStack;//八進制數棧InitStack(&binaryStack);InitStack(&octStack);while (1){char num = 0;scanf("%c", &num);if (num == '\n'){break;}//壓棧push(&binaryStack, num);}while (1){char num = 0;int oct = 0;//8進制數for (int i = 0; i < 3; i++){if (IsEmptyStack(&binaryStack)){break;}pop(&binaryStack, &num);oct += (num - '0')*pow(2, i);}//將八進制數壓入到棧中push(&octStack, oct + '0');if (IsEmptyStack(&binaryStack)){break;}}printf("對應的八進制:");while (!IsEmptyStack(&octStack)){char num = 0;pop(&octStack, &num);putchar(num);}printf("\n");DestroyStack(&binaryStack);DestroyStack(&octStack); } //二進制 轉 十六進制 void BinaryToHex() {printf("請輸入二進制:");SeqStack binaryStack;//二進制數棧SeqStack hexStack;//十六進制數棧InitStack(&binaryStack);InitStack(&hexStack);while (1){char num = 0;scanf("%c", &num);if (num == '\n'){break;}//壓棧push(&binaryStack, num);}while (1){char num = 0;int oct = 0;//十六進制數for (int i = 0; i < 4; i++){if (IsEmptyStack(&binaryStack)){break;}pop(&binaryStack, &num);oct += (num - '0')*pow(2, i);}//將十六進制數壓入到棧中if (oct<10){push(&hexStack, oct + '0');}else{push(&hexStack, oct-10 + 'a');}if (IsEmptyStack(&binaryStack)){break;}}printf("對應的十六進制:");while (!IsEmptyStack(&hexStack)){char num = 0;pop(&hexStack, &num);putchar(num);}printf("\n");DestroyStack(&binaryStack);DestroyStack(&hexStack); }//二進制 轉 八進制 int main(int argc, char *argv[]) {int menu = 1;while (menu){printf("-------菜單-------\n");printf("-------1、二進制轉十進制-------\n");printf("-------2、二進制轉八進制-------\n");printf("-------3、二進制轉十六進制-------\n");printf("-------0、退出-------\n");printf("請輸入菜單:");scanf("%d", &menu);getchar();//去掉換行符 \nswitch (menu) {case 1:BinaryToDeci();break;case 2:BinaryToOct();break;case 3:BinaryToHex();break;default:menu = 0;break;}}return 0; }
運行結果
總結
以上是生活随笔為你收集整理的栈应用:实现二进制转八进制、十进制、十六进制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过动态获取cookie爬取国家企业信用
- 下一篇: 对HashMap对象的键值对内容进行排序