Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 2 학습목표 1. 표준입력을 위한 cin >> 익히기 2. 형식제어 표준출력 (formatted output) 사용하기 3. 추가 산술 연산자, 수학적 함수 사용하기 l 4. 자료형 변환 사용 방법과 사용 시기 익히기 ( 형의 강제 변환, 캐스팅 ) 5. 파일의 개념과 사용방법 소개

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 3 LAB ASSIGNMENTS( 실습과제 ) Lesson 3A Lab 3.1: Working with the cin Statement Lab 3.2: Formatting Output Lab 3.3: Arithmetic Operations and Math Functions Lesson 3B Lab 3.4: Working with Type Casting Lab 3.5: Reading and Writing to a File

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 4 Lab 3.1 Working with the cin Statement Exercise 1: http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download bill.cpp 다음 사항을 만족하는 프로그램을 완성하라. o 구입한 항목 수  22 o 각 항목의 가격  10.98 Please input the number of items bought 22 Please input the price of each item 10.98 The total bill is $241.56http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 5 // This program will read in the quantity of a particular item and its // price. It will then print out the total price. The input will come // from the keyboard and the output will go to the screen. //PLACE YOUR NAME HERE. #include using namespace std; int main() { int quantity; // number of items purchased double itemPrice; // price of each item double totalBill; // total price of all items cout << setprecision(2) << fixed << showpoint; // formatted output bill.cpp

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 6 cout << "Input the number of items bought. "; // Write an input statement that brings in the quantity. // Write a prompt to ask for the price. // Write an input statement that brings in the price of each item. // Write an assignment statement that places the correct value in totalBill. // Write an output statement that prints totalBill with a label to the screen. return 0; } bill.cpp

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 7 Exercise 2: bill.cpp 파일의 cout << setprecision(2) << fixed << showpoint; // formatted output  다음과 같이 변경하고 cout << setprecision(2) << showpoint; // formatted output 연습문제 1 과 같은 자료를 사용하여 실행하라. 출력결과는 ? Exercise 3: fixed 속성을 원래대로 돌려놓고, 소수점 이하 4 째 자리까지 출력하도록 수정하라. 연습문제 1 과 같은 자료를 사용하여 실행하라. 출력결과는 ? Exercise 4: 상품의 이름을 먼저 물은 다음, 항목의 개수와 가격을 입력하게 프로그램을 수정하라. 파일을 include 할 것 !!! 예 1) Please input the name of item: Milk 예 2) Please input the name of item: Chocolate Ice Cream $241.5600

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 8 Lab 3.2 Formatting Output 1234567890 PRICExxxxQUANTITY xxxxxxxxxx 1.95xxxxxxxx8 xxxxxxxxxx 10.89xxxxxxxx9 가격 : 15-width space 내에 오른쪽 정렬  제목 : setw(15) 개수 : 12-width space  제목 : setw(12), 값 : setw(9)

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 9 Exercise 1: http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download tabledata.cpp 다음 실행 결과와 같이 동작하도록 tabledata.cpp 을 확장하라. Enter 키를 입력하기 전에 가격과 개수를 한 줄에 입력하도록 하라.(space 에 의해 구분 )http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab Input the price and quantity of the first item 1.95 8 Input the price and quantity of the second item 10.89 9 PRICE QUANTITY 1.95 8 10.89 9

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 10 // This program will bring in two prices and two item quantities // from the keyboard and print those numbers in a formatted chart. // PLACE YOUR NAME HERE. #include #include ___________ // Include the library needed for formatted output. using namespace std; int main() { double price1, price2; // Price of 2 items int quantity1, quantity2; // Quantity of 2 items cout << setprecision(2) << fixed << showpoint; cout << "Input the price and quantity of the first item" << endl; // Write an input statement that reads in price1 and quantity1 from the keyboard. // Provide a prompt for the second price and quantity. // Write an input statement that reads in price2 and quantity2 from the keyboard. cout << setw(15) << "PRICE" << setw(12) << "QUANTITY" << endl << endl; // Write an output statement that prints the first price // and quantity. Be sure to use setw() statements. // Write an output statement that prints the second price and quantity. return 0; } tabledata.cpp

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 11 Lab 3.3 Arithmetic Operations and Math Functions http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download righttrig.cpp http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab a b hyp = ??? hyp = sqrt(pow(a,2) + pow(b,2));

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 12 // This program will input the value of two sides of a triangle and then // determine the length of the third side in order to make it a right triangle. // PUT YOUR NAME HERE. #include #include // needed for math functions like sqrt() using namespace std; int main() { double a, b; // the smaller two sides of the triangle double hyp; // the hypotenuse (side 3) calculated by the program cout << "Please input the value of the two sides." << endl; cin >> a >> b; // Write a statement to calculate the hypotenuse and assign it to hyp. cout << "The sides of the right triangle are " << a << " and " << b << endl; cout << "The hypotenuse is " << hyp << endl; return 0; } righttrig.cpp

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 13 Exercise 1: 다음과 같이 동작하도록 righttrig.cpp 에서 빠진 부분을 완성하라. Please input the value of the two sides 9 3 The sides of the right triangle are 9 and 3 The hypotenuse is 9.48683 Exercise 2: 다음과 같이 출력되도록 righttrig.cpp 을 수정하라. Please input the value of the two sides 9 3 The sides of the right triangle are 9 and 3 The hypotenuse is 9.49 fixed << showpoint << setprecision(2); hyp = sqrt(pow(a,2) + pow(b,2));

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 14 Lab 3.4 Working with Type Casting http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download batavg.cpp http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab // This program will determine a batting average. The number // of hits and at bats are assigned internally in the program. // PUT YOUR NAME HERE. #include using namespace std; const int ATBATS = 421; const int HITS = 123; int main() { int batavg; batavg = HITS / ATBATS; cout << "The batting average is " << batavg << endl; return 0; } batavg.cpp

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 15 Exercise 1: batavg.cpp 를 실행한 후 그 출력 결과를 제시하라. 타율은 ? Exercise 2: 위 프로그램은 자료형 변환과 관련한 논리 오류를 가지고 있다. batavg 변수의 자료형을 정수형에서 실수형으로 변경하면 이 문제가 해결될 수 있나 ? 변경한 후 재컴파일, 실행한 후 출력 결과 제시하라. 타율은 ? Exercise 3: 올바른 결과인 0.292162 가 출력되게 프로그램을 수정하라. 단 이름상수의 자료형을 변경하지 말것. Typecast 를 사용할 것 ! static_cast

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 16 Lab 3.5: Reading and Writing to a File Exercise 1: http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download billfile.cpp Lab 3.1 프로그램 (bill.cpp) 의 파일 입출력 version 작성 입력 파일 : transaction.dat( 개수 가격 ) 출력 파일 : bill.outhttp://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 22 10.98 The total bill is $241.56

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 17 // This program will read in the quantity of a particular item and its price. // It will then print out the total price. The input will come from a // data file and the output will go to an output file. // PUT YOUR NAME HERE. #include // needed to use files #include using namespace std; int main() { ifstream dataIn; // defines an input stream for a data file ofstream dataOut; // defines an output stream for an output file int quantity; // number of items purchased double itemPrice; // price of each item double totalBill; // total price of all items billfile.cpp

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 18 dataIn.open("transaction.dat"); // These 2 statements open the files. dataOut.open("bill.out"); _______ << setprecision(2) << fixed << showpoint; // format output in the output file // Write an input statement that brings the quantity and price // of the item in from the data file. // Write an assignment statement that places the correct value in totalBill. // Write an output statement that prints totalBill with a label // to the output file. // Write statements to close the files. return 0; }

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


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

Similar presentations


Ads by Google