◈ 이 글은 강의를 위하여 제가 직접 작성한 내용입니다. 따라서 퍼가실 경우, 출처를 명확히 해주시기 바랍니다!!
◈ This ariticle was written by me for teaching. So if you want to copy this article, please clarify the source!!
◈ This ariticle was written by me for teaching. So if you want to copy this article, please clarify the source!!
switch/case 는 if/else 와 매우 흡사하지만 같지는 않다.
다음 예제를 보면서 그 기능에 대해서 알아보자.
다음 예제를 보면서 그 기능에 대해서 알아보자.
// 오늘의 요일을 입력(숫자로)받고, // 몇일후의 요일을 원하는지도 입력을 받으시오. // 그리고 해당날짜의 요일을 화면에 출력하시오. #includeusing namespace std; enum Dates{SUN, MON, TUE, WED, THU, FRI, SAT}; void main( ) { cout << "오늘의 요일"; cout << "(SUN 0, MON 1, TUE 2, WED 3, THU 4, FRI 5, SAT 6) : "; int today=0; cin >> today; cout << "몇일 후의 요일을 원하십니까? : "; int input=0; cin >> input; int dayofweek = (today+input)%7; if(dayofweek < 0) dayofweek += 7; cout << input << "일후의 요일은 "; switch(dayofweek) { case SUN: cout << "SUNDAY" << endl; break; case MON: cout << "MONDAY" << endl; break; case TUE: cout << "TUESDAY" << endl; break; case WED: cout << "WENDSDAY" << endl; break; case THU: cout << "THURSDAY" << endl; break; case FRI: cout << "FRIDAY" << endl; break; case SAT: cout << "SATURDAY" << endl; break; default: cout << "UNKNOWN DAY" << endl; break; } }