Copyright © 2004-2013 Curt Hill Formatting Reals Outputs other than normal.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2: Introduction to C++.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Copyright © 2005 Curt Hill Constants in C++ Why and How.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Copyright Curt Hill Variables What are they? Why do we need them?
Copyright © – Curt Hill Types What they do.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
CS 125 Lecture 9 Martin van Bommel. Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1,
Copyright © – Curt Hill Building Windows Applications in wxDev-C++
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
28 Formatted Output.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
TMF1414 Introduction to Programming
Chapter 3: I/O Management
No Objects, No Type Safety
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 9 - Formatted Input/Output
Conversion Check your class notes and given examples at class.
Introduction to Java Applications
Variables in C Topics Naming Variables Declaring Variables
Putting Values into a Suitable Form for Display
Presentation transcript:

Copyright © Curt Hill Formatting Reals Outputs other than normal

Copyright © Curt Hill Introduction C++ is a general purpose language The default behavior of anything must be general –Work for all types of applications –Scientific, business or whatever The general solution is never the right solution for all applications Something better is needed

What do we want? To be able to take a float or double and get more than just the standard output: –Control the number of digits of precision –Control digits to left and right of the decimal point We could write our own functions to do this Dev-C++ already has a mechanism for this Copyright © Curt Hill

The Solutions There are two static methods of the wxString class –Format –FromDouble We will look at both Copyright © Curt Hill

wxStrings There are several methods of wxString that will be useful to format floats and doubles These are static methods That means they do not need an instance of a wxString to work They do produce an output String Copyright © Curt Hill

Static Methods Both of these are static methods of wxString What is a static method? A method that does not need an instance of the variable of that type Thus we need to call it in one of two ways: –Using an uninvolved instance –Using the class name and scope resolution operator Copyright © Curt Hill

FromDouble Method signature: wx String FromDouble(double,int) The function returns a string representation of the first parameter The second is then number of digits following the decimal point Copyright © Curt Hill

Example Consider the following code: Memo1->AppendText( wxString::FromDouble(d,8)) << wxString::FromDouble(d,2)); Suppose that d has Then the result would be and Copyright © Curt Hill

Format Method signature: wx String Format(string,double) The function returns a string representation of the second parameter, based on the format code of the first parameter The second is the item to be formatted Format codes will be considered next Copyright © Curt Hill

More This scheme is an adaptation of how C did input and output with scanf and printf The first parameter was a format specifier –Quoted string –The format specifier started with a % and ended with the type specification The parameters that followed were plugged into the string into the specifiers Copyright © Curt Hill

Specifiers C had many specifiers but we only want to look at a couple of them A float or double could be handled with the f specifier –This used standard or fixed point notation An int could be handled with the d specifier There were also modifiers Copyright © Curt Hill

Lengths Between the % and the format descriptor can come a length For integers using %d –%d means leave no extra blanks –%4d means use at least 4 spaces –Any spaces will come at the front –If item needs more than 4 it will use it Copyright © Curt Hill

Floats and Doubles For floats and doubles use %f %f means standard default A constant may also be inserted –%12f This may also include a decimal –%12.5 means: –Entire width is 12 –Digits after decimal of 5 –The digit comes out of the 12 –If more are needed the 12 can be enlarged This is fixed decimal format Exponential format is with the %e Copyright © Curt Hill

Example Consider the following code: Memo1->AppendText( wxString::Format("%8.2f",d)); Suppose that d has a value of then it will display as with three leading blanks Notice the rounding that occurred Copyright © Curt Hill

Multiples Anything in the quoted string that was not a format specifier appeared as itself Multiple format specifiers could also be used if there were multiple parameters that followed They should match as to type Copyright © Curt Hill

Example Consider: wxString s = s.Format( “A is %5.2f and B is %5d”, A, B); If A is and B is 12 we would get: A is 3.48 and B is 12 Copyright © Curt Hill

Precision A float has at most about 7 decimal digits A double has at most about 15 decimal digits The length of the specifier should be less than or equal that maximum for the type Copyright © Curt Hill

Lastly FromDouble would be nice for seeing more digits than the standard Format is better when generating a column of numbers and decimal point alignment would be nice –Do not forget to use a monospaced font for this A demo exists, lets look at it Copyright © Curt Hill