如何修复我的Fortran代码中的错误

时间:2015-11-02 13:06:05

标签: fortran

我不熟悉Fortran中的错误。你能解释一下这些错误是什么吗?

int.f90(18): error #5082: Syntax error, found IDENTIFIER 'K' when expecting one of: ( % : . = =>
do k=0.0 to m
---^

int.f90(19): error #5082: Syntax error, found IDENTIFIER 'Q' when expecting one of: ( % : . = =>
 do q=1 to m-1
----^

int.f90(20): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ) ,
 f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
----------------------------------------------^

int.f90(24): error #5082: Syntax error, found IDENTIFIER 'I' when expecting one of: ( % : . = =>
 do i=1 to m-1
----^

int.f90(4): error #6406: Conflicting attributes or multiple declaration of name.   [INT]
function int(f,a,b,int,m)
-------------------^

int.f90(9): error #6418: This name has already been assigned a data type.   [M]
integer:: i,m
------------^

int.f90(10): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute.   [PI]
real,parameter:: pi,pi=3.14
-----------------^

int.f90(10): error #6418: This name has already been assigned a data type.   [PI]
real,parameter:: pi,pi=3.14
--------------------^

int.f90(11): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute.   [EPS]
real,parameter:: eps,eps=1.89
-----------------^

int.f90(11): error #6418: This name has already been assigned a data type.   [EPS]
real,parameter:: eps,eps=1.89
---------------------^

int.f90(12): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute.   [E]
real,parameter:: e,e=1.602*((10)**(-19))
-----------------^

int.f90(12): error #6418: This name has already been assigned a data type.   [E]
real,parameter:: e,e=1.602*((10)**(-19))
-------------------^

int.f90(21): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
 end do
-^

int.f90(23): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
 end do
-^

int.f90(26): error #6410: This name has not been declared as an array or a function.   [F2]
 s=2*f2(t)+4*f2(t+h)
-----^

int.f90(27): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
 end do 
-^

这是我的代码:

function int(f,a,b,int,m)
implicite none
double precision f1,f2,a,b,m,int,s 
double precision h,t
integer:: k,q
integer:: i,m
real,parameter:: pi,pi=3.14
real,parameter:: eps,eps=1.89
real,parameter:: e,e=1.602*((10)**(-19))
a=0.0
b=pi
m=150
s=0.0
h=(b-a)/m
do k=0.0 to m
do q=1 to m-1
f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
end do
f2=((2*pi*(e**2))/eps)*f
end do
do i=1 to m-1
t=a+(i*h)
s=2*f2(t)+4*f2(t+h)
end do 
int=(h/3)*(s+f2(a)+f2(b)+4*f2(a+h))
print*,int
return
end function int

1 个答案:

答案 0 :(得分:3)

您的错误是:

  1. 每个do循环中的循环控件都是格式错误的。 (Fortran 2008 Cl.8.1.6.2 R818)

    DO循环被指定为(忽略可选语法):

    do variable=start, end
    

    请注意逗号而不是单词to

  2. 括号必须平衡。 (参见Fortran 2008 7.1.2.2 R701 (expr)

    每个左括号(必须具有相应的右括号)。分配f1f2的语句都有左括号而不是右括号。

  3. 在同一作用域单元中声明两次变量是错误的。 (Fortran 2008 Cl.16.3.1第3段,另见:Cl.5.2)

    • 要对变量进行delcare并对其进行初始化,只需使用其名称

      real, parameter :: pi=3.14
      
    • 您调用函数int和虚拟变量int。您需要为这些使用不同的名称。

    • 您将m同为integerdouble precision。您只能将其声明为一种类型,而不是两种。

  4. 值得注意的是:

    1. 修复从第一个开始的错误,因为后面的错误可能是由于编译器丢弃了早期的错误语句,并且在修复错误语句时会消失。
    2. do循环变量中的实际值在最新标准中被删除,应该在新代码中避免。 (Fortran 2008附件B.1 2(1))。
    3. 您的代码表明在Fortran中缺乏理解,您将从查找涵盖变量和循环基础知识的Fortran教程中获益匪浅。