์์คํ ํ๋ก๊ทธ๋๋ฐ ํ๋ก์ ํธ #4
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: ...