計算機概論實習 2007-03-16. 2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

Numeric Types, Expressions, and Output ROBERT REAVES.
File streams Chapter , ,
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Chapter 3: Input/Output
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction 21.2Streams Iostream Library Header.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
FORMATTED INPUT OUTPUT. Topics to be discussed……………….. Formatted Console I/O operationsFormatted Console I/O operations Defining field width :Width()Defining.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 - C++ Stream Input/Output Outline 12.1 Introduction 12.2 Streams Classic Streams vs.
Output Formatting. Precision #include... float grade = f; cout.precision(4); cout
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
C++ Streams Lecture-2.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
The C++ Programming Language Streams. Contents u Output Stream u Input Stream u Formatting u Manipulators u Files & Streams.
CPS120: Introduction to Computer Science Formatted I/O.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
Student Book Input / Output in C++
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. More on Handling Basic Data Types Mixed types of data Data.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Chapter 05 (Part II) Control Statements: Part II.
C++ How to Program, 8/e © Copyright by Pearson Education, Inc. All Rights Reserved.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
Chapter 13 Stream Input/Output C++ How to Program, 9/e © Copyright 2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Topic 2 Input/Output.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Chapter 21 - C++ Stream Input/Output
Formatting Screen Input/Output
Standard Input/Output Streams
Standard Input/Output Streams
Manipulators CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 12 - C++ Stream Input/Output
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
Programming with ANSI C ++
C++ Stream Input/Output
Chapter 12 - C++ Stream Input/Output
Presentation transcript:

計算機概論實習

2 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators to change base dec (resets to base 10) cout << dec << 1000  1000 hex (base 16) cout << hex << 1000;  3e8 oct (base 8) cout << oct << 1000;  1750

3 Integral Stream Base expression: dec, oct, hex, setbase, and showbase setbase(newBase) One of 8, 10, or 16 cout << setbase(dec) << 1000; Base remains same until explicitly changed showbase Forces base of number to be shown Decimal numbers default 1000 Preceding 0x or 0X for hex 0x3e8 Preceding 0 for octal Remove with noshowbase

4 Floating-Point Precision (precision, setprecision) Set precision of floating point numbers Number of digits to right of decimal setprecision stream manipulator Pass number of decimal points cout << setprecision(5) precision member function cout.precision(newPrecision) With no arguments, returns current precision Settings remain until changed explicitly

5 precision example int main(){ double root2 = sqrt( 2.0 ); //sqrt(2.0) = ….. for (int i = 0; i <= 11 ; i++){ cout.precision( i ); cout << root2 << endl; } return 0; }

6 Field Width (width, setw) width member function (base class ios_base) Sets field width Number of character positions for output Can also use setw stream manipulator

7 width, setw example int main(){ const int SIZE= 80; char buffer[SIZE]; cout << "Enter a sentence:" << endl; cin.get(buffer, SIZE); cout << "set width 5" << endl; cout.width(5); cout << buffer << endl; cout << "set width 6\n" << setw(6) << buffer << endl; return 0; }

8 Trailing Zeros and Decimal Points (showpoint) showpoint Forces decimal number to print with trailing zeros For decimal number without showpoint with showpoint (up to level of precision) Reset with noshowpoint

9 showpoint example int main(){ cout << showpoint << "After using showpoint" << endl << " prints as: " << << endl << " prints as: " << << endl << " prints as: " << << endl << "9 prints as: " << 9 << endl; return 0; } After using showpoint prints as: prints as: prints as: prints as: 9

10 Justification (left, right and internal) left stream manipulator Left-justified, padding to right right stream manipulator Right-justified, padding to left internal Number's sign left-justified Number's value right-justified showpos forces sign to print Remove with noshowpos

11 left, right and internal example int x = 12345; cout << showpos; cout << "Default is right justified:" << endl << setw( 10 ) << x; cout << "\n\nUse std::left to left justify x:\n" << left << setw( 10 ) << x; cout << "\n\nUse std::right to right justify x:\n" << right << setw( 10 ) << x << endl; cout << "\n\nUse std::internal to internal justify x:\n" << internal << setw(10) << x << endl;

12 left, right and internal example Default is right justified: Use std::left to left justify x: Use std::right to right justify x: Use std::internal to internal justify x:

13 Padding (fill, setfill) Set fill character used in padding fill member function cout.fill('*') cout.fill( '*' ); cout << setw( 10 ) << dec << x << endl;  *****10000 setfill stream manipulator setfill( '*' ) cout << setfill( '*' ) << setw( 10 ) << dec << x << endl;

14 Floating-Point Numbers; scientific and fixed) Stream manipulator scientific Forces scientific notation e+009 Stream manipulator fixed Forces fixed point format Prints number of decimals specified by precision

15 Scientific and fixed example double x = ; double y = 1.946e9; cout << "Displayed in default format:" << endl << x << '\t' << y << endl; cout << "\nDisplayed in scientific format:" << endl << scientific << x << '\t' << y << endl; cout << "\nDisplayed in fixed format:" << endl << fixed << x << '\t' << y << endl;

16 Uppercase/Lowercase Control (uppercase) Stream manipulator uppercase Uppercase E in scientific notation 1E10 Uppercase X in hex notation and uppercase hex letters 0XABCD By default, lowercase Reset with nouppercase

17 Specifying Boolean Format (boolalpha) Data type bool Values true or false Outputs 0 (false) or 1 (true) when used with << Displayed as integers Stream manipulator boolalpha Display strings "true" and "false" Reset with noboolalpha bool boolvalue = true; cout << “The result is " << boolalpha << boolvalue;  The result is true

18 Setting and Resetting the Format State via Member-Function flags Can save/restore format states After apply many changes, may want to restore original Member function flags cout.flags() With no argument Returns current state as fmtflags object Namespace ios_base Represents format state cout.flags(0) : reset format With fmtflags argument Sets state Returns previous state

19 Example int main() { int integerValue = 1000; double doubleValue = ; cout << "The value of the flags variable is: " << cout.flags() << "\nPrint int and double in original format:\n" << integerValue << '\t' << doubleValue << endl << endl; ios_base::fmtflags originalFormat = cout.flags(); cout << showbase << oct << scientific; // change format

20 Example cout << "The value of the flags variable is: " << cout.flags() << "\nPrint int and double in a new format:\n" << integerValue << '\t' << doubleValue << endl << endl; cout.flags( originalFormat ); // restore format cout << "The restored value of the flags variable is: " << cout.flags() << "\nPrint values in original format again:\n" << integerValue << '\t' << doubleValue << endl; return 0; }

21 The value of the flags variable is: 513 Print int and double in original format: The value of the flags variable is: Print int and double in a new format: e-002 The restored value of the flags variable is: 513 Print values in original format again:

22 Practice 2 (P2) Please write a program which input interface is as follows: and the output interface is like below: 請輸入學生的三個成績 >: 80,90,90 國文 英文 數學 平均 字元寛