zoukankan      html  css  js  c++  java
  • 栈的应用之数制转换

    #include <stdio.h>
    
    
    #define STACKSIZE 110
    #define OK 1
    #define TRUE 1
    #define FALSE 0
    int flag;
    
    typedef int ElemType;
    typedef int Status;
    typedef struct
    {
      ElemType data[STACKSIZE];
      int top;
    }SeqStack;
    
    void InitStack(SeqStack *s)
    {
     (*s).top = 0;
    }
    
    Status StackFull(SeqStack s)
    {
     if(s.top==STACKSIZE-1) return FALSE;
     return TRUE;
    }
    
    void Push(SeqStack *s, ElemType p)
    {
        if(!StackFull(*s)) 
        {
            printf("OverFlow
    ");
            flag=1;
        }
     (*s).data[(*s).top]=p;
     (*s).top++;
    }
    
    Status StackEmpty(SeqStack s)
    {
      if(s.top==0) return FALSE;
      return TRUE;
    }
    
    Status Pop(SeqStack *s,ElemType *p)
    {
     if(!StackEmpty(*s)) return FALSE;
     (*s).top--;
      *p=(*s).data[(*s).top];
      return TRUE;
    }
    
    void conversion()
    {
        ElemType n, m;
        int p;
        SeqStack s;
        flag = 0;
      printf("请输入一个数字:
    ");
           scanf("%d", &n);
       printf("请输入需要转换的数制:
    ");
           scanf("%d", &m);
    
      InitStack(&s);
    
      while(n)
      {
       Push(&s, n%m);
       n = n/m;
      }
      
      if(!flag)
      {
       while(StackEmpty(s)==1)
      {
        Pop(&s, &p);
        printf("%d", p);
      }
      printf("
    ");
      }
     
    }
    
    int main()
    {
       conversion();
    
     return 0;
    }
  • 相关阅读:
    断点调试
    内部类
    继承2
    继承
    构造函数
    方法
    二维数组
    HTML 一
    使用mySQL与数据库进行交互(一)
    使用mySQL与数据库进行交互(二)
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5980095.html
Copyright © 2011-2022 走看看