• 栈的应用--进制转换


    问题描述:给一个整数,要求把它转换成相应的二进制。

    题目解读:整数:1.考虑正负.2.如果整数是0

    具体思路:可能学过编程的娃都知道,十进制转二进制就是不断的给它取余,再取余,然后倒着写出来就行了。那么这个过程是不是正好满足栈先进后出的特点呐。嘿嘿~+~

    在这里,我们用两种方法来解决这个问题。一种是顺序栈,一种是链式栈。

    顺序栈

    #include<stdio.h>
    #include<stdlib.h>
    #define STACKSIZE   100
    typedef struct node{
        int top;
        int base;
        int *count ;   //容器
    }Mystack;
    
    int IsEmptyStack(Mystack *p) 
    {
        if(p->top == p->base )
            return 1;
        else return 0;
    }
    int pop(Mystack *p)  //出栈,判断是否栈空
    {
        int temp ;
        if(IsEmptyStack(p) == 1){
            printf("The stack is empty  
    ");
            return -1;
        }
        temp=p->count[p->top];
        p->top--;
        return temp;
    }
    int push(Mystack *p,int t)  //入栈,判断是否栈满
    {
        if(p->top - p->base == STACKSIZE){
            printf("The stack is full 
    ");
            return -1;
        }
        p->top++;
        p->count[p->top]=t ;
        return 0;
    }
    int ExchangeBin(Mystack *p,int x)
    {
        int tag= 0;  //tag == 1 就是负数
        int temp  = abs(x);
        if(x < 0) tag =  1;
        while(temp){         //核心代码
            push(p,temp%2);
            temp/=2;
        }
        if(tag == 1)
            printf(" -");
        while(IsEmptyStack(p) !=  1)
            printf("%2d",pop(p));
        printf("
    
    ");
        return 0;
    }
    int main(void)
    {
        Mystack *s ;
        int x ;
        printf("Please input the integer 
    X: ");
        scanf("%d",&x);
        if(x == 0){
            printf(" 0 
    
    ");
            return 0;
        }
        s=(Mystack *)malloc(sizeof(Mystack) );
        s->count=(int *)malloc(sizeof(int)*STACKSIZE);
        s->top = s->base = -1 ;
    
        ExchangeBin(s,x);
        free(s->count);
        free(s);
        return 0;
    }

    链式栈

    #include<stdio.h>
    #include<stdlib.h>
    #define STACKSIZE 100 
    typedef struct node{
        int data;
    }NODE ;
    typedef struct stack{
        NODE *top;
        NODE *base;
        int StackSize ;
    }Mystack;
    
    int InitStack(Mystack *p)
    {
        p->top = p->base =(NODE *)malloc(sizeof(NODE)*STACKSIZE);
        if(p->top == NULL)
        {
            printf("malloc  error 
    ");
            return -1;
        }
        p->StackSize = STACKSIZE ;
        return 0;
    }
    int IsEmptyStack(Mystack *p)
    {
        if(p->top == p->base)
            return 1;
        else return 0;
    }
    int pop(Mystack *p)
    {
        NODE temp;
        if(IsEmptyStack(p)){
            printf("The stack is empty 
    ");
            return 0;
        }
        temp=*(p->top-1);
        p->top--;
        return temp.data;
    }
    int push(Mystack *p,int t)
    {
        NODE element;
        if(p->top - p->base == STACKSIZE){
            printf("The stack is full 
    ");
            return -1;
        }
        element.data = t ;
        *(p->top) = element ;
        p->top++;
    }
    int ExchangeBin(int x)
    {
        Mystack ty ;
        int tag =  0 ;
        int temp = abs(x ) ;
        if(x< 0)   tag = 1;
        InitStack(&ty);
        while(temp) 
        { 
            push(&ty,temp%2);
            temp /=2 ;
        }
        if(tag)   printf(" -");
        while(IsEmptyStack(&ty) != 1)
            printf("%2d",pop(&ty));
        printf("
    
    ");
        return 0;
    
    }
    int main(void)
    {
        int x;
        printf("Please input the 
    X :");
        scanf("%d",&x);
        ExchangeBin(x);
        return 0;
    }
    
  • 相关阅读:
    模板
    洛谷
    Codeforces
    Codeforces
    Codeforces
    Codeforces
    洛谷
    洛谷
    洛谷
    NOIP 普及组 2016 海港
  • 原文地址:https://www.cnblogs.com/Tattoo-Welkin/p/10335328.html
Copyright © 2020-2023  润新知