void code2binary(string code, string binary) { char d[10], comp[100], j[10]; string dcode, ccode, jcode; if (code[0]=='@') { // A 指令: @number || @symbol int address; int match = sscanf(code, "@%d", &address); if (match == 1) decimal2binary(address, binary); else { char symbol[100]; match = sscanf(code, "@%s", symbol); int* addrPtr = lookup(symbol, symTable, symTop); assert(addrPtr != NULL); address = *addrPtr; decimal2binary(address, binary); } } else { // C 指令: d = comp;j if (strchr(code, '=') != NULL) { sscanf(code, "%[^=]=%s", d, comp); dcode = lookup(d, dMap, arraySize(dMap)); ccode = lookup(comp, cMap, arraySize(cMap)); sprintf(binary, "111%s%s000", ccode, dcode); } else { sscanf(code, "%[^;];%s", comp, j); ccode = lookup(comp, cMap, arraySize(cMap)); jcode = lookup(j, jMap, arraySize(jMap)); sprintf(binary, "111%s000%s", ccode, jcode); } } }
/* *** MAIN *** */ int main(void) { char indata[4] = {163,53,128,0}; int sign = 0; char* p_binvalue; for (int i=0; i<4; i++) { printf("dec - indata[%i]: %d\n",i,indata[i]); printf("hex - indata[%i]: %02x\n\n",i,indata[i]); } // check if value is negative if (indata[0] > 127) { sign = 1; printf("Value is negative. %d\n",sign); } // convert decimal to binary for (int j=0; j<4; j++) { p_binvalue = decimal2binary(indata[j]); printf("Binary string of %d is: %s\n",indata[j],p_binvalue); // print out every bits for (int i=0; i<8; i++) { printf("Bit[%d]: %c\n",i,p_binvalue[i]); } } free(p_binvalue); return 0; }
int main1() { int n, m, i, j; int need[501], value[501], bag[100010]; int max; int *choices; freopen("Input.txt", "r", stdin); scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { scanf("%d %d", &need[i], &value[i]); } memset(bag, 0, sizeof(bag)); // brute force search, enumerate all the possible choices, the time complexity is O(2^n). // This approach will lead Time Limit Exceeded in hihoCoder. max = 0; choices = (int *)malloc(n * sizeof(int)); memset(choices, 0, n * sizeof(int)); for (i = 0; i < pow(2.0, n); i++) { decimal2binary(i, choices); int temp_v = 0, temp_n = 0; for (j = 0; j < n; j++) { if (choices[j] == 1) { temp_v += value[j]; temp_n += need[j]; } } if (temp_n <= m && temp_v > max) { max = temp_v; } } printf("%d\n", max); return 0; }
/* *** MAIN *** */ int main(void) { //char indata[4] = {67,7,0,0}; // 135 => 0x43070000 //char indata[4] = {62,32,0,0}; // 0.15625 => 0x3e200000 //char indata[4] = {66,92,0,0}; // 55 => 0x425c0000 //char indata[4] = {63,128,0,0}; // 1 => 0x3f800000 //char indata[4] = {67,160,0,0}; // 320 => 0x43a00000 //char indata[4] = {195,47,0,0}; // -175 => 0xc32f0000 char indata[4] = {193,58,225,72}; // -11.68 => 0xc13ae148 char* p_binvalue; char bin_data[32]; char exponent[8]; char mantissa[23]; float sum; int sign; // convert decimal to binary for (int i=0; i<4; i++) { p_binvalue = decimal2binary(indata[i]); printf("Binary string of %d is: %s\n",indata[i],p_binvalue); // print out every bits for (int j=0; j<8; j++) { bin_data[j+i*8] = p_binvalue[j]; } } printf("\n"); // make a 32 bit vector and print it out for (int k=0; k<32; k++) { printf("%c",*(bin_data+k)); if (k == 7 || k==15 || k==23) { printf("\n"); } } printf("\n"); // check if the number negative or positive (msb) if (*(bin_data) == '1') { printf("msb: %c -> ",*(bin_data)); printf("Negative number\n"); sign = -1; } else { printf("msb: %c -> ",*(bin_data)); printf("Positive number\n"); sign = 1; } // find exponent, 8 bits, and assign its variable printf("Exponent (bin): "); for (int k=1; k<9; k++) { printf("%c",*(bin_data+k)); exponent[k-1] = *(bin_data+k); } printf("\n"); // make sure end of line is the last chararcter exponent[8] = '\n'; // convert the exponent int dec_exponent = bin2dec(BIAS_ON,exponent); printf("Exponent (dec): %d\n",dec_exponent); // find mantissa, 23 bits, and assign its variable printf("Mantissa (bin): "); for (int k=9; k<32; k++) { printf("%c",*(bin_data+k)); mantissa[k-9] = *(bin_data+k); } printf("\n"); // make sure end of line is the last chararcter exponent[23] = '\n'; int dec_mantissa = bin2dec(BIAS_OFF,mantissa); printf("Mantissa (dec): %d\n",dec_mantissa); for (int i=0; i<23; i++) { if (mantissa[i] == '1') { sum += power(i); } } // remember to add implicit leading bit printf("sum: %.10f\n",IMPLICIT_BIT+sum); float x = power_of_2(dec_exponent); printf("x: %f\n",x); printf("------------------------------\n"); float result = sign*(IMPLICIT_BIT+sum) * x; printf("Result: %f\n",result); printf("------------------------------\n"); free(p_binvalue); return 0; }