zoukankan      html  css  js  c++  java
  • 顺序栈实现数制的转换

    此例为十进制N转换为其它进制

    1.顺序栈的存储结构

    typedef struct{
      ElemType data[MAXSIZE];//为顺序栈分配最大容量的内存
      int top;  //指向栈顶
    }SqStack;
    View Code

    2.初始化栈

    void Initstack(SqStack &S)
    {
        if(!S.data) exit(-1);
        S.top = 0;
    }
    View Code

    3.入栈

    Status Push(SqStack &S,ElemType e)
    {
        if(S.top==MAXSIZE) return ERROR;
        S.data[S.top++] = e;
        return OK;
    }
    View Code

    4.出栈

    Status Pop(SqStack &S)
    {
       if(S.top<=0) return ERROR;
       S.top--;
       return OK;
    }
    View Code

    5.获得栈顶元素

    void getTop(SqStack S,ElemType &e)
    {
        if(S.top==0) printf("栈空了!!");
        e = S.data[S.top-1];
    }
    View Code

    6.进制转换

    void Convertion(SqStack &S)
    {
        int n,r;
        printf("输入要转换进制的数:
    ");
        scanf("%d",&n);
        printf("输入要转换的进制:
    ");
        scanf("%d",&r);
        while(n)
        {
            Push(S,n%r);
            n/=r;
        }
        while(S.top)
        {
            ElemType e;
            getTop(S,e);
            Pop(S);
            printf("%d",e);
        }
        printf("
    ");
    }
    View Code

    7.遍历

    void traverse(SqStack S)
    {
        printf("进制转换结果:
    ");
        for(int i=S.top-1;i>=0;i--)
        {
            printf("%d",S.data[i]);
        }
        printf("
    ");
    }
    View Code

    8.全部代码(这里就整合为一个了)

    #include<stdio.h>
    #include<stdlib.h>
    
    #define MAXSIZE 100
    #define ERROR 0
    #define OK 1
    
    typedef int Status;
    typedef int ElemType;
    
    typedef struct{
      ElemType data[MAXSIZE];//为顺序栈分配最大容量的内存
      int top;  //指向栈顶
    }SqStack;
    
    void Initstack(SqStack &S)
    {
        if(!S.data) exit(-1);
        S.top = 0;
    }
    
    Status Push(SqStack &S,ElemType e)
    {
        if(S.top==MAXSIZE) return ERROR;
        S.data[S.top++] = e;
        return OK;
    }
    
    Status Pop(SqStack &S)
    {
       if(S.top<=0) return ERROR;
       S.top--;
       return OK;
    }
    void getTop(SqStack S,ElemType &e)
    {
        if(S.top==0) printf("栈空了!!");
        e = S.data[S.top-1];
    }
    void Convertion(SqStack &S)
    {
        int n,r;
        printf("输入要转换进制的数:
    ");
        scanf("%d",&n);
        printf("输入要转换的进制:
    ");
        scanf("%d",&r);
        while(n)
        {
            Push(S,n%r);
            n/=r;
        }
        while(S.top)
        {
            ElemType e;
            getTop(S,e);
            Pop(S);
            printf("%d",e);
        }
        printf("
    ");
    }
    void traverse(SqStack S)
    {
        printf("进制转换结果:
    ");
        for(int i=S.top-1;i>=0;i--)
        {
            printf("%d",S.data[i]);
        }
        printf("
    ");
    }
    
    
    int main()
    {
        SqStack S;
    
        Initstack(S);
    
        Convertion(S);
    
       //我看了几个博客,都是用上面的方法,下面这个也可以
       //traverse(S);//这两个函数只能用一个
        return 0;
    }
    View Code
  • 相关阅读:
    【转】C#中的委托和事件
    ASP.NET上传文件实例代码
    我在配置OpenCV环境以及使用VS2013运行代码时遇到的问题
    JAVA_HOME环境变量失效的解决办法
    一道int和unsigned char之间强制类型转换的题目
    为什么不看好OpenStack:它的没落不可避免[转]
    一个销前谈云桌面的适用场景(再议云桌面营销之选择正确的客户环境)
    云桌面的前世今生
    云桌面三大谎言之GPU虚拟化
    如何理解清零心态
  • 原文地址:https://www.cnblogs.com/wwww2/p/11716412.html
Copyright © 2011-2022 走看看