• 数据结构——进制转化


     1 #include <stdio.h>
     2 
     3 #define StackSize 100
     4 #define OK  1
     5 #define ERROR 0
     6 #define TRUE  1
     7 #define FALSE 0
     8 
     9 typedef int ElemType;
    10 typedef int Status;
    11 typedef struct
    12 {
    13     ElemType data[StackSize];
    14     int top; //指向栈顶元素的下一个位置
    15 
    16 }SeqStack;
    17 
    18 //初始化、置空栈
    19 void InitStack(SeqStack *S)
    20 {
    21     S->top = 0;
    22 }
    23 //判断栈空
    24 int StackEmpty(SeqStack S)
    25 {
    26     if(S.top == 0) return TRUE;
    27     else return FALSE;
    28 }
    29 
    30 //判断栈满
    31 int StackFull(SeqStack S)
    32 {
    33     if(S.top == StackSize) return TRUE;
    34     else return FALSE;
    35 }
    36 //入栈
    37 Status Push(SeqStack *S,ElemType e)
    38 {
    39     if(StackFull(*S)) return ERROR;
    40     S->data[S->top++] = e;
    41     return OK;
    42 }
    43 
    44 //出栈
    45 Status Pop(SeqStack *S)
    46 {
    47     if(StackEmpty(*S)) return ERROR;
    48     S->top--;
    49     return OK;
    50 }
    51 
    52 //出栈,返回删除的栈顶元素
    53 Status Pop2(SeqStack *S,ElemType *e)
    54 {
    55     if(StackEmpty(*S)) return ERROR;
    56     *e = S->data[S->top-1];
    57     S->top--;
    58     return OK;
    59 }
    60 
    61 
    62 
    63 //得到栈顶元素
    64 Status GetTop(SeqStack S,ElemType *e)
    65 {
    66     if(StackEmpty(S)) return ERROR;
    67     *e = S.data[S.top-1];
    68     return OK;
    69 }
    70 
    71 
    72 int main()
    73 {
    74     SeqStack S;
    75     int d;
    76     int n,r;
    77     InitStack(&S);
    78     printf("请输入一个数:
    ");
    79     scanf("%d",&n);
    80     printf("请输入需要转化的进制:
    ");
    81     scanf("%d",&r);
    82     while(n)
    83     {
    84         Push(&S,n%r);
    85         n /= r;
    86     }
    87     while(!StackEmpty(S))
    88     {
    89         Pop2(&S,&d);
    90         printf("%d",d);
    91     }
    92     printf("
    ");
    93     return 0;
    94 }
    View Code
  • 相关阅读:
    【luogu P4139】 上帝与集合的正确用法
    pb_ds学习
    【luogu P3868】 [TJOI2009]猜数字
    BZOJ3040: 最短路(road)
    【luogu P1064】 金明的预算方案
    【luogu P2893】 [USACO08FEB]修路Making the Grade
    【luogu P2801】 教主的魔法
    UVA10816 Travel in Desert
    P2916 [USACO08NOV]安慰奶牛Cheering up the Cow
    【BZOJ 2054】 疯狂的馒头
  • 原文地址:https://www.cnblogs.com/biu-biu-biu-/p/6063430.html
Copyright © 2020-2023  润新知