01 基本格式與變量聲明
格式
固定格式(Fixed Format):Fortran77 程序需要滿足一種特定的格式要求,具體形式參考教材
自由格式(Free Format):Fortran90之后程序書寫形式更為自由
數據類型
Fortran 95包含的數據類型
| 數據類型 | 關鍵詞 | C中對應 |
|---|---|---|
| 整型 | integer | int/long ... |
| 浮點型 | real | float/double ... |
| 復數 | complex | <complex.h> |
| 字符/字符串 | character | char ... |
| 邏輯判斷 | logical | bool ... |
| 自定義結構體 | type() | struct |
表達式與邏輯運算
!Fortran 95支持邏輯符號
+、-,*、/:加減乘除
** :乘方
==:等於
/=:不等於
>:大於
>=:大於等於
<:小與
<=:小於等於
| 邏輯判斷式 | 含義 |
|---|---|
| .AND. | 且,兩邊都成立時,表達式成立 |
| .OR. | 或 |
| .NOT. | 非 |
| .EQV. | 兩邊相等時為true |
| .NEQV. | 兩邊不等時為true |
注意:浮點型的邏輯判斷,需要考慮有效位數截斷的問題,例如,2.0000001不等於2,需要使用\(abs(a-2)<2e-5\),這種方式來作判定
代碼結構
Fortran語言不區分大小寫
program main !自定義程序名稱 main
write(*,*) "Hello World" !輸出字符
stop !程序停止
end !main程序結束
輸出與輸入
輸出
write(*,*) "Hello World" !輸出字符
write(*,*)中,第一個*表示默認輸出位置,第二個*表示默認輸出格式,完整表述為以下格式
write(unit=6,fmt=*) "Hello World"
unit=6 表述輸出至屏幕,等效於unit=*。
輸入
program main
integer a,b,c
write(*,*) "please input 3 number"
read(*,*) a,b,c
write(*,*) a+b+c
end
read(*,*)與write(*,*)表示意義基本一致,read(*,*)等效於
read(unit=5,fmt=*) a,b,c
unit=5 表示由鍵盤輸入,等效於unit=*。
格式化輸入與輸出
《Fortran95教程》4-4節
program main
integer a
real b
complex c
logical d
character(len=20) e
a=10
b=12.34
c=(1,2)
d=.true.
e="Fortran"
write(*,"(1X,I5)") a
write(*,"(1X,F6.3)") b
write(*,"(1X,F4.1,F4.1)") c
write(*,"(1X,L3)") d
write(*,"(1X,A10)") e
end
聲明常規數據類型
聲明整型數據進行運算
program main
integer a,b
a=3
b=4
write(*,*) "a=",a
write(*,*) "b=",b
write(*,*) "a+b=",a+b
write(*,*) "a-b=",a-b
write(*,*) "a*b=",a*b
write(*,*) "a/b=",a/b
write(*,*) "a**b=",a**b
stop
end
注意整型除法的截斷問題
integer默認使用長整型進行聲明,可以自定義數據類型存儲長度,例如:
integer(kind=4) a
定義整型變量存儲空間大小為4。
聲明浮點型數據進行運算
program main
real :: a ,b
a=2.5
b=3.5
write(*,*) "a=",a
write(*,*) "b=",b
write(*,*) "a+b=",a+b
write(*,*) "a-b=",a-b
write(*,*) "a*b=",a*b
write(*,*) "a/b=",a/b
write(*,*) "a**b=",a**b
stop
end
real自定義數據長度同上
real(kind=8) a
聲明復數型數據進行運算
program main
complex :: a ,b
a=(1,1)
b=(1,-1)
write(*,*) "a=",a
write(*,*) "b=",b
write(*,*) "a+b=",a+b
write(*,*) "a-b=",a-b
write(*,*) "a*b=",a*b
write(*,*) "a/b=",a/b
stop
end
復數運算符合相應復數運算規則
聲明字符與字符串
聲明字符與字符串
program main
character :: a
character(20) :: b
a="H"
b="Hello World"
write(*,*) a
write(*,*) b
stop
end
字符串操作:數組索引、切片操作
program main
character(len=30) :: first,second
first="Good morning"
second=first(1:4)
write(*,*) first
write(*,*) second
first(6:)="evening"
write(*,*) first
write(*,*) first//second
stop
end
字符串相關函數
| 函數 | 功能 |
|---|---|
| CHAR(num) | 返回num對應的字符 |
| ICHAR(char) | 返回char字符對應的字符編號 |
| LEN(string) | 返回字符串string聲明空間長度 |
| LEN_TRIM(string) | 返回字符串除去尾端空格后的字符長度 |
| INDEX(string,key) | 返回key在string中第一次出現的位置 |
| Trim(string) | 返回清楚尾端空格后的字符串 |
program main
character(len=20) :: string
character(len=5) :: substring
string="Have a nice day!"
substring="nice"
write(*,*) ichar("A")
write(*,*) char(65)
write(*,*) len(string)
write(*,*) len_trim(string)
write(*,*) index(string,substring)
stop
end
邏輯變量
true與false分別為.true.、.false.
program main
logical a,b
a=.true.
b=.false.
write(*,*) "a=",a,"b=",b
stop
end
聲明變量注意事項
變量名前綴必須是英文字母
IMPLICIT命令
implicit integer(A,B,C) !A,B,C開頭變量視為整型
implicit integer(A-F) !A-F開頭變量視為整型
implicit real(M-P) !M-P開頭變量視為浮點型
implicit none !關閉默認類型,所有變量都需要聲明
其他變量聲明
常數聲明方法(PARAMETER)
program main
implicit none
real pi
parameter(pi=3.1415926535)
write(*,"(F6.4)") sin(pi/6)
end
還可以等效成以下聲明語句
real,parameter :: pi=3.14159
::表示類型申明的形容詞已經添加完畢,后面緊隨變量名稱
設置變量初始值
Fortran95 可以直接在變量聲明后進行賦值,而Fortran77需要使用DATA命令進行賦值
program main
implicit none
integer :: a=1
real :: b=2.0
complex :: c=(1,2.0)
character(len=20) :: str="Fortran95"
write(*,*) a,b,c,str
end
等價申明(EQUIVALENCE)
兩個以上的變量使用同一塊內存,類似於多個指針指向同一塊內存,可以用來精簡代碼,節省內存。
integer :: a,b
equivalence(a,b) !a,b兩個變量名共享同一塊內存
聲明語句在程序中的位置
聲明語句必須放置在可執行操作語句之前,也就意味着,程序中所有使用的變量必須首先聲明。
變量類型轉換
program main
implicit none
integer :: a=1,b=2
write(*,*) a/b
write(*,*) real(a)/b
end
real()函數可以將變量轉化為浮點型變量
自定義數據類型
結構體,type定義結構體,%獲得結構體數據成員
program main
implicit none
type:: person
character(len=30) :: name
integer :: age
real :: weight
end type
type(person) :: a
write(*,*) "name:"
read(*,*) a%name
write(*,*) "age:"
read(*,*) a%age
write(*,*) "weight:"
read(*,*) a%weight
write(*,100) a%name,a%age,a%weight
100 format(/,"name:",A10/,&
&/,"age:",I10/,&
&/,"weight:",F5.2)
end
