C + + Description Converts a 2 binary number to 10 digits (used to initialize the stack, into the stack, into the stack)

Source: Internet
Author: User

1 /*2 C + + description converts 2 binary numbers to 10 binary numbers3 problem, 1. After initializing the stack, with new, do not know whether the delete will write a function to free memory,4 or where you can add the delete5 2. If the stack is full, I want to allocate more space, I think the method is to delete the previous application6 space, re-apply with new, but C has a function7 s->base = (elemtype*) realloc (S->base, (s->stacksize + stackincrement) * sizeof (ELEMTYPE));//Allocate Space 8 I don't know if C + + has this kind of function. 9 */Ten#include <iostream> One#include <math.h> A using namespacestd; -  -  the Const intStack_init_size= -; - Const intStackincrement=Ten; -  -typedefCharElemtype;//define a string, because the binary only 0 and 1,0 ASCII table is 48, 1 is the second, +                         //as long as the stack element minus 48 is multiplied by the number of times 2, OK. -  +  Atypedefstruct  at { -elemtype* top;//Top of Stack -elemtype*Base;//Bottom of Stack -      intStackSize;//capacity of the stack - }sqstack; -  in  - voidInitstack (Sqstack *s)//Initialize Stack to {    +     Try -     { theS-Base=NewElemtype [Stack_init_size];//Application Space *S-top = s-Base;//Stack empty, stack top equals bottom of stack $S-stackSize =stack_init_size; Panax Notoginseng     } -  the        Catch(Bad_alloc) +        { Acout <<"Allocating space failed! "<<Endl; theExit (0); +        } - } $  $  - voidPush (Sqstack *s, Elemtype e)//into the stack - { the     Try{ -         if(Top-s, S-Base>= s->stackSize)Wuyi             {   the                  DeleteS-Base;//Delete previously allocated space and reassign -S-Base=NewElemtype[stack_init_size+Stackincrement]; Wu             } -* (s->top) = e;//assign a value and move the top of the stack upwards Abouts->top++; $         }    -     Catch(bad_alloc) { -cout <<"Allocating space failed! "<<Endl; -Exit (0); A     } + } the  - voidPop (Sqstack *s, Elemtype *e) $ { the        if(S--top = = s)Base) the        { thecout <<"stack is empty"<<Endl; theExit0); -        } in      theS--top-;//first point the top of the stack to the element, and then assign the element to the parameter the*e= * s->top;//*e = *--(s->top);  About        the } the  the intStacklen (Sqstack s)//returns the number of elements in a stack + { -    return(S.top-s.Base);//The essence is the address subtraction divided by Elemtype the } Bayi  the  the  - intMain () - { theElemtype C;//declares a character C theSqstack s;//declares a struct s the  the     intlen,i,sum=0;  -Initstack (&s);//Initialize Stack thecout <<"Please enter binary number, enter # End"<<Endl; theCIN >>C; the      while(c! ='#')94     { thePush (&s,c); theCin>>C; the     }98GetChar ();//Receive Enter AboutLen =Stacklen (s); -cout <<"Stack's when capacity"<<len<<Endl;101       for(i=0; I<len; i++)102      {103Pop (&s,&c);104sum = sum + (c- -) * POW (2, i);  the      }106cout <<"The conversion to decimal number is:"<<sum<<Endl;107       108 109      return 0; the}

C + + Description Converts a 2 binary number to 10 digits (used to initialize the stack, into the stack, into the stack)

Related Article

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.