문제 :

입력 파일 :
[sample.txt
실행 결과 :

소스 코드 :
| 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 | #include <stdio.h> #include <string.h> #include <stdlib.h> char sym[50][10], inst[50][10], op[50][10]; int symcnt, instcnt, opcnt= 0; void get_token_num(char *bp); void main() { FILE *fp; char buf[80]; int num = 0; int i; if ((fp = fopen(“sample.s”, “r”)) == NULL) { fprintf(stderr, “file not found…\n”); exit(1); } while(fgets(buf, sizeof(buf), fp) != NULL) { get_token_num(buf); } fclose(fp); printf(“심볼 10칸씩 출력 :\n\n”); for(i=0; i<symcnt; i++){ if(num<10){ printf("%s\t", sym[i]); num++; } else if(num==10){ printf("\n"); num=0; i -= 1; } } num = 0; printf("\n명령어 10칸씩 출력 :\n\n"); for(i=0; i<instcnt; i++){ if(num<10){ printf("%s\t", inst[i]); num++; } else if(num==10){ printf("\n"); num=0; i -= 1; } } printf("\n\n피연산자 10칸씩 출력 :\n\n"); num = 0; for(i=0; i<opcnt; i++){ if(num<10){ printf("%s\t", op[i]); num++; } else if(num==10){ printf("\n"); num=0; i -= 1; } } printf("\n\n"); } void get_token_num(char *bp){ char *cp; char *test; int n = 0; char* result = strtok(bp," \t\n"); while(result!=NULL) { while(1){ if(n<4){ switch (n){ case 0 : n++; break; case 1 : strcpy(sym[symcnt], result); symcnt++; n++; break; case 2 : strcpy(inst[instcnt], result); instcnt++; n++; break; case 3 : strcpy(op[opcnt], result); opcnt++; n++; break; } } else n=0; break; } result = strtok(NULL,"\t\n"); } } |
설명 :
이해하기가 그다지 어렵지 않고 단순히 Token을 잘라서 출력하면 되기 때문에 따로 주석을 달지는 않았습니다.
질문이 있으시다면 댓글 달아주세요.
비어있는 파트를 공백으로 출력하기 위해 입력 파일의 빈 칸의 첫번째에는 Space를 넣어서 구분했습니다.
공유하기
게시글 관리
코딩은 내일부터 ;
‘💻 개발 & CS > 시스템 & 임베디드’ 카테고리의 다른 글
| 시스템 프로그래밍 프로젝트 #1 (1) | 2020.01.12 |
| 시스템 프로그래밍 프로젝트 #2 (1) | 2020.01.12 |
| 시스템 프로그래밍 프로젝트 #4 (1) | 2019.12.19 |
| 시스템 프로그래밍 프로젝트 #5 (1) | 2019.11.23 |
| 시스템 프로그래밍 프로젝트 #6 (1) | 2019.11.21 |
