设为首页 加入收藏

TOP

栈应用之将二进制转化为十进制,八进制,十六进制(一)
2017-10-09 13:49:48 】 浏览:839
Tags:应用 二进制 化为 十进制 进制 十六进制
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 
 5 #define INIT_STACK_SZIE 20
 6 #define STACK_INCREMENT 10
 7 #define OK 1
 8 #define ERROR 0
 9 
 10 typedef char Elemtype;  11 typedef int Status;  12 
 13 typedef struct SuqStack{  14     Elemtype* base;  15     Elemtype* top;  16     int stackSize;  17 }SuqStack;  18 
 19 Status InitStack(SuqStack *s){  20     s->base = (Elemtype*)malloc
		    
 
			
n>(sizeof(Elemtype) * INIT_STACK_SZIE); 21 if(!s->base) 22 return ERROR; 23 s->top = s->base; 24 s->stackSize = INIT_STACK_SZIE; 25 return OK; 26 } 27 Status Pop(SuqStack *s,Elemtype *result){ 28 if(s->base == s->top) 29 return ERROR; 30 *result = *(--(s->top)); 31 return OK; 32 } 33 Status Push(SuqStack *s,Elemtype value){ 34 if(s->top - s->base == s->stackSize){ 35 s->base = (Elemtype*)realloc(s->base,sizeof(Elemtype) * (STACK_INCREMENT + INIT_STACK_SZIE)); 36 if(!s->base) 37 return ERROR; 38 s->top = s->base + INIT_STACK_SZIE; 39 s->stackSize = INIT_STACK_SZIE + STACK_INCREMENT; 40 } 41 *(s->top) = value; 42 s->top++; 43 return OK; 44 } 45 int StackLength(SuqStack s){ 46 return s.top - s.base; 47 } 48 Status Binary2Decimal(){ 49 SuqStack s; 50 InitStack(&s); 51 Elemtype c; 52 int i; 53 int sum = 0; 54 int len; 55 printf("please enter binary number end of '#': "); 56 scanf("%c",&c); 57 while(c != '#'){ 58 Push(&s,c); 59 scanf("%c",&c); 60 } 61 getchar(); 62 len = StackLength(s); 63 for(i = 0; i < len; i++){ 64 Pop(&s,&c); 65 sum = sum + (c-48) * pow(2,i); 66 } 67 printf("result is %d(10)\n",sum); 68 return OK; 69 } 70 Status Binary2Octal(){ 71 Elemtype c; 72 SuqStack s1; 73 SuqStack s2; 74 InitStack(&s1); 75 InitStack(&s2); 76 int i,j,k,len,len1,sum; 77 printf("please enter binary number end of '#': "); 78 scanf("%c",&c); 79 while(c != '#'){ 80 Push(&s1,c); 81 scanf("%c",&c); 82 } 83 getchar(); 84 len = StackLength(s1); 85 for(i = 0; i < len; i = i + 3){ 86 sum = 0; 87 for(j = 0,k = i; j < 3 && k < len; j++,k++){ 88 Pop(&s1,&c); 89 sum = sum + (c-48) * pow(2,j); /* 1的ASCII=49 90 0的ASCII=48 91 */ 92 } 93 //printf("%c\n",sum+48); 94 Push(&s2,sum + 48); //sum+48为ASCII码值 95 //栈中的元素的类型char 96 } 97 len1 = StackLength(s2); 98 printf("the result is "); 99 for(i = 0;i < len1; i++){ 100 Pop(&s2,&c); 101 printf("%c",c); 102 } 103 printf("(8)\n"); 104 return OK; 105 } 106 Status Binary2Hexadecimal(){ 107 Elemtype c; 108 SuqStack s1; 109 SuqStack s2; 110 InitStack(&s1); 111 InitStack(&s2); 112 int i,j,k,len,sum,len1; 113 printf("please enter binary number end of '#': "); 114 scanf("%c",&c); 115 while(c != '#'){ 116 Push(&s1,c); 117 scanf("%c",&c); 118 } 119 getchar(); 120 len = StackLength(s1); 121 for(i = 0; i < len; i = i + 4){ 122 sum = 0; 123 for(j = 0,k = i; j < 4 && k < len; j++, k++){ 124 Pop(&s1,&c); 125 sum = sum + (c - 48) * pow(2, j); 126 } 127 /* 0-------48 1-------49 128 * 2-------50 3-------51 129 * 4-------52 5-------53 130 * 6-------54 7-------55 131 * 8-------56 9-------57 132 * A-------65 B-------66 133 * C-------67 D-------68 134 * */ 135 if(sum < 10) 136 Push(&s2,sum + 48); 137 else 138 Push(&s2,sum + 55); 139 } 140 len1 = StackLength(s2); 141 for(i = 0; i < len1; i++){ 142 Pop(&s2,&c); 143 pri
编程开发网
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【翻译】eJabberd 外部脚本的使用 下一篇elixir mix 简介

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }