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
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(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 printf("%c",c);
144 }
145 printf("\n");
146 }
147 int main(){
148 //Binary2Decimal();
149 //Binary2Octal();
150 Binary2Hexadecimal();
151 return 0;
152 }