Problem:
Input File:
[numb.txt
0.00MB](https://blog.kakaocdn.net/dna/EqJyk/btqA57RastK/AAAAAAAAAAAAAAAAAAAAAMzaoxsjOizEuGCR5qF4Fzsa0iAquIjz6f_89H3gPle-/numb.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=wyxmqU%2F2ZPAkscVFgGfrU1oKVrU%3D&attach=1&knm=tfile.txt) Execution Result:
μμ€ Code:
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 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> void get_token_num(char *bp); void hextodec(char *test, int len); // hex to decimal function void atohex(char *ascii, int len); // ascii to hex function int sum = 0; int cnt = 0; int main() { FILE *fp = NULL; char buf[80]; if((fp = fopen("numb.s", "r"))==NULL) { printf("file not found...\n"); exit(1); } while(fgets(buf, sizeof(buf), fp) != NULL) { get_token_num(buf); } fclose(fp); printf("\nsum = %d\n", sum); return 0; } void get_token_num(char *bp){ int i,len; char test[10]; char* result = strtok(bp," \t\n"); // λ¬Έμμ΄ μλ₯΄κΈ° while(result!=NULL) { // μλ₯Έ tokenμ΄ NULL μΌλκΉμ§ loop if(result[0] == 'X'){ // μλ₯Έ token μ μμκ° 'X' μ΄λ©΄ hexadecimal for(i=2; i<=strlen(result)-2; i++){ // μ²λ¦¬ν λΆλΆλ§ μλΌλ test[i-2] = result[i]; cnt++; } printf("token : %s\t",result); printf("\t\t"); hextodec(test, cnt); cnt=0; } else if(result[0] == 'C'){ // μλ₯Έ tokenμ 첫 μμκ° 'C' μ΄λ©΄ ASCII printf("token : %s\t",result); for(i=2; i<=strlen(result)-2; i++){ // μ²λ¦¬ν λΆλΆλ§ μλΌλ test[i-2] = result[i]; cnt++; } test[cnt] = '\0'; atohex(test, cnt); cnt=0; } else { // μλ₯Έ tokenμ΄ λ¬Έμμ΄ μμμ΄λ©΄ decimal printf("token : %s\t",result); sum += atoi(result); printf("\t\tdec : %d\n", atoi(result)); } result = strtok(NULL," \t\n"); } } void hextodec(char *test, int len){ // 16μ§μλ‘ λ λ¬Έμμ΄ int decimal = 0; // 10μ§μλ₯Ό μ μ₯ν λ³μ int i; int position = 0; for (i = len - 1; i >= 0; i--) { // λ¬Έμμ΄μ μμμΌλ‘ λ°λ³΅ char ch = test[i]; // κ° μλ¦Ώμμ ν΄λΉνλ λ¬Έμλ₯Ό μ»μ if (ch >= 48 && ch <= 57) { // λ¬Έμκ° 09μ΄λ©΄(ASCII μ½λ 4857) // λ¬Έμμμ 0μ ν΄λΉνλ ASCII μ½λ κ°μ λΉΌκ³ // 16μ μλ¦Ώμλ₯Ό κ±°λμ κ³±ν κ°μ κ³±ν¨ decimal += (ch - 48) * pow(16, position); } else if (ch >= 65 && ch <= 70) { // λ¬Έμκ° AFμ΄λ©΄(ASCII μ½λ 6570) // λλ¬Έμλ‘ λ 16μ§μμ μ²λ¦¬ // λ¬Έμμμ (Aμ ν΄λΉνλ ASCII μ½λ κ° - 10)μ λΉΌκ³ // 16μ μλ¦Ώμλ₯Ό κ±°λμ κ³±ν κ°μ κ³±ν¨ decimal += (ch - (65 - 10)) * pow(16, position); } else if (ch >= 97 && ch <= 102) { // λ¬Έμκ° afμ΄λ©΄(ASCII μ½λ 97102) // μλ¬Έμλ‘ λ 16μ§μμ μ²λ¦¬ // λ¬Έμμμ (aμ ν΄λΉνλ ASCII μ½λ κ° - 10)μ λΉΌκ³ // 16μ μλ¦Ώμλ₯Ό κ±°λμ κ³±ν κ°μ κ³±ν¨ decimal += (ch - (97 - 10)) * pow(16, position); } position++; } printf("dec : %d\n", decimal); sum += decimal; } void atohex(char *ascii, int len){ char outword[10]; int i, tmp_len; for(i = 0; i<len; i++){ sprintf(outword+i*2, "%02X", ascii[i]); } tmp_len = strlen(outword); printf("hex : %s\t", outword); hextodec(outword, tmp_len); } Explanation:
...