๐Ÿ’ป Dev & CSSystem & Embedded

์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ”„๋กœ์ ํŠธ #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: ...

December 19, 2019 ยท 4 min ยท Sobamemil