栈的应用--进制转换

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

题目解读:整数: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  \n");
        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 \n");
        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("\n\n");
    return 0;
}
int main(void)
{
    Mystack *s ;
    int x ;
    printf("Please input the integer \nX: ");
    scanf("%d",&x);
    if(x == 0){
        printf(" 0 \n\n");
        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 \n");
        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 \n");
        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 \n");
        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("\n\n");
    return 0;

}
int main(void)
{
    int x;
    printf("Please input the \nX :");
    scanf("%d",&x);
    ExchangeBin(x);
    return 0;
}
是一种后进先出(Last In First Out,LIFO)的据结构,可以用来实现很多算法,其中一个就是应用十进制转八进制十进制转八进制的原理是将一个十进制不断除以八,直到商为0,然后将每次的余倒序排列起来即可得到八进制。我们可以使用来实现这个过程。 具体的算法步骤如下: 1. 初始化一个,用来存放余。 2. 对于给定的十进制n,不断进行以下操作,直到商为0: 1) 将n除以8,将余压入中。 2) 将n更新为商。 3. 从中依次弹出余,得到的结果即为八进制。 下面是使用C语言实现这个算法的代码示例: ```c #include <stdio.h> #define MAX_SIZE 100 int stack[MAX_SIZE]; int top = -1; void push(int x) { if (top == MAX_SIZE - 1) { printf("Stack overflow\n"); return; } stack[++top] = x; } int pop() { if (top == -1) { printf("Stack underflow\n"); return -1; } return stack[top--]; } void decimal_to_octal(int n) { while (n > 0) { push(n % 8); n /= 8; } while (top != -1) { printf("%d", pop()); } } int main() { int n; printf("Enter a decimal number: "); scanf("%d", &n); printf("Octal equivalent: "); decimal_to_octal(n); printf("\n"); return 0; } ``` 在这个程序中,我们使用了一个组来实现的功能,其中`push`函用来将元素压入中,`pop`函用来从中弹出元素。在`decimal_to_octal`函中,我们使用了来存储余,然后在弹出的过程中得到八进制。最后,在`main`函中我们获取用户输入的十进制并输出转换后的八进制。 希望这个算法的详解对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值