21 april, 2000 cs1001 lecture 22 formatted input and output

26
21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

Upload: isaac-lindsey

Post on 18-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

CS1001 Lecture 22

• Formatted Input and Output

Page 2: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Types of Input/Output

• List Directed– PRINT *, “Temperature = “, Temperature

– READ *, Choice

• User-Formatted– PRINT ‘(F8.2)’, Temperature

– PRINT 120, Temperature

120 FORMAT (F8.2)

– READ ‘(I4, I6)’, Input1, Input2

– READ 130, Input1, Input2

130 FORMAT (I4, I6)

Page 3: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

PRINT Statement

• PRINT format-specifier, output-list• format-specifier is

– *, an asterisk (format is compiler-dependent)

– a character constant or variable

– the label of a FORMAT statement • FORMAT statement can apply to more than one PRINT

statement

• FORMAT statement should follow PRINT statement for readability

• output-list is an expression or list of expressions separated by commas

Page 4: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Integer Output

• rIw or rIw.m (right justified)– r - repetition indicator, indicating the number of fields

– I - denotes integer data

– w - number of spaces to display data

– m - minimum number of digits to display

Example of 4I5.2:

^1234^^567^^^89^^^00

Example of 4I5:

^1234^^567^^^89^^^^0

5

5

5

5

5

5

5

5

Page 5: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Real Output

• rFw.d (right justified)– r - repetition indicator, indicating the number of fields

(right-justified)

– F - denotes real (floating point) data

– w - total width of the field

– d - digits to right of decimal point

• Example of 2F8.3:^123.456^^^0.789

8 8 3

3

Page 6: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Real, Exponential Notation

• rEw.d– r - repetition indicator, indicating the number of fields

– E - real data, exponential notation,• 0.0 <= mantissa < 1.0

– w - total width of field

– d - digits to right of decimal point

Example of 2E15.5:

^^^^0.12345E+08 ^^^^0.23700E-0115 5

15 5

Page 7: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Character Output• rA or rAw

– r - repetition indicator

– w - width (optional)

Example:

X = 12.3456

Y = -12.3456

PRINT 264, ‘X = ‘, X, ‘ Y = ‘, Y

264 FORMAT(1X, A, F6.2, A, F6.2)

1X, A, F6.2, A, F6.2

X = 12.35 Y = -12.35 ^ ^ ^^ ^ ^ ^

Page 8: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Positional Descriptors

• 1X at beginning of descriptor list denotes single line spacing AND single blank space

• nX elsewhere in descriptor list denotes number of blank spaces to be printed

• Tc “tabs” to the cth space on the line, so data begins there (absolute space number, but first space is blank)

Page 9: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Control Characters• 1X or ‘ ‘ (blank) for normal spacing• ‘0’ for double spacing• ‘1’ for advancing to a new page• ‘+’ for overprinting current line• Printer/system dependent, may not be in effect

Slash Descriptor• / causes output to begin on a new line• /// would cause two blank lines to be inserted

before data output

Page 10: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Format Descriptors - Summary

• Iw or Iw.m for integer data• A or Aw for character data• ‘x...x’ or “x...x” for character strings• Fw.d for real data in decimal notation• Ew.d real exponential notation• Tc tab descriptors• nX - inserts n blanks in an output line• / - vertical spacing

Page 11: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

WRITE Statement

• WRITE (control-list) output-list• control-list includes:

– a unit specifier indicating the output device• UNIT = unit-specifier

• or simply unit-specifier

– a format-specifier• FMT = format-specifier

• or simply format-specifier

• output-list is same as for PRINT statement

Page 12: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Unit Numbers

• Standard Input device is the terminal keyboard or UNIT 5

• Standard Output device is the terminal display monitor or UNIT 6

• Standard Error device is the terminal display monitor or UNIT 0

Page 13: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Equivalent WRITE Statements

• All of which are equivalent :PRINT *, Time,Temp

WRITE (6, *) Time, Temp

WRITE (6, FMT = *) Time, Temp

WRITE (UNIT = 6, FMT = *) Time, Temp

WRITE (NOUT, *) Time, Temp where NOUT = 6

WRITE (UNIT = NOUT, FMT = *) Time, Temp

Page 14: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

READ Statement

• READ format-specifier, input-list• format-specifier is:

– * (an asterisk)

– a character variable or constant

– the label of a FORMAT statement

– use the first form for this class until we get into file handling

• input-list is a variable or list of variables

Page 15: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

General READ Statement

• READ (control-list) input-list• input-list is same as for READ statement• control-list includes:

– a unit specifier indicating the output device• UNIT = unit-specifier

• or simply unit-specifier

– a format-specifier• FMT = format-specifier

• or simply format-specifier

Page 16: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

READ Statements

• All of which are equivalent :READ *, Time, Temp

READ (5, *) Time, Temp

READ (5, FMT = *) Time, Temp

READ (UNIT = 5, FMT = *) Time, Temp

READ (NIN, *) Time, Temp where NIN = 5

READ (UNIT = NIN, FMT = *) Time, Temp

Page 17: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Additional Format Material

Page 18: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Format Descriptors 1/2

• Iw or Iw.m for integer data• Bw or Bw.m for integer data in binary form• Ow or Ow.m for integer data in octal form• Zw or Zw.m for integer in hexadecimal• A or Aw for character data• ‘x...x’ or “x...x” for character strings• Lw for logical data

Page 19: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Format Descriptors 2/2

• Fw.d for real data in decimal notation• Ew.d or Ew.dEe real exponential notation

• 0.0 <= mantissa < 1.0

• ESw.d or ESw.dEe real scientific notation• 1.0 <= mantissa < 10.0 (unless = 0.0)

• ENw.d or ENw.dEd real engineering notat.• 1.0 <= mantissa < 1000.0, exponent multiple of 3

• Gw.d or Gw.dEe general i/o descriptor

Page 20: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Descriptor Meanings

• w - positive integer constant - width• m - nonnegative integer constant - minimum

number of digits to be displayed• d - nonnegative integer constant - digits to right of

decimal point• e - nonnegative integer constant - digits in

exponent• x - a character

Page 21: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Other Integer Representation

• Given the integer value of “200” decimal• Format B9 produces “^11001000”• Format O4 produces “^310”• Format Z3 produces “^C8”

Page 22: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Do Not Worry About

• S, SP and SS optional output plus sign controls:– SP specifies that optional plus signs are to be printed

– SS suppresses optional plus signs

– S restores the processor option

• BN and BZ blank character controls– BN treats blanks as nulls, and ignores them

– BZ treats blanks as zeros

• kP scale factor control (2P treats input of 123 as 1.23 internally

Page 23: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Other Format Descriptors

• General descriptor Gw.d and Gw.dEe are the same as Iw format, values of d and e are ignored.

• Relative tab descriptors TRw and TLw:• TRw tabs right w positions and is identical in operation to wX

• TLw tabs left w positions and is a form of backward tabbing, which is to be avoided

• There is no reason to use any of these

Page 24: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Real, Scientific Notation

• rESw.d or rESw.dEe– r - repetition indicator, indicating the number of fields

– ES - real data, scientific notation

– w - total width of field

– d - digits to right of decimal point

– e - number of positions to display exponent

Example of 2ES15.5:

^^^^1.23450E+08^^^^2.37000E-01

Page 25: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Real, Engineering Notation

• rENw.d or rENw.dEe– r - repetition indicator, indicating the number of fields

– EN - real data, engineering notation

– w - total width of field

– d - digits to right of decimal point

– e - number of positions to display exponent

Example of 2EN15.5:

^^123.45000E+06^^237.00000E-03

Page 26: 21 April, 2000 CS1001 Lecture 22 Formatted Input and Output

21 April, 2000

Special Descriptors

• Tc, TLn, TRn - tab descriptors– c - positive integer constant representing character

position

– n - positive integer constant specifying number of character positions

• nX - inserts n blanks in an output line• / - vertical spacing