C++ Programming Ch.2 Exercise 4 Solution

Problem: 소수점을 가지는 5개의 실수를 입력 받아 제일 큰 수를 화면에 출력하라. Objective & Hints: cin 활용, 키보드로부터 실수 읽기 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include using namespace std; int main() { float a[5], big; int i; cout << "5 개의 실수를 입력하라>>"; for(i=0;i<5;i++) // 5개의 실수 입력 받기 cin >> a[i]; big = a[0]; // a의 첫번째 원소를 big에 삽입 for(i=1;i<5;i++) if(big<a[i]) //a[i]가 big보다 크면 big에 삽입 big=a[i]; cout << "제일 큰 수 = " << big; return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

명품 C++ programming 실습문제 2장 3번

Problem: 키보드로부터 두 개의 정수를 읽어 큰 수를 화면에 출력하라. Objective & Hints: cin 활용, 키보드로부터 정수 읽기 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include using namespace std; int main(){ int a,b; cout << "두 수를 입력하라>>"; cin >> a >> b; // cin 연산자를 이용하여 두개의 정수 입력받기 cout << "큰 수 = "; if(a<b) cout << b; else cout << a; return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

C++ Programming Ch.2 Exercise 2 Solution

Problem: cout과 << 연산자를 이용하여 다음과 같이 구구단을 출력하는 프로그램을 작성하라. Objective & Hints: cout 활용, 화면 출력 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include using namespace std; int main() { int i, j; for(i=1; i<10; i++){ for(j=1; j<10; j++) { cout << j << "x" << i << "=" << j*i << '\t'; if(j==9) // 9단 출력 후 줄바꿈. cout << endl; } } return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

C++ Programming Ch.2 Exercise 1 Solution

Problem: cout과 << 연산자를 이용하여, 1에서 100까지 정수를 다음과 같이 한 줄에 10개씩 출력하라. 각 정수는 탭으로 분리하여 출력하라. Execution Result: Objective & Hints: cout 활용, 화면 출력 Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include using namespace std; int main(){ int i; for(i=1;i<101;i++){ cout << i << "\t"; if(i%10==0) // 10개마다 개행문자 출력 cout << endl; } return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

시스템 프로그래밍 프로젝트 #1

Problem: Input File: [sample.txt 0.00MB](https://blog.kakaocdn.net/dna/WEEud/btqA6oyqdRE/AAAAAAAAAAAAAAAAAAAAAF6o8u7LK5SB9AIdNSz_-IRJgLPtbZjLxGCM5EIoOvqI/sample.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=AakSu1DOyqICwWZkF6%2F%2FvvgPYME%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 #include <stdio.h> #include <string.h> #include <stdlib.h> void main() { FILE *fp; char buf[80]; int n = 0; if ((fp = fopen("sample.s", "r")) == NULL) { fprintf(stderr, "file not found...\n"); exit(1); } while(fgets(buf, sizeof(buf), fp) != NULL) { n += get_token_num(buf); } fclose(fp); printf("Number of token = %d\n", n); } int get_token_num(char *bp) { char *cp; int n = 0; for(cp = strtok(bp, " \t\n"); cp != NULL; ) { n++; cp = strtok(NULL, " \t\n"); } return(n); } Explanation: ...

January 12, 2020 · 1 min · Sobamemil

시스템 프로그래밍 프로젝트 #2

Problem: Input File: [sample.txt 0.00MB](https://blog.kakaocdn.net/dna/I5Kf9/btqA7q3zbiX/AAAAAAAAAAAAAAAAAAAAANrvSUHPXY9pb861NzszU1ZSjedb5bh_Z-XW6gY7gY7Q/sample.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=Z3BB1PLUPoQ6gz717fxkZL1UeNg%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 #include <stdio.h> #include <string.h> #include <stdlib.h> void get_token_num(char *bp, int *num, int *alpha, int *other) { char *cp; int i, count1, count2; for(cp = strtok(bp, " \t\n"); cp != NULL;){ if('A' <= cp[0] && 'Z' >= cp[0]) count1 = -1; else if('0' <= cp[0] && '9' >= cp[0]) count1 = 0; else{ count1 = 1; count2 = 1; break; } int len = strlen(cp); for(i=0; i<len; i++){ if ( 'A' <= cp[i] && 'Z' >= cp[i]) count2 = -1; else if ( '0' <= cp[i] && '9' >= cp[i]) count2 = 0; else count2 = 1; if(count2 != count1){ count2 = 1; break; } } switch(count2) { case -1 : *alpha += 1; break; case 0 : *num += 1; break; case 1 : *other += 1; break; default : fprintf(stderr,"error...\n"); exit(2); } cp = strtok(NULL," \t\n"); } } int main(){ FILE *fp; char buf[80]; int num = 0, alpha=0, other=0; 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, &num, &alpha, &other); } fclose(fp); printf("Number = %d\n", num); printf("Alphabet = %d\n", alpha); printf("Other = %d\n", other); return 0; } Explanation: ...

January 12, 2020 · 2 min · Sobamemil

시스템 프로그래밍 프로젝트 #3

Problem: Input File: [sample.txt 0.00MB](https://blog.kakaocdn.net/dna/45Fiq/btqA5PcbiZG/AAAAAAAAAAAAAAAAAAAAAKYSkI0QPrzP-9mNerqdA2M0Tul-f5_nfn5HKLp50AXV/sample.txt?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1788188399&allow_ip=&allow_referer=&signature=zgVylyi4wQaArIqnlrxSbRC1hSk%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 #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"); } } Explanation: ...

January 12, 2020 · 2 min · Sobamemil

시스템 프로그래밍 프로젝트 #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

C++ Programming Ch.9 Exercise 10 Solution

Problem: 간단한 그래픽 편집기를 콘솔 바탕으로 만들어보자. 그래픽 편집기의 기능은 "삽입", "삭제", "모두보기", "종료" 의 4가지이고, 실행 과정은 다음과 같다. Objective & Hints: 추상 클래스, 상속 종합 응용 Shape과 이를 상속받은 Circle, Line,Rect 클래스는 [그림9-13]을 이용하고 필요한 클래스와 main() 함수를 작성하라. 전체 프로그램은 대략 아래와 같이 구성된다. 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 #include #include<stdlib.h> using namespace std; class UI { public: static int show_menu() { int num; cout << "삽입:1, 삭제:2, 모두보기:3, 종료:4 >>" ; cin >> num; return num; } static int input_shape() { int num; cout << "선:1, 원:2, 사각형:3 >> "; cin >> num; return num; } static int del_index() { int num; cout << "삭제하고자 하는 도형의 인덱스 >> "; cin >> num; return num; } }; class Shape { // Node Shape* next; protected: virtual void draw() { cout << "--Shape--" << endl; } public: Shape() { next = NULL; } virtual ~Shape() {} Shape* add(Shape* p) { this->next = p; return p; } Shape* getNext() { return next; } void paint() { draw(); } void setNext(Shape *p) { this->next = p->next; } }; class Line : public Shape { public: virtual void draw() { cout << "Line" << endl; } }; class Circle : public Shape { public: virtual void draw() { cout << "Circle" << endl; } }; class Rect : public Shape { public: virtual void draw() { cout << "Rectangle" << endl; } }; class GraphicEditor { // List int node_size; Shape* pStart; Shape* pLast; public: GraphicEditor() { pStart = NULL; node_size = 0; } int run() { cout << "그래픽 에디터입니다.\n"; while(true){ int num; num = UI::show_menu(); switch (num){ case 1:{ num = UI::input_shape(); input_new(num); break; } case 2:{ if(pStart == NULL){ cout << "List Empty\n"; break; } num = UI::del_index(); del(num); break; } case 3:{ show(); break; } case 4:{ exit(0); } default : cout << "메뉴를 잘못 선택하셨습니다.\n"; } } } void input_new(int n) { switch (n){ case 1: { if(node_size == 0) { pStart = new Line(); pLast = pStart; } else pLast = pLast->add(new Line()); node_size++; break; } case 2: { if(node_size == 0){ pStart = new Circle(); pLast = pStart; } else pLast = pLast->add(new Circle()); node_size++; break; } case 3: { if(node_size == 0){ pStart = new Rect(); pLast = pStart; } else pLast = pLast->add(new Rect()); node_size++; break; } default : cout << "메뉴를 잘못 선택하셨습니다.\n"; } } bool del(int n) { int k=0; Shape* target_node = pStart; Shape* priv_node; if(n == 0){ pStart = pStart->getNext(); delete target_node; } else{ while( (target_node != NULL) && (k < n)){ priv_node = target_node; target_node = target_node->getNext(); k++; } if(target_node == NULL){ cout << "없는 노드입니다.\n"; return false; } else { priv_node->setNext(target_node); delete target_node; } } node_size--; } void show() { Shape* p = pStart; int i=0; if(p == NULL) cout << "List Empty\n"; else while(p != NULL){ cout << i << ": "; p->paint(); p = p->getNext(); i++; } } }; int main() { GraphicEditor* g_editor = new GraphicEditor; g_editor->run(); delete g_editor; } Explanation: ...

November 26, 2019 · 4 min · Sobamemil

C++ Programming Ch.9 Exercise 9 Solution

Problem: 다음 그림과 같은 상속 구조를 갖는 클래스를 설계한다. 모든 프린터는 모델명(model), 제조사(manufacturer), 인쇄 매수(printedCount), 인쇄 종이 잔량(availableCount)을 나타내는 정보를 가진다. print(int pages) 함수와 show() 함수는 가상 함수로 구현하라. print(int pages)는 pages 만큼 프린트하는 함수이고, show() 함수는 현재 프린트의 모델, 제조사, 인쇄 매수, 인쇄 종이 잔량 등을 출력하는 함수이다. 잉크젯 프린터는 잉크 잔량(availableInk) 정보를 추가적으로 가지며, 레이저 프린터는 토너 잔량(availableToner) 정보를 추가적으로 가진다. 이들의 print(int pages) 멤버 함수는 프린터 타입에 맞게 구현하라. 각 클래스를 설계 구현하고 다음과 같이 실행되도록 전체 프로그램을 완성하라. InkJetPrinter 객체와 LaserPrinter 객체를 각각 하나만 동적으로 생성하여 시작한다. ...

November 26, 2019 · 3 min · Sobamemil