需要 FORTRAN 77 程序方面的帮助
我正在尝试编写一个程序来求解二次方程。如果 (B**B-4*A*C) 的值为 0 或负数,则应该立即写“方程的根是复数”,但是如果是积极的,则应进行评估。看来我的逻辑是错误的,因为无论我给 A、B 和 B 赋予什么值, C,我不断收到“方程的根很复杂”。请参阅下面的代码和结果。谢谢。
PROGRAM QUADEQN
INTEGER A,B,C
REAL D,X,Y,Q
D=(B**2-4*A*C)
Q=SQRT(D)
READ(*,5)A
READ(*,6)B
READ(*,7)C
IF(B**2-4*A*C)10,15,20
X=(-B+Q)/(2*A)
Y=(-B-Q)/(2*A)
20 WRITE(*,25)X,Y
5 FORMAT(I2)
6 FORMAT(I2)
7 FORMAT(I2)
10 WRITE(*,*)'THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX'
15 WRITE(*,*)'THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX'
25 FORMAT(/,'THE ROOTS OF THE EQN ARE',1X,F8.4,'AND',1X,F8.4)
STOP
END
结果
D:\Postgraduate\Programming\FORTRAN>gfortranquad.f
D:\Postgraduate\Programming\FORTRAN>a.exe 8 3 2 二次方程的根很复杂 二次方程的根是复数
D:\Postgraduate\Programming\FORTRAN>
I am trying to write a program to solve a quadratic equation.If the value of (B**B-4*A*C) is 0 or negative, it should immediately write that "The roots of the equation is complex", but if positive, it should evaluate. It seems my logic is faulty cos no matter what values I give for A,B & C, I keep getting "The roots of the equation are complex". Please see code and results below. Thanks.
PROGRAM QUADEQN
INTEGER A,B,C
REAL D,X,Y,Q
D=(B**2-4*A*C)
Q=SQRT(D)
READ(*,5)A
READ(*,6)B
READ(*,7)C
IF(B**2-4*A*C)10,15,20
X=(-B+Q)/(2*A)
Y=(-B-Q)/(2*A)
20 WRITE(*,25)X,Y
5 FORMAT(I2)
6 FORMAT(I2)
7 FORMAT(I2)
10 WRITE(*,*)'THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX'
15 WRITE(*,*)'THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX'
25 FORMAT(/,'THE ROOTS OF THE EQN ARE',1X,F8.4,'AND',1X,F8.4)
STOP
END
RESULT
D:\Postgraduate\Programming\FORTRAN>gfortran quad.f
D:\Postgraduate\Programming\FORTRAN>a.exe
8
3
2
THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX
THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX
D:\Postgraduate\Programming\FORTRAN>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
哇,我已经 20 多年没有见过计算 GOTO 了。
他们不可能还在教人们如何用这种方式编写 FORTRAN,不是吗?
我会使用更现代的风格,如下所示:
Wow, I haven't seen a computed GOTO in more than 20 years.
They can't possibly still be teaching people how to write FORTRAN this way, are they?
I'd use a more modern style, like this:
以更现代的方式编写。根据您的喜好修改字符串。
Written in a little more modern way. Modify the strings to your liking.
正如 duffymo 所说,在从用户读取
A
、B
和C
之前,您正在评估D
。最后我检查了 FORTRAN 不具备读取用户思想的通灵能力。实际上它通常完全忽略用户的意愿。只是在开玩笑。将
D=(B**2-4*A*C)
移至READ
语句之后,并根据 FORTAN 90Like duffymo said, you are evaluating
D
beforeA
,B
, andC
are read from the user. Last I checked FORTRAN does not have psychic abilities to read the minds of users. Actually it usually completely ignores the wishes of the user. Just kidding.Move the
D=(B**2-4*A*C)
to after theREAD
statements, and modernize the style according to FORTAN 90程序的另一个问题是,一旦执行了第 20 行,它将继续执行下一个可执行语句,在本例中是第 10 行,然后是第 15 行。这就是为什么您会得到“二次方程的根是复数二次方程的根是复数”。您可以通过在 STOP 之前使用 CONTINUE 语句并使用 GOTO 到达那里来解决此问题,但最好使用上面建议的方法之一。
Another issue with your program is that once it has executed line 20, it will then go on to execute the next executable statement, which in this case is line 10, followed by 15. That's why you get "THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX". You could fix this by using a CONTINUE statement just before STOP, and using GOTO to get there, but it would be much better to use one of the approaches suggested above.