数制转换
十进制数N与其他d进制的转换一个简单的算法是:
N = (N/d)*d + N%d
对应代码:
1 2 3 4 5 6
| int num; while(num){ item = num % d; stack->push(d); num /= d; }
|
最后将栈内的数从栈顶到栈底输出即为转换后的进制数。
NumberConversion.c利用了前面的C封装的顺序栈对象 用线性表表示的顺序栈
实现了输入一个十六进制/十进制/八进制/二进制的正数/负数,输出十六进制/十进制/八进制/二进制对应的数值,达到数制转换的效果。
github源码
NumberConversion.c文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| #include <stdio.h> #include <malloc.h> #include "LinearListStack.h"
int strlen(char *str){ int i = 0; while(*(str+i) != '\0'){ i++; } return i; }
double my_pow(double a,int b){ int s = 0,i; double r = 1; if(b == 0) return 1; if(b<0){ b*=-1; s = 1; } for(i = 0; i < b; i++){ r *= a; } if(s) r = 1/r; return r; }
LinearListStack *intToHex(int num){ LinearListStack *stack = InitLinearListStack(); char item; if(num < 0){ num = -num; }else if(num == 0){ item = 0x30; stack->push(stack,&item); } while(num){ item = num % 16; if(item <= 9){ item += 0x30; }else{ item += 0x37; } stack->push(stack,&item); num /= 16; } return stack; }
LinearListStack *intToDec(int num){ LinearListStack *stack = InitLinearListStack(); char item; if(num < 0){ num = -num; }else if(num == 0){ item = 0x30; stack->push(stack,&item); } while(num){ item = num % 10; item += 0x30; stack->push(stack,&item); num /= 10; } return stack; }
LinearListStack *intToOct(int num){ LinearListStack *stack = InitLinearListStack(); char item; if(num < 0){ num = -num; }else if(num == 0){ item = 0x30; stack->push(stack,&item); } while(num){ item = num % 8; item += 0x30; stack->push(stack,&item); num /= 8; } return stack; }
LinearListStack *intToBin(int num){ LinearListStack *stack = InitLinearListStack(); char item; if(num < 0){ num = -num; }else if(num == 0){ item = 0x30; stack->push(stack,&item); } while(num){ item = num % 2; item += 0x30; stack->push(stack,&item); num /= 2; } return stack; }
void displayConvert(int num){ int negative = 0; LinearListStack *stack = NULL;; negative = num < 0 ? 1 : 0; stack = intToDec(num); negative ? printf("DEC: -") : printf("DEC: "); stack->downPrint(stack); DestroyLinearListStack(stack); stack = intToHex(num); negative ? printf("HEX: -0X") : printf("HEX: 0X"); stack->downPrint(stack); DestroyLinearListStack(stack); if(negative){ stack = intToHex(0XFFFF + num + 1); printf("HEX negative number saved form: 0X"); stack->downPrint(stack); } DestroyLinearListStack(stack); stack = intToOct(num); negative ? printf("OCT: -o") : printf("OCT: o"); stack->downPrint(stack); DestroyLinearListStack(stack); if(negative){ stack = intToOct(0XFFFF + num + 1); printf("OCT negative number saved form: o"); stack->downPrint(stack); } DestroyLinearListStack(stack); stack = intToBin(num); negative ? printf("BIN: -b") : printf("BIN: b"); stack->downPrint(stack); DestroyLinearListStack(stack); if(negative){ stack = intToBin(0XFFFF + num + 1); printf("BIN negative number saved form: b"); stack->downPrint(stack); } DestroyLinearListStack(stack); }
int stringToInt(char *str){ int basenum = 0,i = 0,num = 0; int stack_length = 0; int start_save = 0; int negative = 0; char item; LinearListStack *stack = InitLinearListStack(); while(*str != '\0'){ if(start_save != 1){ if(*str == '-'){ negative = 1; }else if(*str == 'o' || *str == 'O'){ start_save = 1; basenum = 8; }else if(*str == 'b' || *str == 'B'){ start_save = 1; basenum = 2; }else if(*str == 'd' || *str == 'D'){ start_save = 1; basenum = 10; }else if(*str == 'x' || *str == 'X'){ if(start_save == 2){ start_save = 1; basenum = 16; } }else if(*str == '0'){ start_save = 2; } }else if(start_save == 1){ stack->push(stack,str); } str++; } stack_length = stack->length(stack); for(i=0;i<stack_length;i++){ stack->pop(stack,&item); if(item >= 0x30 && item <= 0x39){ item -= 0x30; }else if(item >= 0x41 && item <= 0x46){ if(basenum == 16){ item -= 0x37; }else{ printf("Num string formal error!\n"); DestroyLinearListStack(stack); return 0; } }else if(item >= 0x61 && item <= 0x66){ if(basenum == 16){ item -= 0x57; }else{ printf("Num string formal error!\n"); DestroyLinearListStack(stack); return 0; } }else{ printf("Num string formal error!\n"); DestroyLinearListStack(stack); return 0; } num += my_pow(basenum,i)*item; } DestroyLinearListStack(stack); if(negative) num = -num; return num; }
int main(void) { int num; char str[100]; printf("please enter a num!\n"); printf("num format just like these:\n"); printf("HEX: -0X1F -0x1F 0x1F 0X1F\n"); printf("DEC: -D11 -d11 D11 d11\n"); printf("OCT: -O11 -o11 O11 o11\n"); printf("BIN: -B10 -b10 B10 b10\n"); printf("Enter:\n"); gets(str); printf("\n"); num = stringToInt(str); displayConvert(num); return 0; }
|
编译:
1
| gcc LinearListStack.c LinearListStack.h NumberConversion.c -o NumberConversion
|
运行NumberConversion,显示:
1 2 3 4 5 6 7
| please enter a num! num format just like these: HEX: -0X1F -0x1F 0x1F 0X1F DEC: -D11 -d11 D11 d11 OCT: -O11 -o11 O11 o11 BIN: -B10 -b10 B10 b10 Enter:
|
例1:
输入:d123
输出:
1 2 3 4
| DEC: 123 HEX: 0X7B OCT: o173 BIN: b1111011
|
例2:
输入:-0x2df3
输出:
1 2 3 4 5 6 7
| DEC: -11763 HEX: -0X2DF3 HEX negative number saved form: 0XD20D OCT: -o26763 OCT negative number saved form: o151015 BIN: -b10110111110011 BIN negative number saved form: b1101001000001101
|