A. Fortran命令行啟動怎麼往主程序輸入參數
主要使用下面的三個函數實現:
第一個:
COMMAND_ARGUMENT_COUNT — Get number of command line arguments
Description:
COMMAND_ARGUMENT_COUNT returns the number of arguments passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Inquiry function
Syntax:
RESULT = COMMAND_ARGUMENT_COUNT()
Arguments:
None
Return value:
The return value is an INTEGER of default kind.
Example:
program test_command_argument_count
integer :: count
count = command_argument_count()
print *, count
end program test_command_argument_count
COMMAND_ARGUMENT_COUNT — Get number of command line arguments
Description:
COMMAND_ARGUMENT_COUNT returns the number of arguments passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Inquiry function
Syntax:
RESULT = COMMAND_ARGUMENT_COUNT()
Arguments:
None
Return value:
The return value is an INTEGER of default kind.
Example:
program test_command_argument_count
integer :: count
count = command_argument_count()
print *, count
end program test_command_argument_count
第二個:
GET_COMMAND_ARGUMENT — Get command line arguments
Description:
Retrieve the NUMBER-th argument that was passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])
Arguments:
NUMBER Shall be a scalar of type INTEGER and of default kind, NUMBER \geq 0
VALUE (Optional) Shall be a scalar of type CHARACTER and of default kind.
LENGTH (Optional) Shall be a scalar of type INTEGER and of default kind.
STATUS (Optional) Shall be a scalar of type INTEGER and of default kind.
Return value:
After GET_COMMAND_ARGUMENT returns, the VALUE argument holds the
NUMBER-th command line argument. If VALUE can not hold the argument, it
is truncated to fit the length of VALUE. If there are less than NUMBER
arguments specified at the command line, VALUE will be filled with
blanks. If NUMBER = 0, VALUE is set to the name of the program (on
systems that support this feature). The LENGTH argument contains the
length of the NUMBER-th command line argument. If the argument retrieval
fails, STATUS is a positive number; if VALUE contains a truncated
command line argument, STATUS is -1; and otherwise the STATUS is zero.
Example:
PROGRAM test_get_command_argument
INTEGER :: i
CHARACTER(len=32) :: arg
i = 0
DO
CALL get_command_argument(i, arg)
IF (LEN_TRIM(arg) == 0) EXIT
WRITE (*,*) TRIM(arg)
i = i+1
END DO
END PROGRAM
GET_COMMAND_ARGUMENT — Get command line arguments
Description:
Retrieve the NUMBER-th argument that was passed on the command line when the containing program was invoked.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])
Arguments:
NUMBER Shall be a scalar of type INTEGER and of default kind, NUMBER \geq 0
VALUE (Optional) Shall be a scalar of type CHARACTER and of default kind.
LENGTH (Optional) Shall be a scalar of type INTEGER and of default kind.
STATUS (Optional) Shall be a scalar of type INTEGER and of default kind.
Return value:
After GET_COMMAND_ARGUMENT returns, the VALUE argument holds the
NUMBER-th command line argument. If VALUE can not hold the argument, it
is truncated to fit the length of VALUE. If there are less than NUMBER
arguments specified at the command line, VALUE will be filled with
blanks. If NUMBER = 0, VALUE is set to the name of the program (on
systems that support this feature). The LENGTH argument contains the
length of the NUMBER-th command line argument. If the argument retrieval
fails, STATUS is a positive number; if VALUE contains a truncated
command line argument, STATUS is -1; and otherwise the STATUS is zero.
Example:
PROGRAM test_get_command_argument
INTEGER :: i
CHARACTER(len=32) :: arg
i = 0
DO
CALL get_command_argument(i, arg)
IF (LEN_TRIM(arg) == 0) EXIT
WRITE (*,*) TRIM(arg)
i = i+1
END DO
END PROGRAM
第三個:
GET_COMMAND — Get the entire command line
Description:
Retrieve the entire command line that was used to invoke the program.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND([COMMAND, LENGTH, STATUS])
Arguments:
COMMAND (Optional) shall be of type CHARACTER and of default kind.
LENGTH (Optional) Shall be of type INTEGER and of default kind.
STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:
If COMMAND is present, stores the entire command line that was used to
invoke the program in COMMAND. If LENGTH is present, it is assigned the
length of the command line. If STATUS is present, it is assigned 0 upon
success of the command, -1 if COMMAND is too short to store the command
line, or a positive value in case of an error.
Example:
PROGRAM test_get_command
CHARACTER(len=255) :: cmd
CALL get_command(cmd)
WRITE (*,*) TRIM(cmd)
END PROGRAM
GET_COMMAND — Get the entire command line
Description:
Retrieve the entire command line that was used to invoke the program.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND([COMMAND, LENGTH, STATUS])
Arguments:
COMMAND (Optional) shall be of type CHARACTER and of default kind.
LENGTH (Optional) Shall be of type INTEGER and of default kind.
STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:
If COMMAND is present, stores the entire command line that was used to
invoke the program in COMMAND. If LENGTH is present, it is assigned the
length of the command line. If STATUS is present, it is assigned 0 upon
success of the command, -1 if COMMAND is too short to store the command
line, or a positive value in case of an error.
Example:
PROGRAM test_get_command
CHARACTER(len=255) :: cmd
CALL get_command(cmd)
WRITE (*,*) TRIM(cmd)
END PROGRAM
最近在Fortran的數據傳遞設計上碰到了問題。現在略歸納下子程序間參數傳遞的實現辦法。
首先,Fortran subroutine
獨立地擁有屬於自己的變數申明。有說:Fortran的變數作用域只有兩類:一是全局作用域,二是子程序作用域,並且子程序內部不存在嵌套作用域。最後一
點很重要喲。舉例:在一個子程序里申明的變數,不能在與之並列的另外一個子程序裡面使用。即兩個子程序不能有變數重疊。即使在這個兩個子程序里都使用了同
名的某個變數,本質上講,在這兩個子程序里的同名變數也是沒任何聯系的。
那麼,如果需要在子程序之間傳遞變數(參數),怎麼辦呢?還要細說一點:傳參的結構有兩類:第一類,諸如主程序-子程序、外層子程序-內層子程序(子程序嵌套)之間的參數傳遞,另外一類,就是並列的兩個子程序(即它們之間不需要有關系,僅僅由於需求相同的變數)之間的參數傳遞。
第一類傳參結構的實現常見傳址調用。
第二類傳參結構的實現常見申明公共內存塊,將需求的相同變數申明成全局變數,那麼各個子程序自然都可以用了。注意,仍然是地址,所以子程序可以修改這些全局變數的。
其中,FORTRAN77的公共塊(全局變數申明)使用COMMON;FORTRAN90以後使用MODULE來包裝公共塊。
來看幾個問題:
一、以數組A中滿足某一條件的元素的編號構成新的數組B
do bi=1,bn
do ai=1,an
if(A(ai).eq.a0) then
B(bi)=ai
endif
enddo
enddo
二、以數組A的元素為編號,取數組B的元素進行其他運算
do bi=1,bn
do ai=1,an
if (bi.eq.A(ai)) then
fun(i)=fun(i)+fun2(bi)
endif
enddo
enddo