View
215
Download
0
Embed Size (px)
Citation preview
MET 50, FALL 2011
2
Character data
We have extensively used REAL and INTEGER data types.
There is also:
CHARACTER data COMPLEX data LOGICAL data
12/1/2011
MET 50, FALL 2011
3
Character data
COMPLEX numbers take the form:
Z = X + iY
where X and Y are both real numbers
and i2 = -1, so that i = (-1)
12/1/2011
MET 50, FALL 2011
4
Character data
Z = X + iY
X is called the real part of Z {denoted Re(Z)}
REAL (X) value
Y is called the imaginary part of Z {denoted Im(Z)}
AIMAG (X) value
12/1/2011
MET 50, FALL 2011
5
Character data
Fortran stores Z as two real numbers: X and Y
When you write
PRINT*,Z
You get: (12.0, -9.0)
12/1/2011
MET 50, FALL 2011
6
Character data
CHARACTER DATA
Letters, spaces, punctuation marks
Combined into something called a STRING
12/1/2011
MET 50, FALL 2011
8
Character data
Declared as follows:
CHARACTER (n) :: string
Where “n” is the length = number of characters in the string
12/1/2011
MET 50, FALL 2011
9
Character data
example:
CHARACTER (3) :: month
Then “month” is type character with 3 elements, such as
jan feb mar
Perhaps you want to print a table of results with the month printed at the top … see later for format!
12/1/2011
MET 50, FALL 2011
10
Character data
Other ways to declare:
CHARACTER (LEN=n) :: string1
CHARACTER :: string2
Length in 2nd example is assumed to be ONE!!!
12/1/2011
MET 50, FALL 2011
11
Character data
Other ways to declare:
CHARACTER :: stringa*10, stringb*20, stringc*25
12/1/2011
MET 50, FALL 2011
12
Character data
To specify the value of a string:
CHARACTER (8) :: namename=“John Doe”
Note that the space counts as a character!!
CHARACTER (21) :: filenamefilename=“temperature_data_2005”
12/1/2011
MET 50, FALL 2011
13
Character data
Formatted read/write of a string:
In the FORMAT statement, use the descriptor:
“An”
As in: format (1x, I5, A20, F10.2)
Options: “I”, “F”, “E”, and now…”A” (also “D”…last slides)
12/1/2011
MET 50, FALL 2011
14
Character data
Unformatted read/write:
CHARACTER (3) :: MONTHINTEGER :: NUMBERREAD*,MONTH, NUMBER
You might enter: “JAN” 150
12/1/2011
MET 50, FALL 2011
15
Character data
Defects of character (hahaha!)
If a string is read in that is too long, it is truncated on the right.
CHARACTER (6) :: DAYDAY = “WEDNESDAY”
DAY is stored as WEDNES
12/1/2011
MET 50, FALL 2011
16
Character data
If a string is read in that is too short, it is padded with blanks on the right.
CHARACTER (12) :: DAYDAY = “WEDNESDAY”
DAY is stored as “WEDNESDAY ”
12/1/2011
MET 50, FALL 2011
17
Character data
Character data can be “added” !!!
The operation is called: concatenation “cat” for short
character :: first*5, second*6, third*11first = ‘minor’second = ‘ thing’third = first // second
produces… third = ‘minor thing’
12/1/2011
MET 50, FALL 2011
18
Character data
Useful??? Creation of file names inside some code…
character :: first*9, second*4, third*9character :: title*22first = ‘data-for-’third = ‘-smoothed’ DO YEAR = 1,10 if (year == 10) then second = ‘2010’ title = first // second // third endif! Produces title = ‘data-for-2010-smoothed’OPEN (15, file=‘title’) END DO
12/1/2011
MET 50, FALL 2011
19
Character data
DOUBLE PRECISION data…
Suppose you declare:REAL :: TEMP
and later print a value with PRINT*,TEMP
a number with certain number of digits after the decimal.
12/1/2011
MET 50, FALL 2011
20
Character data
Instead if you declare:DOUBLE PRECISION:: TEMP
and later print a value with PRINT*,TEMP
® a number with DOUBLE the number of digits after the decimal.
® Double precision!
12/1/2011