C++ Programming Ch.2 Exercise 6 Solution

Problem: ๋ฌธ์ž์—ด์„ ๋‘ ๊ฐœ ์ž…๋ ฅ๋ฐ›๊ณ  ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ๊ฐ™์€์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ. ๋งŒ์ผ ๊ฐ™์œผ๋ฉด "๊ฐ™์Šต๋‹ˆ๋‹ค", ์•„๋‹ˆ๋ฉด "๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค"๋ฅผ ์ถœ๋ ฅํ•˜๋ผ. Objective & Hints: ๊ณต๋ฐฑ ์—†์ด ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด ์ฝ๊ธฐ (๋นˆ์นธ ์—†์ด ์ž…๋ ฅ) Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include #include using namespace std; int main() { char pw1[100]; char pw2[100]; cout << "์ƒˆ ์•”ํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>"; cin.getline(pw1,100); cout << "์ƒˆ ์•”ํ˜ธ๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ž…๋ ฅํ•˜์„ธ์š”>>"; cin.getline(pw2,100); if(strcmp(pw1,pw2)==0) cout << "๊ฐ™์Šต๋‹ˆ๋‹ค"; else cout << "๊ฐ™์ง€ ์•Š์Šต๋‹ˆ๋‹ค"; return 0; } Explanation: ...

February 28, 2020 ยท 1 min ยท Sobamemil

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

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