zoukankan      html  css  js  c++  java
  • C语言 数据结构 将十进制转换为二进制

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define initSize 9 
     4 typedef int SElemType; 
     5 typedef struct 
     6 {
     7     SElemType *elem;
     8     int  maxSize,top;
     9 }SeqStack;
    10 void InitStack(SeqStack *S)
    11 {
    12      
    13      S->.elem=(SElemType *)malloc(initSize*sizeof(SElemType));
    14      if(S->elem==NULL)
    15      {
    16          printf("存储分配失败
    ");
    17         exit(1);
    18     }
    19     S->maxSize=initSize;
    20     S->top=-1; 
    21 }
    22 int Push(SeqStack *S,SElemType x)
    23 {
    24     if(S->top==S->maxSize-1)
    25     {
    26         return 0;
    27     }
    28     S->elem[++S->top]=x;
    29     return 1; 
    30 }
    31 int Pop(SeqStack *S,SElemType *x)
    32 {
    33     if(S->top==-1)
    34     {
    35         return 0; 
    36     }
    37     *x=S->elem[(*S).top--]; 
    38     return 1;
    39 }
    40 int StackEmpty(SeqStack *S)
    41 {
    42     return S->top==-1;
    43 } 
    44 void Reverse(SElemType A[],int n)
    45 {
    46     SeqStack S;
    47     InitStack(&S);
    48     int i;
    49     for(int i=1;i<=n;i++)
    50     {
    51         Push(&S,A[i-1]);
    52     }
    53     i=0;
    54     while(!StackEmpty(&S))
    55     {
    56         Pop(&S,&A[i++]); 
    57     } 
    58 }
    59 main()
    60 {
    61     int x,n=0;
    62     int A[9]; 
    63     scanf("%d",&x);
    64     while(x!=0)
    65     {
    66         A[n++]=x%2;
    67         x=x/2;
    68     } 
    69     Reverse(A,n);
    70     for(int i=0;i<n;i++)
    71     {
    72         printf("%3d",A[i]);
    73     }
    74     printf("
    ");
    75 } 
    76      

    运行 代码↓

  • 相关阅读:
    奶酪(NOIP2017 Day2 T1)
    图的遍历(某谷P3916)
    20154331 EXP9web安全基础实践
    20154331 EXP8 web基础
    20154331EXP7 网络欺诈
    20154331 Exp6 信息搜集与漏洞扫描
    Exp5 MSF基础应用
    Exp4 恶意代码分析
    Exp3 免杀原理与实践
    20154331黄芮EXP2 后门原理与实践
  • 原文地址:https://www.cnblogs.com/Ssinoo/p/10940020.html
Copyright © 2011-2022 走看看