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

문제 : 다음과 같이 "yes"가 입력될 때까지 종료하지 않는 프로그램을 작성하라. 사용자로부터의 입력은 cin.getline() 함수를 사용하라. 목적 및 힌트 : 공백을 포함하는 문자열 읽기 실행 결과 : 코드 : 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; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : 문자열을 두 개 입력받고 두 개의 문자열이 같은지 검사하는 프로그램을 작성하라. 만일 같으면 "같습니다", 아니면 "같지 않습니다"를 출력하라. 목적 및 힌트 : 공백 없이 입력된 문자열 읽기 (빈칸 없이 입력) 실행 결과 : 코드 : 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; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : 키가 입력될 때까지 문자들을 읽고, 입력된 문자 'x'의 개수를 화면에 출력하라. 목적 및 힌트 : cin.getline() 함수를 이용해 한 줄의 문자열 읽기 실행 결과 : 코드 : 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; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : 소수점을 가지는 5개의 실수를 입력 받아 제일 큰 수를 화면에 출력하라. 목적 및 힌트 : cin 활용, 키보드로부터 실수 읽기 실행 결과 : 코드 : 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; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : 키보드로부터 두 개의 정수를 읽어 큰 수를 화면에 출력하라. 목적 및 힌트 : cin 활용, 키보드로부터 정수 읽기 실행 결과 : 코드 : 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; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : cout과 << 연산자를 이용하여 다음과 같이 구구단을 출력하는 프로그램을 작성하라. 목적 및 힌트 : cout 활용, 화면 출력 실행 결과 : 코드 : 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; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : cout과 << 연산자를 이용하여, 1에서 100까지 정수를 다음과 같이 한 줄에 10개씩 출력하라. 각 정수는 탭으로 분리하여 출력하라. 실행 결과 : 목적 및 힌트 : cout 활용, 화면 출력 코드 : 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; } 설명 : ...

2020년 2월 28일 · 1 분 · Sobamemil

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

문제 : 입력 파일 : [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) 실행 결과 : 소스 코드 : 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); } 설명 : ...

2020년 1월 12일 · 1 분 · Sobamemil

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

문제 : 입력 파일 : [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) 실행 결과 : 소스 코드 : 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; } 설명 : ...

2020년 1월 12일 · 2 분 · Sobamemil

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

문제 : 입력 파일 : [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) 실행 결과 : 소스 코드 : 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"); } } 설명 : ...

2020년 1월 12일 · 2 분 · Sobamemil