C++ Programming Ch.2 Exercise 8 Solution

Problem: 한 라인에 ';' 으로 5개의 이름을 구분하여 입력받아, 각 이름을 끊어내어 화면에 출력하고 가장 긴 이름을 판별하라. Objective & Hints: cin.getline()으로 문자열 읽기 ';'까지 문자열을 읽고자 하면 다음 코드를 사용하고 1 2 char name[100]; cin.getline(name, 100, ';'); 5개 읽어야 하니 5번 루프를 돈다. 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 #include #include using namespace std; int main() { int A=0; int i; char name[100]; char longName[100]; cout << "5 명의 이름을 ';' 으로 구분하여 입력하세요 \n>>"; for(i=1;i<6;i++){ cin.getline(name,100,';'); cout << i << " : " << name << endl; if(A < strlen(name)) { A = strlen(name); strcpy(longName,name); } } cout << "가장 긴 이름은 " << longName; return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

C++ Programming Ch.2 Exercise 7 Solution

Problem: 다음과 같이 "yes"가 입력될 때까지 종료하지 않는 프로그램을 작성하라. 사용자로부터의 입력은 cin.getline() 함수를 사용하라. Objective & Hints: 공백을 포함하는 문자열 읽기 Execution Result: Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include #include using namespace std; int main() { char A[] = "yes"; // 배열 선언시 "yes"로 초기화 char B[100]; while(true){ cout << "종료하고 싶으면 yes를 입력하세요>>"; cin.getline(B,100); if(strcmp(A,B) == 0) break; } cout << "종료합니다..."; return 0; } Explanation: ...

February 28, 2020 · 1 min · Sobamemil

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