
FORTRAN Format FORTRAN formats are used to control the ap p earance of printed output Its has the follow ing form format edit descriptors There are three dierentways of using format CHARACTERLEN PARAMETER FMT IF CHARACTERLEN PARAMETER FMT I E F CHARACTERLEN STRING READ Form WRITE READFMT Form WRITEFMT READFMT WRITEFMT STRING I F READSTRING Form WRITESTRING Format Edit Descriptors There are several commonly used edit descriptors where w numb er of p ositions m minimum number of po sitions d number of digits to the right of the decimal p oint and e numb er of digits in the exp onential part For readingwri ti ng INTEGERs Iw and Iwm For readingwri ti ng REALs a Decimals Fwd b Scientic notation Ewd and EwdEe For readingwri ti ng CHARACTERs A and Aw For readingwri ti ng LOGICALs Lw For horizontal spacing nX For vertical spacing For tabbing Tc TLc and TRc Edit descriptors are separated by commas CHARACTERLEN FRMT FRMT X I F A WRITEFRMT READFRMT Integer Output Editing Descriptors r Iw and r Iwm I INTEGER output w width of eld iethenumb er of p ositions m minimum numberofdigitstobeprinted r rep etition indicator I is equivalentto I I I INTEGER A B A B WRITE I A WRITE I A WRITE I A WRITE I A WRITE I WRITE I B WRITE I B WRITE I B WRITE I B Real Output the F Descriptor Editing Descriptors r Fwd F REAL output w width of eld iethenumb er of p ositions d numb er of digits to the right of the decimal p oint r rep etition indicator F is equivalentto F F F IMPORTANT the integer part has only w d p ositions to use for decimal p oint and for sign REAL A B A WRITE F A WRITE F A WRITE F A WRITE F A WRITE F A WRITE F A WRITE F A WRITE F A WRITE F A WRITE F A WRITE F A Real Output the E Descriptor Editing Descriptors r Ewd and r EwdEe E REAL output w width of eld iethenumb er of p ositions d numb er of digits to the right of the decimal p oint e numb er of p ositions for exp onent r rep etition indicator E is equivalentto E E E Before printing the number to be IMPORTANT n printed is converted to dddd The actual n printout is dddd rather than the original For Ewd the w p ositions are arranged as follows Thus w d d E w For EwdEethew p ositions are arranged as follows Thus w d e d e E w REAL PI WRITE E PI WRITE EE PI WRITE EE PI E E E Logical Output Editing Descriptors r Lw L LOGICAL output w width of eld iethenumb er of p ositions r rep etition indicator L is equivalentto L L L IMPORTANT the output of TRUE and FALSE are T and F resp ectively and are right justied LOGICAL A B A TRUE B FALSE WRITELL A B T F WRITELL A B T F Character Output Editing Descriptors r A and r Aw A CHARACTER output w width of eld iethenumb er of p ositions r rep etition indicator A is equivalentto A A A IMPORTANT If w is missing the length of the character value is used If w is larger than the length of the character value that value is right justied If w is smaller than the length of the character value that value is truncated ie only the left most w characters will b e printed CHARACTERLEN A CHARACTERLEN B A ABCDE B XYZ WRITEAA A B ABC XYZ WRITEAA A B ABCXYZ WRITEAA A B ABCDE XYZ WRITEA A B ABCDEXYZ Spacing and Tabbing Editing Descriptors nX and Tc TLc and TRc nX insert n spaces Tc move to p osition c TLc movebackward c p ositions TRc moveforward c p ositions IMPORTANT No rep etitio n can b e used Nothing will b e printed However the p osition for printing next data item will b e aected INTEGER A B A B WRITE XIXI A B XIXI INTEGER A B A B WRITETITI A B INTEGER A B C A B C WRITE TITLITRI A B C T I TL I TR I the result you see Printer Control you want to print on a This is necessary ONLY IF printer First Char on a Line Eect space Advance to next line BEFORE printing Advance two lines BEFORE printing Advance to next page BEFORE printing No advancing print on the current line overprinting Watch out the FIRST Character You Print INTEGER A B WRITE I A first space for advancing WRITE I B first line on next page in for form feed The Slash Descriptor Advance to next line Between two adjacent s no comma is required is the same as INTEGER A B CHARACTERLEN C ABCDEFGH CHARACTERLEN ABC CHARACTERLEN TITLE WRITE XIITA A B C ABCDEFGH TITLE TITLE ABC ABC WRITEX A A TITLE ABC TITLE ABC next WRITE prints here The Grouping Descriptor r The descriptors within are rep eated r times X I F A I F means I F is rep eated three times Thus the ab ove is equivalentto X I F I F I F A Grouping can b e nested X I I A A The ab ove is equivalentto X I I A I I A I I A A which in turn is equivalentto X I I A I A I I A I A I I A I A A A group with rep etition factor r has sp ecial meaning Example Computing means revisited PROGRAM Means IMPLICIT NONE REAL a b c REAL Am Gm Hm CHARACTERLEN PARAMETER FMT X AX A F READ a b c Am a b c Gm a b c Hm a b c WRITEFMT Input Data a a b b c c WRITEFMT Computed Results Arithmetic Mean Am Geometric Mean Gm Harmonic Mean Hm END PROGRAM Means Input Data a b c Computed Results Arithmetic Mean Geometric Mean Harmonic Mean Example Printamultiplication table PROGRAM MultiplicationTable IMPLICIT NONE INTEGER PARAMETER MAX INTEGER i j CHARACTERLEN FORMAT FORMAT X I A I A I DO i MAX WRITEFORMAT i j ij j MAX END DO END PROGRAM MultiplicationTable Matching Data Values with Edit Descriptors When a WRITE starts its format is scanned from left to right Edit descriptors are pro cessed until a descriptor that requires a data value ie I F E L and A is encoun tered Take next data item and use the encountered edit descriptor for printing the value The typ e of the value and the edit descriptor MUST match I for INTEGER F and E for REAL L for LOGICAL and A for CHARACTER After pro cessing a value go back to step This pro cess will continue until encounters the right parenthesis Matching Data Values with Edit Descriptors If the rightmost right parenthesis is reached If there is no data items left we are done If there are unpro cessed data items then the format edit descriptors will b e rescanned a If there is no grouping parenthesis in a format then start a new line and rescan the format from the very b eginning INTEGER A B C INTEGER D E F WRITE X I A B C D E F Output b If there are grouping parenthesis then nd the rightmost rightparenthesis in the format and its matching left parenthesis and start rescan there Note that any rep etition preceding this group is in eect INTEGER DIMENSION A INTEGER i DO i Ai i END DO WRITE X II I Ai i Output c Continue this pro cess until all data items are printed Example Printanarray on a single column PROGRAM SingleColumn IMPLICIT NONE INTEGER PARAMETER MAXSIZE INTEGER DIMENSIONMAXSIZEx INTEGER i CHARACTERLEN FMT DO i MAXSIZE Xi MODi i END DO FMT T A T A T A T AT I T I WRITEFMT Generated Table No Data xi i MAXSIZE END PROGRAM SingleColumn Generated Table No Data Example Printanarray six elements p er row PROGRAM SixPerRow IMPLICIT NONE INTEGER PARAMETER SIZE INTEGER DIMENSIONSIZEx INTEGER i CHARACTERLEN String String X A X A X I DO i SIZE xi MODi i END DO WRITEString Generated Table xi i SIZE END PROGRAM SixPerRow Generated Table Integer Input Editing Descriptors r Iw I INTEGER input w width of eld iethenumb er of p ositions m minimum numberofdigitstobeprinted r rep etition indicator I is equivalentto I I I The content in the next w p ositions is considered to b e an integer whose value go es into the corresp onding variable All blanks in the eld of w p ositions are IGNORED by default INTEGER A B C D READI A B C D A B C D A B C and D receives and resp ectively Real Input Editing Descriptors r Fwd F REAL input w width of eld iethenumb er of p ositions d numb er of digits to the right of the decimal p oint r rep etition indicator F is equivalentto F F F Three cases are p ossible If there is no decimal p oint in the eld the right most d p ositions are considered to the rightofthe decimal p oint REAL A B C READ F A B C A B C The rst eld is the second is and the third is Taking the last two digits to b e the fraction part A B and C are and If the input has a decimal p oint the actual value will b e taken and the d in Fwd has no eect REAL A B C READ F A B C A B C A B and C receives and You can use scientic notation by adding an E fol lowed by an exp onent The remaining part is han dled by the ab ovetwo rules based on if a decimal point presents REAL A B READ F A B E E A B The eld for A after removing spaces is E Since the edit descriptor is Fthe part is read as two decimal places and the actual value is Thus A receives and B has Logical Input Editing Descriptors r Lw L LOGICAL input w width of eld iethenumb er of p ositions r rep etition indicator L is equivalentto L L L The leftmost T or F in the eld yields the value
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages28 Page
-
File Size-