| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
                            Tags
                            
                        
                          
                          - 분류
 - LG
 - OpenAI
 - 지도학습
 - LG Aimers
 - Machine Learning
 - supervised learning
 - 티스토리챌린지
 - LLM
 - LG Aimers 4th
 - 딥러닝
 - Classification
 - 해커톤
 - 오블완
 - regression
 - GPT-4
 - gpt
 - PCA
 - ChatGPT
 - 회귀
 - AI
 - 머신러닝
 - deep learning
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
SYDev
[객체지향프로그래밍] 5주차 정리 본문
경희대학교 컴퓨터공학부 이대호 교수님의 객체지향프로그래밍 수업 자료를 참고한 정리본
while(cin)
#include <iostream>
#include <limits>
using namespace std;
int main(void) {
    cout << boolalpha << (bool)cin << endl;
    int input, sum = 0;
    cout << "Enter numbers to sum, type = 'q' to end the list: ";
    while(cin >> input) {   //왜 실수 입력하면 한 번 더 돌아가는지?
        cout << boolalpha << (bool)cin << endl;
        sum += input;
    }
    
    cout << "sum = " << sum << endl;
    cout << boolalpha << (bool)cin << endl;
    cin.clear();    //cin의 error state를 clear
    cout << boolalpha << (bool)cin << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');    //키보드 버퍼를 비운다.
    
    cin >> input;
    cout << input << endl;
    return 0;
}
-> 실수를 넣었을 때, cin에서 .을 인식하기 전까지는 정수이기 때문에 그것을 인식하지 못하고 while문을 돌다가 '.'을 만나고 while문을 멈춤 -> 그냥 잘못된 input이라 그럼 -> 컴파일러마다 스탠다드가 다르기 때문에 신경 X
EoF(End of File)
- cin.eof(): 파일의 끝 혹은 종료를 의미
 - Max/Unix - Control + d
 
#include <iostream>
using namespace std;
int main(){
    int a, b;
    
    while(1){
        cin >> a >> b;
        if(cin.eof()) break;
        else cout << a+b << endl;
    }
    return 0;
}
-> ctrl + d 입력시 프로그램 종료
#include <iostream>
using namespace std;
int main(){
    while (true) {
        int n;
        cin >> n;
        
        if(cin.eof()) {
            cin.clear();
            cout << "***1" << endl;
        }
        else if (cin.bad()) {   //입출력 장치에 문제가 있는 경우, file로 입출력할 때
            cout << "***2" << endl;
        }
        else if (cin.fail()) {  //잘못 입력
            cin.clear();    //unset failbit
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            //clear, ignore했기 때문에 다시 입력을 받을 수 있음
            cout << "***3" << endl;
        }
        else {
            cout << "->" << n << endl;
        }
    }
    
    return 0;
}
Abnormal Loop Termination
goto
#include <iostream>
using namespace std;
int main(){
    int count = 1;
top:
    if (count > 5) {
        goto end;
    }
    cout << count << endl;
    count++;
    goto top;
end:
    return 0;
}
-> goto 뒤에 있는 label로 이동
Iterations Examples
tree
#include <iostream>
using namespace std;
int main(){
    int height;
    cout << "Enter height of tree: ";
    cin >> height;
    
    for(int i = 0; i < height; i++) {
        for(int j = 0; j <= height - i - 1; j++) {  //마지막 줄 공백 하나 ㅗ함
            cout << ' ';
        }
        
        for(int k = 0; k < (i * 2 + 1); k++) {
            cout << '*';
        }
        cout << endl;
    }
    
    return 0;
}
Do/While
#include <iostream>
using namespace std;
int main(){
    int in_value;
    cout << "Please enter an integer in ter range 0-10: ";
    do {
        cin >> in_value;
    } while (in_value < 0 || in_value > 10);
    cout << " Legal value entered was " << in_value << endl;
    
    return 0;
}
-> do/while문을 활용함으로써, in_value의 초기값을 설정하지 않고 내부에서 cin이 가능해짐
For문
- for문 안에서 continue를 만나면 i++로 갔다가 비교문으로 이동
 
#include <iostream>
using namespace std;
int main(void) {
	int i, j, k;
    for(i = 0, j = 0; i < 10; i++, j+= 2) {	//루프 초기화 부분의 comma는 분리자, 루프 업데이트 부분의 comma는 연산자
    
    }
    
    //k = (i = 10, j = 20);
    i = 10;
    j = 20;
    k = j;
    
    cout << k << endl;
    
    return 0;
}
-> ','도 연산자 취급
-> 좌측부터 순서대로 표현식 평가
-> k는 20
for( ; ; ) {
	...
}
-> while(true)와 같음
참고자료
[C++] EOF(End Of File) 처리하기
EOF란? End Of File의 약자로, 파일의 끝 또는 종료를 의미한다. 흔히 소스코드를 빌드 후 프롬프트 창에서 실행한 것 또한 파일의 실행이므로, 프로그램 종료 또는 무한루프 탈출 조건으로 EOF를 쓰
heestory0324.tistory.com
[C++] Comma Operator | 콤마 연산자
Comma Operator 콤마 연산자 - 콤마 연산자는 두 개의 표현식을 하나로 결합하는 연산자이다. - 문법 규칙 상, 하나의 표현식만을 허용하는 자리에 콤마 연산자를 사용하면 두 개의 표현식을 동시에
dad-rock.tistory.com
728x90
    
    
  반응형
    
    
    
  '3학년 1학기 전공 > 객체지향 프로그래밍' 카테고리의 다른 글
| [객체지향프로그래밍] 9주차 정리 (0) | 2024.05.06 | 
|---|---|
| [객체지향프로그래밍] 8주차 정리 (0) | 2024.04.30 | 
| [객체지향프로그래밍] 4주차 정리 (0) | 2024.03.27 | 
| [객체지향프로그래밍] 3주차 정리 (0) | 2024.03.19 | 
| [객체지향프로그래밍] 2주차 정리 (1) | 2024.03.12 |