我在FORTRAN的初学者级别。最近,我尝试设计一个简单的程序来计算排列和组合。它将有一个“菜单”供用户选择继续...
!/*-------MAIN PROGRAM STARTS-------*/
!PURPOSE: TO CALCULATE COMBINATIONS AND PERMUTATIONS
PROGRAM COMBINATION_PERMUTATION
IMPLICIT NONE
!DECLARATION OF VARIABLES
REAL N, K, FACT, COMBINATION, PERMUTATION
INTEGER CHOICE
!CHOICE MENU
CALL MENU
!USER INPUT OF CHOICE
READ(*,*) CHOICE
!EXECUTION OF CODE BASED ON USER'S CHOICE
SELECT CASE(CHOICE)
CASE(1)
!CALLS SUBROUTINE FOR COMBINATION
CALL COMBINATION_CALC (N, K, FACT, COMBINATION)
CASE(2)
!CALLS SUBROUTINE FOR PERMUTATION
CALL PERMUTATION_CALC (N, K, FACT, PERMUTATION)
CASE(3)
!EXITS THE PROGRAM
STOP
CASE DEFAULT
!INVALID CHOICE LEADS TO MENU BACK
WRITE(*,*) 'INVALID CHOICE! PLEASE CHOOSE AN OPTION TO CONTINUE'
CALL MENU
END SELECT
END PROGRAM COMBINATION_PERMUTATION
!/*-------MAIN PROGRAM ENDS-------*/
!/*-------SUB PROGRAMS START-------*/
!FUNCTION FOR FACTORIAL
FUNCTION FACT(N)
IMPLICIT NONE
REAL FACT, N
INTEGER P, I
P=1
DO I=1, N
P=P*I
END DO
FACT=P
RETURN
END FUNCTION
!SUBROUTINE FOR MENU
SUBROUTINE MENU
IMPLICIT NONE
WRITE(*,*) ' CHOOSE AN OPTION TO CONTINUE.. '
WRITE(*,*) ' 1. COMBINATION '
WRITE(*,*) ' 2. PERMUTATION '
WRITE(*,*) ' 3. EXIT PROGRAM '
RETURN
END SUBROUTINE MENU
!SUBROUTINE FOR COMBINATION
SUBROUTINE COMBINATION_CALC (N, K, FACT, COMBINATION)
IMPLICIT NONE
REAL N, K, FACT, COMBINATION
WRITE(*,*) ' WHAT IS THE VALUE OF N? '
READ(*,*) N
2 WRITE(*,*) ' WHAT IS THE VALUE OF K? '
READ(*,*) K
IF (K<0) THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
GO TO 2
ELSE IF (K>N)THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
GO TO 2
ELSE
END IF
COMBINATION=FACT(N)/(FACT(N-K)*FACT(N))
WRITE(*,*) ' HENCE, THERE ARE ', COMBINATION, ' WAYS TO ARRANGE. '
CALL MENU
RETURN
END SUBROUTINE COMBINATION_CALC
!SUBROUTINE FOR PERMUTATION
SUBROUTINE PERMUTATION_CALC (N, K, FACT, PERMUTATION)
IMPLICIT NONE
REAL N, K, FACT, PERMUTATION
WRITE(*,*) ' WHAT IS THE VALUE OF N? '
READ(*,*) N
3 WRITE(*,*) ' WHAT IS THE VALUE OF K? '
READ(*,*) K
IF (K<0) THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
GO TO 3
ELSE IF (K>N) THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
GO TO 3
ELSE
END IF
PERMUTATION=FACT(N)/FACT(N-K)
WRITE(*,*) ' HENCE THERE ARE', PERMUTATION,' WAYS TO ARRANGE. '
CALL MENU
RETURN
END SUBROUTINE PERMUTATION_CALC
!/*-------SUB PROGRAMS END-------*/
每当我输入输入1或2来选择要继续的案例时,我都会遇到运行时错误....
错误在弹出窗口中显示如下: -
RUN TIME ERROR
当需要程序时,尝试将参数编号为3的例程调用为实数(kind = 1)。
COMBINATION_CALC-文件combinationpermutation.for第62行[+0068] 第23行[+ 00e2]
中的main-in文件combinationpermutation.for据我所知,COMBINATION_CALC中的第三个参数是“FACT”,它是计算阶乘的函数。我无法理解错误。请指导我。提前谢谢..
更新
我已经使用yosukesabai给出的修复修复了RUN-TIME ERROR ...我的程序现在已经完成了..但是,INTEGER OVERFLOW对我来说是一个新问题。我已经将REAL声明更改为INTEGER(KIND = 4)..当N值和K值超过12时...组合和排列的结果答案会很大......我可以知道如何解决这个问题吗?
以下是我的程序的新代码......
!/*-------MAIN PROGRAM STARTS-------*/
!PURPOSE: TO CALCULATE COMBINATIONS AND PERMUTATIONS
!CREATED BY: RETHNARAJ RAMBABU
!DATE: 28/10/2011
PROGRAM COMBINATION_PERMUTATION
IMPLICIT NONE
!DECLARATION OF VARIABLES
INTEGER CHOICE, N, K
INTEGER(KIND=4) COMBINATION, PERMUTATION
INTEGER, external :: FACT
!CHOICE MENU
1 WRITE(*,*) ' CHOOSE AN OPTION TO CONTINUE.. '
WRITE(*,*) ' 1. COMBINATION '
WRITE(*,*) ' 2. PERMUTATION '
WRITE(*,*) ' 3. EXIT PROGRAM '
!USER INPUT OF CHOICE
READ(*,*) CHOICE
!EXECUTION OF CODE BASED ON USER'S CHOICE
SELECT CASE(CHOICE)
CASE(1)
!CALLS SUBROUTINE FOR COMBINATION
CALL COMBINATION_CALC (N, K, FACT, COMBINATION)
GO TO 1
CASE(2)
!CALLS SUBROUTINE FOR PERMUTATION
CALL PERMUTATION_CALC (N, K, FACT, PERMUTATION)
GO TO 1
CASE(3)
!EXITS THE PROGRAM
STOP
CASE DEFAULT
!INVALID CHOICE LEADS TO MENU BACK
WRITE(*,*) 'INVALID CHOICE! PLEASE CHOOSE AN OPTION TO CONTINUE'
GO TO 1
END SELECT
END PROGRAM COMBINATION_PERMUTATION
!/*-------MAIN PROGRAM ENDS-------*/
!/*-------SUB PROGRAMS START-------*/
!FUNCTION FOR FACTORIAL
FUNCTION FACT(N)
IMPLICIT NONE
INTEGER N, P, I, FACT
P=1
DO I=1, N
P=P*I
END DO
FACT=P
RETURN
END FUNCTION
!SUBROUTINE FOR COMBINATION
SUBROUTINE COMBINATION_CALC (N, K, FACT, COMBINATION)
IMPLICIT NONE
INTEGER N, K, FACT
INTEGER(KIND=4) COMBINATION
WRITE(*,*) ' WHAT IS THE VALUE OF N? '
READ(*,*) N
2 WRITE(*,*) ' WHAT IS THE VALUE OF K? '
READ(*,*) K
IF (K<0) THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
GO TO 2
ELSE IF (K>N)THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
GO TO 2
ELSE
END IF
COMBINATION=FACT(N)/(FACT(N-K)*FACT(K))
WRITE(*,*) ' HENCE, THERE ARE ', COMBINATION, ' WAYS TO ARRANGE. '
RETURN
END SUBROUTINE COMBINATION_CALC
!SUBROUTINE FOR PERMUTATION
SUBROUTINE PERMUTATION_CALC (N, K, FACT, PERMUTATION)
IMPLICIT NONE
INTEGER N, K, FACT
INTEGER(KIND=4)PERMUTATION
WRITE(*,*) ' WHAT IS THE VALUE OF N? '
READ(*,*) N
3 WRITE(*,*) ' WHAT IS THE VALUE OF K? '
READ(*,*) K
IF (K<0) THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE MORE THAN 0! '
GO TO 3
ELSE IF (K>N) THEN
WRITE(*,*) ' ERROR! VALUE OF K SHOULD BE LESS THAN N! '
GO TO 3
ELSE
END IF
PERMUTATION=FACT(N)/FACT(N-K)
WRITE(*,*) ' HENCE THERE ARE', PERMUTATION,' WAYS TO ARRANGE. '
RETURN
END SUBROUTINE PERMUTATION_CALC
!/*-------SUB PROGRAMS END-------*/
答案 0 :(得分:0)
靠近代码顶部,
PROGRAM COMBINATION_PERMUTATION
IMPLICIT NONE
!DECLARATION OF VARIABLES
REAL N, K, FACT, COMBINATION, PERMUTATION
INTEGER CHOICE
在这里你说FACT是真实的,所以fortran认为它是一个真正的变量。但您的意思是这是对您在同一文件中定义的函数的引用。所以修改如
!DECLARATION OF VARIABLES
REAL N, K, COMBINATION, PERMUTATION
INTEGER CHOICE
REAL, external :: FACT
外部是fortran的说法,它是一个你在程序块(程序,子程序或函数)之外的某个地方定义的函数
(如果您不在乎,请在此处忽略)
我注意到没有传递函数引用的意义。所以你可能想要停止传递函数,并直接从子程序combination_calc中调用它。在这种情况下,n,k,组合是子程序的三个参数,而在combination_calc中你将有real, external :: fact
。
答案 1 :(得分:0)
关于INTEGER溢出,声明变量P(在函数FACT中)REAL而不是INTEGER。 INTEGER(4)变量不能超过2^31
和12! < 2^31 < 13!
但请注意:REAL值也可能出现溢出(在我的计算机上35!
:34!=2.9523282E+38
)。使用DOUBLE PRECISION(REAL(8))时,溢出发生在171!
(170! = 7.2574156153+306
)