The code for converting a stack binary number to an octal number is as follows
#include <time.h> #include <math.h> # include <stdio.h> # include <stdlib.h> #define Stack_init_
SIZE #define STACKINCREMENT typedef char ELEMTYPE;
typedef struct {elemtype *base;//stack-bottom pointer elemtype *top;//stack top pointer int stacksize;//stack The maximum capacity currently usable}sqstack;
void Initstack (Sqstack *s) {s->base = (elemtype*) malloc (stack_init_size*sizeof (elemtype)); if (! (
S->base)) exit (0);
S->top = s->base;
S->stacksize = stack_init_size; } void Push (Sqstack *s,elemtype e) {if (s->top-s->base >= s->stacksize) {s->base = (Elem
type*) ReAlloc (S->base, (stackincrement + s->stacksize) *sizeof (elemtype)); if (! (
S->base)) exit (0);
S->top = s->base+ s->stacksize;
S->stacksize = s->stacksize + stackincrement;
} * (s->top) = e;
(s->top) + +;
} void Pob (Sqstack *s,elemtype *e) {if (s->top==s->base) return;
s->top--;
*e = * (S->top); } int StaCklen (Sqstack s) {return (s.top-s.base);}
int main () {Sqstack s,p;
int sum = 0;
Initstack (&s);
Initstack (&p);//The Stack elemtype C that stores octal numbers;
printf ("Output the binary number that will be converted, and end with +");
scanf ("%c", &c);
while (c!= ' + ') {Push (&s,c);
scanf ("%c", &c);
} int len = Stacklen (s);
printf ("Stack's current capacity%d\n", Len);
int num = LEN/3;
int tt = LEN%3;
int count = 0;
for (int i = 0; i < len;i++) {if (num>0) {Pob (&s,&c);
sum = sum + (c-48) *pow (2.0,count);
count++;
if (count==3) {num--;
c = sum + 48;
Push (&P,C);
sum = 0;
Count = 0;
}} else {Pob (&s,&c);
sum = sum + (c-48) *pow (2.0,count);
count++;
if (count = = TT) {c = sum + 48; Push (&P,C);
}}} len = Stacklen (p);
printf ("The current capacity of the octal stack%d\n", Len);
for (int i = 0;i < len;i++) {Pob (&p,&c);
printf ("%c", c);
} printf ("\ n");
return 0;
}