Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 2 학습목표 1. C++ 프로그램 기본 구성요소 익히기 2. 기억장소 사용에 대한 기본 지식 습득 3. 변수와 이름 상수 사용방법 익히기 4. 기본 자료타입 이해하기 : a. integer b. character c. floating-point d. Boolean e. string 5. 배정 연산자 사용하기 6. 출력을 위한 cout << 문 사용하기 7. 산술 연산자 사용하기 8. 단순 명령어를 사용한 소형 프로그램 개발 9. 스트링 (string) 클래스 사용하기
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 3 LAB ASSIGNMENTS Lesson 2A Lab 2.1: Working with the cout Statement Lab 2.2: Working with Constants, Variables, and Arithmetic Operators Lesson 2B Lab 2.3 Rectangle Area and Perimeter Lab 2.4 Working with Characters and Strings
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 4 Lab 2.1 Working with the cout Statement Exercise 1: 에서 download name.cpp 다음 사항을 수행하도록 프로그램 코드를 완성하라. - 하나의 행에 이름과 성을 출력하라 - 다음 행에 주소를 출력하라. (endl 사용 ) - 다음 행에 자신의 전화번호를 출력하라. * 문자열 출력을 위해 “hello” 와 같이 리트럴 사용 - 컴파일 후 실행 !!! 예 ) Deano Beano 123 Markadella Lane Fruitland, Md http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 5 // This program will write the name, address and telephone // number of the programmer. #include using namespace std; int main() { // Fill in this space to write your first and last name. // Fill in this space to write your address. // Fill in this space to write your city, state and zip. // Fill in this space to write your telephone number. return 0; } name.cpp
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 6 Exercise 2: 전화번호와 주소 사이에 빈 행을 세 개를 넣어 출력하도록 수정하라. Exercise 3: 다음과 같이 출력되도록 수정하라. ************ Programmer: Deano Beano 123 Markadella Lane Fruitland, Md Telephone: ************
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 7 Lab 2.2 Working with Constants, Variables, and Arithmetic Operators Exercise 1: 에서 download circlearea.cpp // This Program will output the circumference and area // of the circle with a given radius. // PLACE YOUR NAME HERE. #include using namespace std; const double PI = 3.14; const double RADIUS = 5.4; circlearea.cpp
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 8 int main() { _____ area; // definition of area double circumference; // definition of circumference circumference = 2 * PI * RADIUS; // computes circumference area = _____________; // computes area // Write a cout statement that will output (with description) // the circumference of the circle. // Write a cout statement that will output (with description) // the area of the circle. return 0; }
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 9 Exercise 2: 다음과 같은 출력을 되게 빈칸을 채우고 cout 문장을 작성하라. The circumference of the circle is The area of the circle is Exercise 3: circumference 변수의 자료형을 double 에서 int 로 변경하라. 프로그램을 실행하고 결과를 기록하라. The circumference of the circle is ___________ The area of the circle is ___________ 위 결과에 대해 설명하시오.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Lab 2.3 Rectangle Area and Perimeter Exercise 1: Lab 2.2 를 참고하여 직사각형의 면적과 둘레를 계산하는 프로그램을 작성하라. LENGTH = 8, WIDTH = 3 으로 상수로 사용 Exercise 2: 컴파일하고 프로그램 실행, 다음 출력 값을 확인 ? The area of the rectangle is 24 The perimeter of the rectangle is 22
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Lab 2.4: Working with Characters and Strings Exercise 1: 에서 download stringchar.cpp char one character string sequence of charactershttp://ce.kyungil.ac.kr/~kykim/OOP2009/Lab // This program demonstrates the use of characters and strings. // PLACE YOUR NAME HERE. #include using namespace std; // definition of constants const string FAVORITESODA = "Brand X"; // strings get double quotes const char BESTRATING = 'A'; // characters get single quotes stringchar.cpp
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide int main() { char rating2; // 2nd highest product rating string favoriteSnack; // most preferred snack int numberOfPeople; // number of people in the survey int topChoiceTotal; // number of people who prefer the top choice // Write lines of code to do the following: // Assign the value of "crackers" to favoriteSnack. // Assign a grade of 'B' to rating2. // Assign the number 250 to numberOfPeople. // Assign the number 148 to topChoiceTotal. // Fill in each of the following blanks with the name of the // appropriate variable or named constant. cout << "The number one soda is " << ____________ << endl; cout << "The number one snack is " << ____________ << endl; cout << "Out of " << _____________ << " people " << _________ << " chose these items!" << endl; cout << "Each of these products received a rating of " << _______; cout << " from our expert tasters." << endl; cout << "The other products were rated no higher than a " << _________ << endl; } stringchar.cpp 수정
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Exercise 2: stringchar.cpp 에서 지시된 사항 ( 주석 부분, 밑줄 부분 ) 을 완성하고, 컴파일한 다음 오류가 없으면 실행하라. ( 오류가 있으면 수정하라 ) The number one soda is Brand X The number one snack is crackers Out of 250 people 148 chose these items! Each of these products received a rating of A from our expert tasters. The other products were rated no higher than a B Exercise 3: 프로그램의 main 모듈 내에서 FAVORITESODA 값을 변경할 수 있나 ? 이유는 ? Exercise 4: 프로그램의 main 모듈 내에서 favoriteSnack 의 선택을 변경할 수 있나 ? 이유는 ?