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

Slides:



Advertisements
Similar presentations
Problem You require an algorithm that will receive the length of the hypotenuse and one of the sides of a right triangle, and calculate the length of the.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 9- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6 WEEK 1 - INTRODUCTION TO VOID FUNCTIONS (PROCEDURES)
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 수식과.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11.5 Lines and Curves in Space.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 4 Applications of the Derivative.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1 Functions.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 7 WEEK 1 – 구조체 (STRUCTURES)
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6-2 WEEK 2 - FUNCTIONS THAT RETURN A VALUE.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 4.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Introduction to C++ (Extensions to C)
Introduction to Programming
CPS120: Introduction to Computer Science
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Chapter 2 Assignment and Interactive Input
Chapter 3: Expressions and Interactivity.
Starting Out with C++: From Control Structures through Objects
Wednesday 09/23/13.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Input Based Programs.
Presentation transcript:

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

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

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

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

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

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

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 $

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

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide // 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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Lab 3.3 Arithmetic Operations and Math Functions 에서 download righttrig.cpp a b hyp = ??? hyp = sqrt(pow(a,2) + pow(b,2));

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide // 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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 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 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));

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Lab 3.4 Working with Type Casting 에서 download batavg.cpp // 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

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

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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide // 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

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 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; }

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