Converting binary to octal based on stack binary

Source: Internet
Author: User

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 (&AMP;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 (&AMP;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;


 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.