1、解二次方程 spa
C C quadraticroot.f C PROGRAM QUADRATIC 50 READ(*,10)A,B,C 10 FORMAT (3F3.1) IF (A.EQ.0.0) THEN WRITE(*,*) '二次項係數不能爲0!' STOP ENDIF D=B*B-4.0*A*C IF (D.LT.0.0) THEN WRITE(*,*) '該二次方程沒有實根!' STOP ENDIF X1=( B+SQRT(D))/(2.0*A) X2=(-B+SQRT(D))/(2.0*A) WRITE(*,20) X1,X2 20 FORMAT(1X, F6.2,10X,F6.2) GO TO 50 END PROGRAM QUADRATIC
50 READ(*,10)A,B,Ccode
它指出從代號爲*的輸入設備按照標號10的格式語句所提供的數據格式讀入三個數,分別送到A,B,C三個單元orm
*:是通道代碼,這裏*表明是標準輸入設備(即鍵盤)htm
在Fortran中有4個預約義的外部文件(設備):get
設備號0,5,6能夠經過OPEN語句鏈接到其餘文件上(重定向),當在程序中關閉設備號是O,5,6的外部文件後,若是下次還要使用這些設備號進行輸入輸出操做,這些設備號將自動鏈接到它們各自的缺省的設備上去。因此該句也能夠改成:it
50 READ(5,10)A,B,C io
10:格式語句的行號,即function
10 FORMAT (3F3.1)這一行form
C C helloworld.f C gfortran helloworld.f -o helloworld PROGRAM HELLOWORLD WRITE(*,10) 10 FORMAT('hello, world') OPEN(unit=16, file='results.txt') WRITE(16,10) END PROGRAM HELLOWORLD
該代碼同時向屏幕和文件results.txt寫hello,worldList
open(unit=16, file='results.txt')將定義設備號16爲文件results.txt。(文件的設備號最好使用10以上)
小於10可能被用做標準設備
Example 1: Formatted read, trap I/O errors, EOF, and I/O status:
READ( 1, 2, ERR=8, END=9, IOSTAT=N ) X, Y ... 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' STOP 9 WRITE( *, * ) 'EoF on 1' RETURN END
Example 2: Direct, unformatted read, trap I/O errors, and I/O status:
READ( 1, REC=3, IOSTAT=N, ERR=8 ) V ... 4 CONTINUE RETURN 8 WRITE( *, * ) 'I/O error # ', N, ', on 1' END
Example 3: List-directed read from keyboard:
READ(*,*) A, V or READ*, A, V
Example 4: Formatted read from an internal file:
CHARACTER CA*16 / 'abcdefghijklmnop' /, L*8, R*8 READ( CA, 1 ) L, R 1 FORMAT( 2 A8 )
Example 5: Read an entire array:
DIMENSION V(5) READ( 3, '(5F4.1)') V
Example 6: Namelist-directed read:
CHARACTER SAMPLE*16 LOGICAL NEW*4 REAL DELTA*4 NAMELIST /G/SAMPLE,NEW,DELTA ... READ(1, G) or READ(UNIT=1, NML=G) or READ(1, NML=G)
[例1.6] 分別求半徑R=1,3,12.5時的圓周長。此源程序由二部分組成:主程序和子程序。[e_121_06.f90]
! f95 e_121_06.f90 -o e_121_06 PROGRAM e_121_06 real :: r(3) data r /1, 3, 12.5/ do i=1,3 print *, '半徑=', r(i), '圓周長=', C(r(i)) end do END PROGRAM e_121_06 function C(radius) pi=acos(-1.0) C=2*pi*radius return end
求出全部100-999之間每一位上的數字的立方和等於自身的三位數。
!本程序可以求出全部100-999之間每一位上的數字的立方和等於自身的三位數。 PROGRAM SUM_OF_CUBES INTEGER A,B,C DO A = 1,9 DO B = 0,9 DO C = 0,9 IF (100*A + 10*B + C == A**3 + B**3 + C**3) & PRINT "(3I1)", A,B,C END DO END DO END DO END PROGRAM SUM_OF_CUBES