more control over input and output (i / o)

30
1 More Control over INPUT and OUTPUT (I / O) The interface between the user and the computer Formats and edit descriptors Input editing Output editing read, write and print statements More powerful formats

Upload: arlen

Post on 04-Jan-2016

34 views

Category:

Documents


1 download

DESCRIPTION

More Control over INPUT and OUTPUT (I / O). The interface between the user and the computer Formats and edit descriptors Input editing Output editing read, write and print statements More powerful formats. More Control over INPUT and OUTPUT (I / O). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: More Control over  INPUT and OUTPUT (I / O)

1

More Control over

INPUT and OUTPUT (I / O)

The interface between the user and the computerFormats and edit descriptors

Input editingOutput editing

read, write and print statementsMore powerful formats

Page 2: More Control over  INPUT and OUTPUT (I / O)

2

More Control over INPUT and OUTPUT (I / O)

The input and output facilities of any programming language are extremely important, because it is through these features of the language that communication between the user and the program is carried out.

F, therefore, provides facilities for input and output at two quite different levels.

As are given before, list-directed input and output statements that provide the capability for straightforward input from the keyboard and also the output to the display or printer.

These list-directed “i/o” - statements, however, allow the user very little control over the source or layout of the input data.

Page 3: More Control over  INPUT and OUTPUT (I / O)

3

F allows the programmer,

to specify exactly how the data will be presented and interpreted, from which of the available input units it is to be read

how the results are to be displayed

to which of the available output units the results are to be sent.

More Control over INPUT and OUTPUT (I / O)

Page 4: More Control over  INPUT and OUTPUT (I / O)

4

INTERFACE between the USER and the COMPUTER

Considering the following 9 – digit input data there are enormous number of possible interpretations :

Data : 1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

- 1, 2, 3, 4, 5, 6, 7, 8, and 9

- 123, 456, 789

- 12345 . 6789

- 1.23, 0.45, 67, 8900- etc.

Page 5: More Control over  INPUT and OUTPUT (I / O)

5

FORMATS and EDIT DESCRIPTORS

An input statement must contain three distinct types of information

•where the data is to be found•where it is to be stored in the computer’s memory•how it is to be interpreted

Similarly, an output statement must define

•where the results are currently stored•where they are to be sent•in what form they are to be displayed.

Page 6: More Control over  INPUT and OUTPUT (I / O)

6

INPUT EDITING

EXAMPLES of INPUT DATA : 123456789

read “ ( i 3, i 3, i 3 )” , n1, n2, n3

! n1 = 123, n2 = 456, n5 = 789

read “ ( t 5, i 2, t 6, i 2, t 2, i 4 )” , x, y, z

! x = 56, y = 67, z = 2345

read “ ( f 9.4 )” , real_num ! real_num = 12345.6789

read “ ( f 3.1, f 2.2, f 3.0 )” , r1, r2, r3 ! r1 = 12.3, r2 = 0.45, r3 = 678.0

123456789

123456789

123456789

Page 7: More Control over  INPUT and OUTPUT (I / O)

7

EXAMPLE for formatting of real numbers using “ f – edit descriptor “ depending on the two different INPUT DATA :

read “ ( f 3.1, f 2.2, f 3.0 )” , s1, s2, s3

DATA 1 DATA 2

123456789 .23.56.8 ---------------------- -----------------s1 contains : 12.3 0.23

s2 contains : 0.45 0.5

s3 contains : 678.0 6.8

Page 8: More Control over  INPUT and OUTPUT (I / O)

8

EXAMPLEs for formatting of an input real number in different ways using “ a – edit descriptor “

real number is : 361.764

•361.764•3.61764+2•361764–3•0.0361764e4•3617.64d-1•3.61764 E +2•361.764+0

Page 9: More Control over  INPUT and OUTPUT (I / O)

9

EXAMPLEs for formatting of an input – character type using “ a – edit descriptor “

• character ( len = 10 ) : : ch1• character ( len = 6 ) : : ch2• character ( len = 15 ) : : ch3

Depending on the above-given declaration the formats may have the following forms in read statements :

• read “ ( a10, a6, a15 )” , ch1, ch2, ch3 or• read “ ( a, a, a )” , n1, n2, n3

Page 10: More Control over  INPUT and OUTPUT (I / O)

10

LOGICAL DATA - INPUT EDITING

The edit descriptor is used with logical data, and takes the following form, where upper case “ L” is used to avoid the potential confusion with the digit “ 1 “ that can be caused to human readers by using the lower-case form :

L w

“ w “ caharacters are used to derive either a “ true “ value, a “ false “ value or an error.

Page 11: More Control over  INPUT and OUTPUT (I / O)

11

Page 12: More Control over  INPUT and OUTPUT (I / O)

12

Any of the following are acceptable as representing true :

t true .T .true. truthful

while the following will all be interpreted as false :

F False .f. futile

LOGICAL DATA - INPUT EDITING

Page 13: More Control over  INPUT and OUTPUT (I / O)

13

OUTPUT EDITING

The following figure shows the main edit descriptor that are available for output.

EXAMPLE for “i” edit descriptor

tom = 23dick = 715harry = -12

print *, “( i 5, i 5, i 5 )”, tom, dick, harry

This statement produce following line of output (where the symbol # represents a space ) :

###23##715##-12

Page 14: More Control over  INPUT and OUTPUT (I / O)

14

EXAMPLE for “f” edit descriptor :

x = 3.14159y = -275.3024z = 12.9999

print *, “( f 10.3, f 10.3, f 10.3 )”, x, y, z

This statement produce following line of output (where the symbol # represents a space ) :

#####3.142##-275.302###-13.000

OUTPUT EDITING

Page 15: More Control over  INPUT and OUTPUT (I / O)

15

EXAMPLE :

program tabular_output

real, parameter : : third = 1.0 / 3.0 real : : x integer : : i do i = 1, 10 x = i print “( f 15.4, f 15.4, f 15.4 )”, x, sqrt (x), x**third end do

end program tabular_output

OUTPUT EDITING

Page 16: More Control over  INPUT and OUTPUT (I / O)

16

In the above-given program the same edit descriptor has been repeated several times.

A number, called a “repeat count”, may be placed before the “ i, f, a or L “ – edit descriptors to indicate how many times they are to be repeated. Depending on this, the format could be written more succinctly andİt is possible to write it more clearly.

EXAMPLE : print “( i 5, i 5, i 5, f 6.2, f 6.2, f 6.2, f 6.2 )”, x, y, z, d, e, f, g

or better

print “( 3 i 5, 4 f 6.2 )”, x, y, z, d, e, f, g

OUTPUT EDITING

Page 17: More Control over  INPUT and OUTPUT (I / O)

17

Exercise 2

program test1integer ::a,breal::c,dprint*,"please enter an integer values which is

more than 6 digit"read "(i4,t1,i4,t2,f4.1,t1,f4.2)",a,b,c,dprint "(i4,a,i5,a,i5,a,f6.2,a,f6.2,a,f8.3)",a,& " minus",b," is",a-b,";",c," minus",d, " is",c-dend program test1

Page 18: More Control over  INPUT and OUTPUT (I / O)

18

Exercise 3

program test2

character(len=6)::a,b,c

Print*,”Please write your name”

read "(a8,t1,a4,t1,a)",a,b,c

print "(a10,tr12,a4,tr30,a)",a,b,c

print "(a,t10,a,t52,a)",a,b,c

end program test2

Page 19: More Control over  INPUT and OUTPUT (I / O)

19

program five_digit_numbers integer::iinteger,dimension(12)::arrarr = (/12345, 23456, 34567, 45678,& 90123, 12340, 24680, 46802,& 13579, 35791, 57913, 69649/)

! output for single column format exampleprint*,"Single column of numbers"do i = 1 , 12print "(t8,i2,1x,i5)",i,arr(i)end do! output for four rows of three numbers print "(2/t3,a/)","Four rows of three numbers "print "(t8,i2,1x,3(i5,1x))", 1,arr(1:3)print "(t8,i2,1x,3(i5,1x))", 2,arr(4:6)print "(t8,i2,1x,3(i5,1x))", 3,arr(7:9)print "(t8,i2,1x,3(i5,1x))", 4,arr(10:12)! a single line of numbersprint "(2/t3,a/)", "A single line of numbers "print "(t2,12(i5,1x))", arrend program five_digit_numbers

Page 20: More Control over  INPUT and OUTPUT (I / O)

20

program multi_record_example

real :: a,b

a = 12.25

b = 23.50

write(unit=6,fmt="(t10,a,3/,a,f6.2,a,f6.2,a,///,f7.2,2/,a,f10.3)")&

"Multi-record example", &

" The sum of ",a," and",b," is", a+b, &

" Their product is",a*b

end program multi_record_example

Page 21: More Control over  INPUT and OUTPUT (I / O)

21

program railway_time_tableinteger,parameter::n=5integer::ireal,dimension(n)::arrival,departureinteger,dimension(n)::platformcharacter(len=10),dimension(n)::destinationarrival=(/9.23, 9.28, 10.41, 10.48, 11.15/)departure=(/9.25, 9.32, 10.53, 10.53, 11.18/)platform=(/2, 1, 3, 2, 1/)destination=(/"Edinburgh"," London","Sheffield",& "Newcastle"," London"/)write(unit=6,fmt="(/a/2x,7('='),3x,9('='),3x,8('='),3x,11('='))") & " Arrival Departure Platform Destination"do i = 1 , nwrite(unit=6,fmt="(3x,f5.2,6x,f5.2,8x,i1,8x,a10)") & arrival(i),departure(i),platform(i),destination(i)end doend program railway_time_table

Page 22: More Control over  INPUT and OUTPUT (I / O)

22

program test2character(len=6)::a,b,cprint *, "........................ STEPPER... Entering PROGRAM. Press Enter"print*,"program test2"read *print *, "........................ STEPPER... Before READ. Press Enter"print*,"read ""(a8,t1,a4,t1,a)"",a,b,c"read *read "(a8,t1,a4,t1,a)",a,b,cprint *, "........................ STEPPER... After READ. Press Enter"read *print "(a8,tr2,a4,tr3,a)",a,b,cprint*,"print ""(a8,tr2,a4,tr3,a)"",a,b,c"print *, "........................ STEPPER... After PRINT. Press Enter"read *print "(a,t10,a,tr2,a)",a,b,cprint*,"print ""(a,t10,a,tr2,a)"",a,b,c"print *, "........................ STEPPER... After PRINT. Press Enter"read *print *, "........................ STEPPER... Before ENDPROGRAM. Press Enter"print*,"end program test2"read *end program test2

Page 23: More Control over  INPUT and OUTPUT (I / O)

23

program tabular_outputreal, parameter :: third = 1.0 / 3.0real :: xinteger :: ido i = 1 , 10x = i

! print "(f15.4, f15.4, f15.4)", x,sqrt(x),x**thirdprint "(3f15.4)", x,sqrt(x),x**thirdend doend program tabular_output

Page 24: More Control over  INPUT and OUTPUT (I / O)

24

program aa character (len=50) :: a, b, c, d, e integer :: i, j a = "aaa bbbb ccccc dddddd eeeeeee" print *, a i = index(a," ")+1 b = a(:i-2) print *, b c = a(i:) print *, c j = index (c," ") +1 d = c(:j-2) print *, d e = d(j:) print *, e end program aa

Page 25: More Control over  INPUT and OUTPUT (I / O)

25

program character_searc_function !examle for index, you can olso try scan and verify character (len=50) :: a, b, c, d, e integer :: i, j a = "bbb cccc ddddd eeeeee" print *, a i = index(a,"d") !Calculates how many chracter before and including "c" print*,"i",i b = a(:i-2) print *, b c = a(i:) print *, c j = index (c," ") +1 d = c(:j-2) print *, d e = d(j:) print *, e end program character_searc_function

Page 26: More Control over  INPUT and OUTPUT (I / O)

26

! Assignment statements with constant expressions. program C01 implicit none integer, parameter :: NAME_LENGTH = 21 real :: Avogadros_Number integer :: Many complex :: Z logical :: Flag character (len = NAME_LENGTH) :: Name! start program C01 Many = 346021 ! Integer type Avogadros_Number = 6.0221367e23 ! Real type Z = (0.0, 1.75e8) ! Complex type Flag = .true. Name = " Mustafa " ! Character type, length 21 write (unit = *, fmt = *) Many, Avogadros_Number, Z, Flag, Name stop end program C01

Page 27: More Control over  INPUT and OUTPUT (I / O)

27

! Assignment statements with variable expressions. module C02M implicit none public :: Assign_2 contains subroutine Assign_2( ) ! Type declarations with initialization: real, save :: Y = 1.23, Pi = 3.141592 integer, save :: Counts = 173 character (len = 8), save :: I = " Optics " logical, save :: Flag = .true. ! Type declarations without initialization: real :: X, Theta integer :: Many character (len = 8) :: K logical :: Done ! start subroutine Assign_2 X = Y ! Real type Y = X Theta = Pi Many = Counts ! Integer type K = I ! Character type, length 8 Done = Flag ! Logical type write (unit = *, fmt = *) Y, Pi, X, Theta, Counts, Many, I, K, Flag, Done return end subroutine Assign_2 end module C02M

program C02 use C02M implicit none! start program C02 call Assign_2( ) stop end program C02

Page 28: More Control over  INPUT and OUTPUT (I / O)

28

! Assignment statements with arithmetic expressions. program C03 implicit none real, parameter :: GRAVITY = 9.8 real :: Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp real :: Mass, Velocity, Kinetic_Energy, Pressure, Density, Height, Bernoulli integer, parameter :: J = 6 integer :: I! start program C03 I = J + 1 write (unit = *, fmt = *) I read (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Press, New_Temp New_Vol = Old_Vol * (Old_Press / New_Press) * (New_Temp / Old_Temp) write (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp read (unit = *, fmt = *) Mass, Velocity Kinetic_Energy = 0.5 * Mass * Velocity ** 2 write (unit = *, fmt = *) Mass, Velocity, Kinetic_Energy read (unit = *, fmt = *) Pressure, Density, Velocity, Height Bernoulli = Pressure + Density * (0.5 * Velocity ** 2 + GRAVITY * Height) write (unit = *, fmt = *) Pressure, Density, Velocity, Height, Bernoulli stop end program C03

Page 29: More Control over  INPUT and OUTPUT (I / O)

29

program aa character (len=50) :: a, b, c, d, e integer :: i, j a = "aaa bbbb ccccc dddddd eeeeeee" print *, a i = index(a," ")+1 b = a(:i-2) print *, b c = a(i:) print *, c j = index (c," ") +1 d = c(:j-2) print *, d e = d(j:) print *, e end program aa

program 0206 ! Asks the hour and the minute values of the ! present time and displays it as a sentence. integer :: hh,mm print *, " Input the hour, in the 24 hours format." read *, hh print *, " Input the minute." read *, mm print *, "" print *, " THE TIME IS ",mm," MINUTES AFTER ",hh end program 0206 ! Assignment statements with constant expressions.

program C01 implicit none integer, parameter :: NAME_LENGTH = 21 real :: Avogadros_Number integer :: Many complex :: Z logical :: Flag character (len = NAME_LENGTH) :: Name ! start program C01 Many = 346021 ! Integer type Avogadros_Number = 6.0221367e23 ! Real type Z = (0.0, 1.75e8) ! Complex type Flag = .true. Name = " Mustafa Sezer" ! Character type, length 21 write (unit = *, fmt = *) Many, Avogadros_Number, Z, Flag, Name stop end program C01

Page 30: More Control over  INPUT and OUTPUT (I / O)

30

program C02 use C02M implicit none ! start program C02 call Assign_2( ) stop end program C02 ! Assignment statements with variable expressions. module C02M implicit none public :: Assign_2 contains subroutine Assign_2( ) ! Type declarations with initialization: real, save :: Y = 1.23, Pi = 3.141592 integer, save :: Counts = 173 character (len = 8), save :: I = " Optics " logical, save :: Flag = .true. ! Type declarations without initialization: real :: X, Theta integer :: Many character (len = 8) :: K logical :: Done ! start subroutine Assign_2 X = Y ! Real type Y = X Theta = Pi Many = Counts ! Integer type K = I ! Character type, length 8 Done = Flag ! Logical type write (unit = *, fmt = *) Y, Pi, X, Theta, Counts, Many, I, K, Flag, Done return end subroutine Assign_2 end module C02M ! Assignment statements with arithmetic expressions.

program C03 implicit none real, parameter :: GRAVITY = 9.8 real :: Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp real :: Mass, Velocity, Kinetic_Energy, Pressure, Density, Height, Bernoulli integer, parameter :: J = 6 integer :: I ! start program C03 I = J + 1 write (unit = *, fmt = *) I read (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Press, New_Temp New_Vol = Old_Vol * (Old_Press / New_Press) * (New_Temp / Old_Temp) write (unit = *, fmt = *) Old_Vol, Old_Press, Old_Temp, New_Vol, New_Press, New_Temp read (unit = *, fmt = *) Mass, Velocity Kinetic_Energy = 0.5 * Mass * Velocity ** 2 write (unit = *, fmt = *) Mass, Velocity, Kinetic_Energy read (unit = *, fmt = *) Pressure, Density, Velocity, Height Bernoulli = Pressure + Density * (0.5 * Velocity ** 2 + GRAVITY * Height) write (unit = *, fmt = *) Pressure, Density, Velocity, Height, Bernoulli stop end program C03