题目要求:
编写一个程序,要求从终端输入一串0/1表示的二进制数,输出它的八进制表示形式。
二进制转八进制的过程
将栈A的10转换为2,存放到栈B中;
主要考点是练习动态申请内存空间;
#include "stdio.h"#include "math.h"#define STACK_INIT_SIZE 20#define STACKINCREMENT 10typedef char ElemType;typedef struct{ ElemType *base; ElemType *top; int stacksize;}sqStack;/*初始化栈*/void initStack(sqStack *s){ /*内存中开辟一段连续空间作为栈空间,首地址赋值给s->base*/ s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if(!s->base) exit(0); /*分配空间失败*/ s->top = s->base; /*最开始,栈顶就是栈底*/ s->stacksize = STACK_INIT_SIZE; /*最大容量为STACK_INIT_SIZE */}/*入栈操作,将e压入栈中*/void Push(sqStack *s, ElemType e){ if(s->top - s->base >= s->stacksize){ /*栈满,追加空间*/ s->base = (ElemType *)realloc(s->base, (s->stacksize STACKINCREMENT)*sizeof(ElemType)); if(!s->base) exit(0);/*存储分配失败*/ s->top = s->base s->stacksize; s->stacksize = s->stacksize STACKINCREMENT;/*设置栈的最大容量*/ } *(s->top) = e; /*放入数据*/ s->top ;}/*出栈操作,用e将栈顶元素返回*/void Pop(sqStack *s , ElemType *e){ if(s->top == s->base) return; *e = *--(s->top); }/*计算栈s的当前长度*/int StackLen(sqStack s){ return (s.top - s.base) ; }main(){ ElemType c; sqStack s1; sqStack s2; int len,i,j,sum = 0;initStack(&s1);/*创建一个栈s1,用来存放二进制字符串*/ printf("Please input a binary number and type # for endn"); /*输入0/1字符表示的二进制数,以#结束*/ scanf("%c",&c); while(c!='#') { if(c=='0' || c=='1') Push(&s1,c); scanf("%c",&c); } initStack(&s2);/*创建一个栈s2,用来存放八进制字符串*/ len = StackLen(s1);/*得到栈中的元素个数,即二进制数的长度*/ for(i=0;i运行结果:
运行结果