140 likes | 269 Views
CS1001 Lecture 24. Character Data Type Strings Functions. Character Data Type. Declaration CHARACTER (LEN = n) :: list or CHARACTER (n) :: list e.g., CHARACTER (10) :: First_Name, Mid_Name, Last_Name CHARACTER (10) :: First_Name, Mid_Name, Last_Name*20
E N D
CS1001 Lecture 24 • Character Data Type • Strings Functions
Character Data Type • Declaration CHARACTER (LEN = n) :: list or CHARACTER (n) :: list e.g., CHARACTER (10) :: First_Name, Mid_Name, Last_Name CHARACTER (10) :: First_Name, Mid_Name, Last_Name*20 First_Name, Mid_Name will have 10 characters Last_Name*20 declares that Last_Name will have 20 characters • Operation Concatenation // - combining two characters values e.g., “centi” // “meter” will give “centimeter” substring - get characters within the string e.g., Length = “centimeter” Length(4:7) will give “time”
E n g n n g e e r i i Concatenation & Substring CHARACTER (15) :: Course, Name*8, New Course = “Engineering” 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Course( :6) = “Engine” Course(10:) =Course(10:15) = “ngbbbb” where b represents blank Course(3:5) = “gin” New = Course(:3) //Course(8:8)// “b2031” = “Engrb2031” Course(1:4)=Course(8:11) gives “ringneering” Course(1:4) = Course (3:6) is not allowed because of overlapping Name = Course will give only “Engineer”
I/O Character(10) : Item,Color*7,Item1*5 Item = “mmbcamera” Item1 = Item Color = “white” PRINT *, Item, Color,Item1 ==> mmbcamerabwhitebbmmbca PRINT “(1X,A,A4)”, Color, “red” ==> bwhitebbbred PRINT “(1X,A3)”,Color ==> bwhi PRINT “(1X,I2,A)”, 35, Item ==> b35mmbcamerab rAw descriptor 10 5 7 7 1 4 1 3 10 1 2
I/O Character :: Item*10,Color*7,Item1*5 READ “(3A)”, Item, Color, Item1 Item = “ComputerbS” Color = “cience1” Item1 = “001” will be stored as “001bb” CHARACTER*4 :: Alpha REAL :: X INTEGER :: I READ 29, Alpha, X, I 29 FORMAT (A3, 1x, F4.2, 3x, I1) ComputerbScience1001 7 10 Test2413/1234 Alpha = “Tesb” X = 24.13 I = 3
I/O Example INTEGER :: Count,Openstatus REAL :: Tem CHARACTER :: Scale*2, Itemp*7 READ *, Itemp OPEN(UNIT = 15, FILE = Itemp, STATUS & = “OLD”, IOSTAT = Openstatus) : DO … READ(15,100) Count, Tem, Scale 100 FORMAT(2X, I4, 4X, F4.1,T16, A2) : File15 listing of File15: 2X I4 4x f4.1 A2 000001bbbb1234bC 000002bbbb1256bC 000003bbbb1278bC count = 1 Tem = 123.4 Scale = “Cb” T16
Character Functions • ADJUSTL(str) and ADJUSTR(str) -- left and right justifies str CHARACTER :: String*10 string = “bstring” = “bstringbbb” ASJUSTL(string) = “stringbbbb” • LEN (str) -- gives length of string str • LEN_TRIM(str) -- gives length of str, ignoring trailing blanks • INDEX(str1,str2) -- return the position of the first occurrence of str2 in str1, return 0 if str2 does not appear in str1 CHARACTER (25) :: UNITS = “centimetersbandbmeters” INDEX(UNITS, “meter”) ==> 6 INDEX(UNITS, “cents”) ==> 0
Derived Types (Chap 10) • Fortran provides six intrinsic data types: INTEGER, REAL, COMPLEX, CHARACTER, LOGICAL, and ARRAY -- (ARRAY is a structure of the other 5 types.) • ARRAYS are used to store elements of the same type. e.g., REAL, DIMENSION (1:50) :: X
Derived Data Types - Structure • Form: TYPE type-name type-specification-statement component-list1 type-specification-statement component-list2 etc. END TYPE type-name e.g., TYPE Student CHARACTER (LEN = 20) :: First, Last INTEGER :: ID CHARACTER (LEN = 4) :: Major, Level END TYPE Student
Last ID Major Level Last ID Major Level Last ID Major Level First First First Using Structures • Definition (previous slide) goes in specification area of program • Declaration is • TYPE (Student), DIMENSION(30) :: CS1001 • Can reference individual components: • structure-name%component-name • Cs1001(15)%cFirst = “Brian” 1 2 30 . . .
Why Structures? • Structures enable you to “package” a set of data related to some common item (student, catalog item, etc.) • A group of these items can then be declared as an array of those items (class, parts list, etc.) • Individual data items in the structure can be referenced and used in a program
Deck of Cards TYPE PlayingCard CHARACTER (LEN = 2) :: Suit*1, Card INTEGER :: Value, Used END TYPE PlayingCard TYPE (PlayingCard) :: Deck(52) ! To shuffle the deck: DO N=1, 52 Deck(N)%Used = 0 END DO ! Trying to do this using a multi-dimension array or individual ! variables would be frustrating at best
Quiz #6 --1 INTEGER, DMIENSION(5,3) :: Table (5,3) INTEGER :: i DO i = 1, 5 Table (i,3) = -1 CONTINUE Table(1,3) = -1 Table(2,3) = -1 Table(3,3) = -1 Table(4,3) = -1 Table(5,3) = -1
Quiz #6 -- 2 I = 27 X = 142.7883 Y = 64.7 Alpha = 'ABCD' Beta = 'WXYZ' print 27, I, X, Y, Alpha, Beta ##27142.79##65.ABCDWXY 27 FORMAT (I4, F6.2, 2x, F3.0, A, A3) 27 FORMAT (1x, I3, F6.2, F5.0, A4, A3)