๐Ÿ’ป Dev & CSC++ Programming

C++ Programming Ch.2 Exercise 5 Solution

Problem: ํ‚ค๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ๊นŒ์ง€ ๋ฌธ์ž๋“ค์„ ์ฝ๊ณ , ์ž…๋ ฅ๋œ ๋ฌธ์ž 'x'์˜ ๊ฐœ์ˆ˜๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋ผ. Objective & Hints: cin.getline() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํ•œ ์ค„์˜ ๋ฌธ์ž์—ด ์ฝ๊ธฐ Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include using namespace std; int main() { int i,x=0; char a[100]; cout << "๋ฌธ์ž๋“ค์„ ์ž…๋ ฅํ•˜๋ผ(100๊ฐœ ๋ฏธ๋งŒ).\n"; cin.getline(a,100); // ๋ฌธ์ž์—ด ๋‹จ์œ„๋กœ ์ฝ์Œ for(i=0; i<100; i++){ if(a[i]=='x') // a[i]๊ฐ€ 'x'์ด๋ฉด ์นด์šดํŠธ x++; } cout << "x์˜ ๊ฐœ์ˆ˜๋Š” " << x; return 0; } Explanation: ...

February 28, 2020 ยท 1 min ยท Sobamemil
๐Ÿ’ป Dev & CSC++ Programming

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
๐Ÿ’ป Dev & CSC++ Programming

๋ช…ํ’ˆ 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
๐Ÿ’ป Dev & CSC++ Programming

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
๐Ÿ’ป Dev & CSC++ Programming

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
๐Ÿ’ป Dev & CSSystem & Embedded

์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ”„๋กœ์ ํŠธ #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
๐Ÿ’ป Dev & CSSystem & Embedded

์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ”„๋กœ์ ํŠธ #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
๐Ÿ’ป Dev & CSSystem & Embedded

์‹œ์Šคํ…œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ”„๋กœ์ ํŠธ #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
๐Ÿ’ป 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
๐Ÿ’ป Dev & CSC++ Programming

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