栈与队列应用:二进制转十进制 八进制 十六进制(栈)
1 //将二进制转化为十进制(利用后入先出的特点) 2 //二进制数 1101001 3 /* top 4 1 5 0 6 0 7 1 8 0 9 0 10 1 11 1 base */ 12 13 #include<stdio.h> 14 #include<stdlib.h> 15 #include<math.h> 16 17 #define STACK_INIT_SIZE 20 18 #define STACKINCREMENT 10 19 20 typedef char ElemType; //如果用int则相当于给计算机一个整形 21 22 typedef struct 23 { 24 ElemType *base; 25 ElemType *top; 26 int stackSize; 27 }sqStack; 28 29 void InitStack(sqStack *s) 30 { 31 s->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); 32 if(!s->base) 33 { 34 exit(0); 35 } 36 s->top = s->base; //初始化栈顶等于栈底 37 s->stackSize = STACK_INIT_SIZE; 38 } 39 40 void Push(sqStack *s,ElemType e) 41 { 42 if(s->top - s->base >= s->stackSize)//检查栈是否已经满了 如果满了再申请空间 43 { 44 s->base = (ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(ElemType)); 45 if(!s->base) 46 { 47 exit(0); 48 } 49 } 50 *(s->top) = e; 51 s->top++; 52 } 53 54 void Pop(sqStack *s,ElemType *e)//弹 55 { 56 if(s->top == s->base) 57 { 58 return; 59 } 60 *e = *--(s->top); 61 } 62 63 int StackLen(sqStack s)//测量长度不需要对栈进行修改 所以不需要使用指针 64 {//传进来的是一个结构 65 return (s.top - s.base); 66 } 67 68 int main(void) 69 { 70 ElemType c; 71 sqStack s; 72 73 int len,i,sum = 0; 74 75 InitStack(&s);//初始化 76 77 printf("请输入二进制数,输入#符号表示结束!\n"); 78 scanf("%c",&c); 79 80 while(c != '#') 81 { 82 Push(&s,c); 83 scanf("%c",&c); 84 } 85 86 getchar(); //过滤回撤 87 len = StackLen(s); 88 printf("栈的当前容量是:%d\n",len); 89 90 for(i = 0; i < len; i++) 91 { 92 Pop(&s,&c); 93 sum = sum + (c-48)*pow(2,i); 94 } 95 96 printf("转化为十进制数是:%d\n",sum); 97 return 0; 98 } 99 100 //二进制转化为八进制 101 例如(15)10 = (1111)2 = (17)8 102 #include <stdio.h> 103 #include <stdlib.h> 104 #include <math.h> 105 #define SIZE 20 106 #define STACKINCREMENT 10 107 typedef char ElemType;//如果定义int,一串数字都会一起放入 108 typedef struct 109 { 110 ElemType *base; 111 ElemType *top; 112 int stackSize; 113 }sqStack; 114 void InitStack(sqStack *s) 115 { 116 s->base=(ElemType *)malloc(SIZE*sizeof(ElemType)); 117 if(!s->base) 118 exit(0); 119 s->top=s->base; 120 s->stackSize=SIZE; 121 } 122 void Push(sqStack *s,ElemType e) 123 { 124 if(s->top-s->base==s->stackSize) 125 { 126 s->base=(ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(ElemType)); 127 if(!s->base) 128 exit(0); 129 s->top=s->base+s->stackSize; 130 s->stackSize=s->stackSize+STACKINCREMENT; 131 } 132 *(s->top)=e; 133 s->top++; 134 } 135 int Pop(sqStack *s,ElemType *e) 136 { 137 if(s->top==s->base) 138 return 0; 139 *e=*(--(s->top)); 140 return 1; 141 142 } 143 int StackLen(sqStack s)//测试传数据,修改传指针 144 { 145 return(s.top-s.base); 146 } 147 int main() 148 { 149 ElemType c,ch,chi; 150 sqStack s,q; 151 int len1,len2,i,j,k,sum=0; 152 InitStack(&s); 153 InitStack(&q); 154 printf("请输入二进制数:输入#符号表示结束\n"); 155 scanf("%c",&c); 156 while(c!='#') 157 { 158 Push(&s,c); 159 scanf("%c",&c); 160 } 161 getchar();//吸收回车 162 len1=StackLen(s); 163 for(i=0;i<len1;i=i+3) 164 { 165 for(j=0;j<3;j++) 166 { 167 Pop(&s,&c); 168 sum+=(c-48)*pow(2,j); 169 if( s.base == s.top ) 170 break; 171 } 172 chi=sum+'0'; 173 Push(&q,chi); 174 sum=0;//每次循环结束,sum要清零 175 } 176 len2=StackLen(q); 177 printf("转换后八进制数是:"); 178 for(k=0;k<len2;k++) 179 { 180 Pop(&q,&ch); 181 printf("%c",ch); 182 } 183 return 0; 184 } 185 186 187 188 //二进制转化为十六进制 189 #include <stdio.h> 190 #include <stdlib.h> 191 #include <math.h> 192 #define SIZE 20 193 #define STACKINCREMENT 10 194 typedef char ElemType; 195 typedef struct 196 { 197 ElemType *base; 198 ElemType *top; 199 int stackSize; 200 }sqStack; 201 void InitStack(sqStack *s) 202 { 203 s->base=(ElemType *)malloc(SIZE*sizeof(ElemType)); 204 if(!s->base) 205 exit(0); 206 s->top=s->base; 207 s->stackSize=SIZE; 208 } 209 void Push(sqStack *s,ElemType e) 210 { 211 if(s->top-s->base==s->stackSize) 212 { 213 s->base=(ElemType *)realloc(s->base,(s->stackSize+STACKINCREMENT)*sizeof(ElemType)); 214 if(!s->base) 215 exit(0); 216 s->top=s->base+s->stackSize; 217 s->stackSize=s->stackSize+STACKINCREMENT; 218 } 219 *(s->top)=e; 220 s->top++; 221 } 222 int Pop(sqStack *s,ElemType *e) 223 { 224 if(s->top==s->base) 225 return 0; 226 *e=*(--(s->top)); 227 return 1; 228 229 } 230 int StackLen(sqStack s)//测试传数据,修改传指针 231 { 232 return(s.top-s.base); 233 } 234 int main() 235 { 236 ElemType c,ch,chi; 237 sqStack s,q; 238 int len1,len2,i,j,k,sum=0; 239 InitStack(&s); 240 InitStack(&q); 241 printf("请输入二进制数:输入#符号表示结束\n"); 242 scanf("%c",&c); 243 while(c!='#') 244 { 245 Push(&s,c); 246 scanf("%c",&c); 247 } 248 getchar();//吸收回车 249 len1=StackLen(s); 250 for(i=0;i<len1;i=i+4) 251 { 252 for(j=0;j<4;j++) 253 { 254 Pop(&s,&c); 255 sum+=(c-48)*pow(2,j); 256 if( s.base == s.top ) 257 break; 258 } 259 chi=sum+'0'; 260 switch(sum) 261 { 262 case 10: 263 chi='A'; 264 break; 265 case 11: 266 chi='B'; 267 break; 268 case 12: 269 chi='C'; 270 break; 271 case 13: 272 chi='D'; 273 break; 274 case 14: 275 chi='E'; 276 break; 277 case 15: 278 chi='F'; 279 break; 280 } 281 Push(&q,chi); 282 sum=0;//每次循环结束,sum要清零 283 } 284 len2=StackLen(q); 285 printf("转换后十六进制数是:"); 286 for(k=0;k<len2;k++) 287 { 288 Pop(&q,&ch); 289 printf("%c",ch); 290 } 291 return 0; 292 }