C Language Stack queue implementation two-10/two-eight binary conversion

Source: Internet
Author: User

C Language Stack queue implementation two-10/two-eight binary conversion

2015-04-05 Lover Snow Child

1 //using the stack to find the decimal and octal results of binary numbers2#include <stdio.h>3#include <stdlib.h>4#include <math.h>5 6 #defineStack_init_size 20//Initial Stack size7 #defineStack_increment 10//increased memory each time the stack is expanded8 9typedefCharElemtype;//data types in the stackTentypedefstruct { OneElemtype *Base; AElemtype *top; -     intstackSize; - }sqstack; the  - //Initialize Stack - voidInit_stack (Sqstack *s) { -S->Base= (Elemtype *)malloc(Stack_init_size *sizeof(Elemtype)); +     if(!s->Base) -Exit (-1); +S->top = s->Base; AS->stacksize =stack_init_size; at } - //into the stack - voidPush (Sqstack *S,elemtype e) { -     if(s->top-s->Base>= s->stacksize) {//detects if the stack is full -         //if full, the stack space is automatically expanded -S->Base= (Elemtype *)realloc(s->Base, (s->stacksize + stack_increment) *sizeof(Elemtype)); in         if(!s->Base) -Exit (-1); to     } +* (s->top) =e; -s->top++; the } * //out of the stack $ voidPop (Sqstack *s, Elemtype *e) {Panax Notoginseng     if(S->top = = s->Base){ -         return ; the     } +*e = * (--(s->top)); A } the //the number of stack data, because we do not modify it, so there is no need to pass pointers + intStack_len (Sqstack s) { -     return(S.top-s.Base);//number of returned data $ } $  - intMainvoid){ - elemtype C; the Sqstack S; -     intLen =0, i =0, sum_10 =0;Wuyi     intj =0, Sum_8 =0, Tmp_8 =0; the      -Init_stack (&s);//Initialize Stack Wu      while(1){ -printf"Please enter a binary number, enter the # symbol to end, enter q/q exit!\n"); Aboutscanf"%c", &c);//receive by character format $         if(c = ='Q'|| c = ='Q') -              Break; -          while(c! ='#'){ -Push (&AMP;S,C);//into the stack Ascanf"%c",&c); +         } theGetChar ();//removes the carriage return from the current buffer and ends when the user presses ENTER -          $Len = Stack_len (s);//get the current capacity of the stack theprintf"the current capacity of the stack is:%d\n", Len); the  the          for(i =0; I<len; i++){ thePop (&s, &c);//pass-through values using reference methods -Sum_10 = (int) (Sum_10 + (C- -) * POW (2, i));//convert ASCII to reshape in  theTmp_8 = (int) (Tmp_8 + (C- -) * POW (2, i%3));//convert ASCII to reshape the  About             //printf ("%d%d\n", i%4,tmp_8); the             if(i%3==2){         theSum_8 + = (int) (Tmp_8 * POW (Ten, (j + +))); theTmp_8 =0; +             }     -         } the         if(Tmp_8! =0){BayiSum_8 + = (int) (Tmp_8 * POW (Ten, (j + +))); theTmp_8 =0; thej =0; -         } -printf"Convert to decimal number is:%d octal: 0%d\n", sum_10,sum_8); theSum_10 =0; theSum_8 =0; thej =0; the     } -     return 0; the}

C Language Stack queue implementation two-10/two-eight binary conversion

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.