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      

    运行 代码↓

  • 相关阅读:
    在MyEclipse中设置Source folders和output folder
    在Myeclipse中设置源码和webroot目录
    将svn下载的工程转化为web工程
    位运算
    maxSequence
    krusual C++
    Dijkstra And Floyd C++
    Graph And Trave
    Set
    Tree
  • 原文地址:https://www.cnblogs.com/Ssinoo/p/10940020.html
Copyright © 2011-2022 走看看