Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 4.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 4."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 4

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 2 학습목표 1. 관계형 연산자 익히기 2. 조건문 익히기 : if, if/else, if/else if 3. 중첩 조건문 익히기 4. 논리 연산자 익히기 : AND, OR, NOT 5. switch 문 익히기 6. 분기문 선택 및 사용 방법 익히기 7. 독립적인 소형 프로그램 작성

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 3 LAB ASSIGNMENTS( 실습과제 ) Lesson 4A Lab 4.1: Relational Operators and the if and if/else Statements Lab 4.2: if/else if Statements Lab 4.3: Logical Operators and Nested if Statements Lesson 4B Lab 4.4: The switch Statement Lab 4.5: Branching Errors Lab 4.6: Student-Generated Code Assignments

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 4 Lab 4.1 Relational Operators and the if and if/else Statements http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “compare.cpp” 초기값 ?= 입력 값 ( 같으면 “ 같다 !” 아니면 “ 같지 않다 ” 출력 ) Exercise 1: 매번 다른 입력 값을 사용하여 여러 번 compare.cpp 프로그램을 실행하여 보라. 프로그램이 기대한 대로 실행되는가 ? 잘못된 결과를 보인다면 그 이유를 말해보고, 오류를 찾고 수정하라. http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 5 // This program tests whether or not an initialized value // (num2 = 5) is equal to a value input by the user. // PLACE YOUR NAME HERE. #include using namespace std; int main( ) { int num1, // num1 is not initialized num2 = 5; // num2 is initialized to 5 cout << "Please enter an integer" << endl; cin >> num1; cout << "num1 = " << num1 << " and num2 = " << num2 << endl; if (num1 = num2) cout << "Hey, that's a coincidence!" << endl; if (num1 != num2) cout << "The values are not the same." << endl; return 0; } compare.cpp 오류 ! =  ==

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 6 Exercise 2: compare.cpp 프로그램을 수정하여 비교할 두 값을 사용자가 입력하게 하라. 각 자료 입력 시에 자료 입력 요청을 위한 적절한 메시지를 출력하라. 수정된 프로그램을 검사하기 위해 같은 값, 다른 값의 자료를 입력으로 사용하라. Exercise 3: compare.cpp 프로그램을 수정하여 숫자 값이 같을 경우 다음 행을 출력하도록 하라. The values are the same. Hey that’s a coincidence! Exercise 4: 두 if 문을 하나의 if/else 문으로 변경하라. (Exercise 3 의 결과 사용 )

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 7 Lab 4.2 if/else if Statements http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “grades.cpp” Exercise 1: grades.cpp 프로그램에 대해서 평균값으로 80, 55, 60 각각의 값을 입력하여 실행 해보라. 60 을 입력할 때 출력되는 결과는 기대한 대로 실행되는가 ? 잘못된 결과를 보인다면 올바르게 수정하라. (You Pass 가 출력되게 ) http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 8 // This program prints "You Pass" if a student's average // is 60 or higher and prints "You Fail" otherwise. // PLACE YOUR NAME HERE. #include using namespace std; int main() { double average; // holds the grade average cout << "Input your average: "; cin >> average; if (average > 60) cout << "You Pass" << endl; if (average < 60) cout << "You Fail" << endl; return 0; } grades.cpp

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 9 Exercise 2: 두 개의 if 문 대신에 하나의 if/else 문을 사용하도록 grades.cpp 프로그램을 수정하라. Exercise 3: 다음과 같이 출력되도록 exercise 2 를 수정하라. CaseDisplay ------------------------------------------------- 100 초과  “ 유효하지 않은 자료 ” 90 -100  “Excellent” 80-89  “Good” 60-79  “You Pass” 0-59  “You Fail” 음수  "Illegal value entered” If/else if

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 10 Lab 4.3 Logical Operators and Nested if Statements http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “insurance.cpp” Exercise 1: 다음과 같이 출력되도록 입력 값을 넣고 실행하라. Gender : F Age : 19 Insurance category : 2 Gender : F Age : 23 Insurance category : 4 Gender : M Age : 20 Insurance category : 1 Gender : M Age : 21 Insurance category : 3 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 11 // This determines a client's automobile insurance category based // on gender and age. It illustrates the use of logical operators // and nested if statements. // PLACE YOUR NAME HERE. #include using namespace std; int main() { char gender; int age, category = 0; cout << "Enter your gender (M for male or F for female): "; cin >> gender; cout << "Enter your age: "; cin >> age; insurance.cpp

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 12 if (gender == 'M') { if (age < 21) category = 1; else category = 3; } else if (gender == 'F') { if (age < 21) category = 2; else category = 4; } if (category != 0) cout << "Gender: " << gender << " Age: " << age << " Insurance category: " << category << endl; else cout << "Illegal gender entered" << endl; return 0; }

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 13 Exercise 2: 중첩 if 문을 관계연산자와 if 문으로 수정해보라. if ( (gender == 'M' || gender == 'm') && age < 21) category = 1; else if (gender == 'M' || gender == 'm') // but not < 21 category = 3; else if ( (gender == 'F' || gender == 'f') && age < 21) category = 2; else if (gender == 'F' || gender == 'f') // but not < 21 category = 4;

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 14 Lab 4.4 The switch Statement http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “switch.cpp ” http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab // This program uses a switch statement to create an // appropriate output based on the grade a student earned. // PUT YOUR NAME HERE. #include using namespace std; switch.cpp Exercise 1: switch.cpp 에서 각 case 문에 있는 break 문을 삭제한 다음 실행해 보자. 프로그램 실행 결과는 ?

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 15 int main() { char grade; cout << "What grade did you earn in Programming I?" << endl; cin >> grade; switch( grade ) // The switch statement begins here. { case 'A':cout << "You got an A. Excellent work!" << endl; break; case 'B':cout << "You got a B. Good job." << endl; break; case 'C':cout << "Earning a C is satisfactory." << endl; break; case 'D':cout << "D is passing, but there is a problem." << endl; break; case 'F':cout << "You failed. Better luck next time." << endl; break; default: cout << "You did not enter A, B, C, D, or F." << endl; } return 0; } switch.cpp

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 16 Exercise 2: switch 문을 수정하여 대문자 뿐 아니라 소문자도 처리될 수 있게 하라. case 'a': case 'A': cout << "You got an A. Excellent work!" << endl; break; … Exercise 3: switch 문을 if/else if 문으로 변경하라. ( 논리연산자 “||” 사용 )

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 17 properly block (through braces) instructions working with basic file instructions Exercise 1: payprog.cpp 의 주석되어 있는 부분을 적절한 코드로 채워라. ( 입력 화일명 : overtime.dat) 이 프로그램을 실행하면 어떠한 오류가 발생하나 ? 수정해 보라. 입력 자료 : 30 10  The individual did not work overtime. The gross pay is $300 50 20  The individual worked overtime. The gross pay is $1100 Lab 4.5 Branching Errors http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “payprog.cpp ” http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 18 // This program reads from a file the maximum hours an employee must // work before receiving overtime pay, as well as the overtime rate. // It then has the user enter hours worked and pay rate from the keyboard. // The program uses this information to calculate and display gross pay. // It also prints out a message if gross pay is over $5,000. // PLACE YOUR NAME HERE #include using namespace std; int main() { double maxHours; // Maximum hours worked before overtime is calculated double overRate; // Rate of overtime pay (ex. 1.5 is time and a half) double hoursWorked; double ratePerHour; double grossPay; ifstream getData; payprog.cpp

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 19 // Fill in the code to open getData. The actual data file is called // overtime.dat // Fill in the code to check if getData can be found. This should include // an if statement and a cout statement that executes if the file // could not be found. // Fill in the code to read maxHours and overRate from the file. // Fill in the code to close the data file. cout << "Please input the number of hours worked this week: "; cin >> hoursWorked; cout << "Please input rate per hour: "; cin >> ratePerHour;

20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 20 if (hoursWorked > maxHours) { cout << "This individual worked overtime." << endl; grossPay = (ratePerHour * maxHours) + (ratePerHour * overRate * (hoursWorked - maxHours)); cout << "The gross pay is $" << grossPay << endl; } else cout << "This individual did not work overtime." << endl; grossPay = (ratePerHour * hoursWorked); cout << "The gross pay is $" << grossPay << endl; if (grossPay > 5000); cout << "You earned more than $5,000 this week!!!" << endl; return 0; }

21 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 21 Thanks!!!!!!!!


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 4."

Similar presentations


Ads by Google