进制数转换M

  1 #include<stdio.h>

  2 #include<stdlib.h>

  3 #define Stack_Size 100

  4 #define Stackincrement 10

  5 #define ok 0

  6 #define error -1

  7 #define overflow 2

  8 #define TRUE 1

  9 #define FALSE 0

 10 typedef int status;

 11 typedef struct

 12 {

 13   int *base;

 14   int *top;

 15   int stacksize;

 16 }SqStack;

 17 status InitStack(SqStack &S)

 18 {

 19   S.base=(int *)malloc(Stack_Size*sizeof(int));

 20   if(!S.base)  exit(overflow);

 21   S.top=S.base;

 22   S.stacksize=Stack_Size;

 23   return ok;

 24 }

 25 status StackEmpty(SqStack &S)

 26 {

 27   if(S.top==S.base)

 28   return TRUE;

 29   else return FALSE;

 30 }

 31 status StackLength(SqStack S)

 32 {

 33    return (S.top-S.base);

 34 }

 35 status GetTop(SqStack S,int &e)

 36 {

 37   if(S.top==S.base)  return error;

 38   e=*(S.top-1);

 39   return ok;

 40 }

 41 status Push(SqStack &S,int e)

 42 {

 43   if((S.top-S.base)==S.stacksize)

 44   {

 45     S.base=(int *)realloc(S.base,(S.stacksize+Stackincrement)*sizeof(int));

 46     if(!S.base)  exit(overflow);

 47     S.top=S.base+S.stacksize;

 48     S.stacksize+=Stackincrement;

 49   }

 50   *S.top++=e;

 51   return ok;

 52 }

 53 status Pop(SqStack &S,int &e)

 54 {

 55   if(S.top==S.base)  return error;

 56   e=*--S.top;

 57   return ok;

 58 }

 59 void conversion(int m)

 60 {

 61   SqStack S;

 62   int n,e;

 63   n=m;

 64   InitStack(S);

 65   while(n)

 66   {

 67     Push(S,n%2);

 68     n=n/2;

 69   }

 70   printf("二进制形式为:\n");

 71   while(!StackEmpty(S))

 72   {

 73     Pop(S,e);

 74     printf("%d",e);

 75   }

 76   putchar('\n');

 77   n=m;

 78   InitStack(S);

 79   while(n)

 80   {

 81     Push(S,n%8);

 82     n=n/8;

 83   }

 84   printf("八进制形式为:\n");

 85   while(!StackEmpty(S))

 86   {

 87     Pop(S,e);

 88     printf("%d",e);

 89   }

 90   putchar('\n');

 91   n=m;

 92   InitStack(S);

 93   while(n)

 94   {

 95     e=n%16;

 96     if(e<10)

 97     Push(S,'0'+e);

 98     else

 99     Push(S,'A'+e-10);

100     n=n/16;

101   }

102   printf("十六进制形式为:\n");

103   while(!StackEmpty(S))

104   {

105     Pop(S,e);

106     printf("%c",e);

107   }

108   putchar('\n');

109 }

110   void printSqStack(SqStack S)

111  {

112     int *p;

113     printf("堆栈的元素如下:");

114     printf("\n***************\n");

115     for(p=S.base;p<S.top;p++)

116     printf("%d  ",*p);

117     printf("\n***************\n");

118  }

119  int main()

120  {

121     int i,j,n,e;

122     SqStack S1;

123     InitStack(S1);

124     printf("请输入堆栈长度;\n");

125     scanf("%d",&n);

126     printf("请输入堆栈元素:\n");

127     for(i=0;i<n;i++)

128     {

129         scanf("%d",&e); 

130         Push(S1,e);

131     }

132     printSqStack(S1);

133     printf("请输入要插入的元素;\n");

134     scanf("%d",&e);

135     Push(S1,e);

136     printSqStack(S1);

137     printf("删除S1的栈顶元素:\n");

138     Pop(S1,e);

139     printf("你删除的元素为:%d\n",e); 

140     printSqStack(S1);

141     printf("下面进行十进制二进制十六进制之间的转化:\n");

142     printf("请输入要转化的十进制数:\n");

143     scanf("%d",&n);

144     conversion(n);

145     system("pause");

146     return 0;

147 }

你可能感兴趣的:(转换)