需要 FORTRAN 77 程序方面的帮助

发布于 2024-10-04 13:26:04 字数 904 浏览 3 评论 0原文

我正在尝试编写一个程序来求解二次方程。如果 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终弃我 2024-10-11 13:26:04

哇,我已经 20 多年没有见过计算 GOTO 了。

他们不可能还在教人们如何用这种方式编写 FORTRAN,不是吗?

我会使用更现代的风格,如下所示:

    PROGRAM QUADEQN
      INTEGER A,B,C
      REAL D,X,Y,Q
      READ(*,5)A
      READ(*,6)B
      READ(*,7)C
      D=(B**2-4*A*C)
      IF(D .LE. 0.0) THEN
  10  WRITE(*,*)'THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX'
      ELSE IF (D .EQ. 0.0) THEN
      WRITE(*,*)'WHAT SHOULD YOU SAY ABOUT THE ROOTS HERE?'
      ELSE
  25  FORMAT(/,'THE ROOTS OF THE EQN ARE',1X,F8.4,'AND',1X,F8.4)
      Q=SQRT(D)
      X=(-B+Q)/(2*A)
      Y=(-B-Q)/(2*A)
  20  WRITE(*,25)X,Y
      END IF
  5   FORMAT(I2)
  6   FORMAT(I2)
  7   FORMAT(I2)
      STOP
      END 

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:

    PROGRAM QUADEQN
      INTEGER A,B,C
      REAL D,X,Y,Q
      READ(*,5)A
      READ(*,6)B
      READ(*,7)C
      D=(B**2-4*A*C)
      IF(D .LE. 0.0) THEN
  10  WRITE(*,*)'THE ROOTS OF THE QUADRATIC EQUATION IS COMPLEX'
      ELSE IF (D .EQ. 0.0) THEN
      WRITE(*,*)'WHAT SHOULD YOU SAY ABOUT THE ROOTS HERE?'
      ELSE
  25  FORMAT(/,'THE ROOTS OF THE EQN ARE',1X,F8.4,'AND',1X,F8.4)
      Q=SQRT(D)
      X=(-B+Q)/(2*A)
      Y=(-B-Q)/(2*A)
  20  WRITE(*,25)X,Y
      END IF
  5   FORMAT(I2)
  6   FORMAT(I2)
  7   FORMAT(I2)
      STOP
      END 
一腔孤↑勇 2024-10-11 13:26:04

以更现代的方式编写。根据您的喜好修改字符串。

  PROGRAM roots
  !Purpose:
  ! This program solves for the roots of a quadratic equation of the
  ! form a*x**2 + b*x + c = 0. It calculates the answers regardless
  ! of the type of roots that the equation possesses.
  IMPLICIT NONE
  REAL :: a, b, c, discriminant, imag_part, real_part, x1, x2

  WRITE(*,*) 'This program solvenes for the roots of a quadratic'
  WRITE(*,*) 'equation of the form A * X**2 + B * X + C = 0.'
  WRITE(*,*) 'Enter the coefficients A, B and C:'
  READ(*,*)a,b,c
  WRITE(*,*) 'The coefficients A, B and C are: ',a,b,c

  discriminant = b**2 - 4.*a*c

  IF (discriminant>0.) THEN
        !there are two real roots, so ...
        x1 = (-b + sqrt(discriminant)) / (2.*a)
        x2 = (-b - sqrt(discriminant)) / (2.*a)
        WRITE(*,*) 'This equation has two real roots:'
        WRITE(*,*) 'X1 = ',x1
        WRITE(*,*) 'X2 = ',x2
  ELSE IF (discriminant<0.) THEN 
        !there are complex roots, so ...
        real_part = (-b)/(2.*a)
        imag_part = sqrt(abs(discriminant))/(2.*a)
        WRITE(*,*) 'This equation has comples roots:'
        WRITE(*,*) 'X1 = ',real_part,' +i ',imag_part
        WRITE(*,*) 'X2 = ',real_part,' -i ',imag_part
  ELSE 
        !here is one repeated root, so ...
        x1 = (-b)/(2.*a)
        WRITE(*,*) 'This equation has two identical real roots:'
        WRITE(*,*) 'X1 = X2 =',x1
  END IF
  END PROGRAM roots

Written in a little more modern way. Modify the strings to your liking.

  PROGRAM roots
  !Purpose:
  ! This program solves for the roots of a quadratic equation of the
  ! form a*x**2 + b*x + c = 0. It calculates the answers regardless
  ! of the type of roots that the equation possesses.
  IMPLICIT NONE
  REAL :: a, b, c, discriminant, imag_part, real_part, x1, x2

  WRITE(*,*) 'This program solvenes for the roots of a quadratic'
  WRITE(*,*) 'equation of the form A * X**2 + B * X + C = 0.'
  WRITE(*,*) 'Enter the coefficients A, B and C:'
  READ(*,*)a,b,c
  WRITE(*,*) 'The coefficients A, B and C are: ',a,b,c

  discriminant = b**2 - 4.*a*c

  IF (discriminant>0.) THEN
        !there are two real roots, so ...
        x1 = (-b + sqrt(discriminant)) / (2.*a)
        x2 = (-b - sqrt(discriminant)) / (2.*a)
        WRITE(*,*) 'This equation has two real roots:'
        WRITE(*,*) 'X1 = ',x1
        WRITE(*,*) 'X2 = ',x2
  ELSE IF (discriminant<0.) THEN 
        !there are complex roots, so ...
        real_part = (-b)/(2.*a)
        imag_part = sqrt(abs(discriminant))/(2.*a)
        WRITE(*,*) 'This equation has comples roots:'
        WRITE(*,*) 'X1 = ',real_part,' +i ',imag_part
        WRITE(*,*) 'X2 = ',real_part,' -i ',imag_part
  ELSE 
        !here is one repeated root, so ...
        x1 = (-b)/(2.*a)
        WRITE(*,*) 'This equation has two identical real roots:'
        WRITE(*,*) 'X1 = X2 =',x1
  END IF
  END PROGRAM roots
深海夜未眠 2024-10-11 13:26:04

正如 duffymo 所说,在从用户读取 ABC 之前,您正在评估 D。最后我检查了 FORTRAN 不具备读取用户思想的通灵能力。实际上它通常完全忽略用户的意愿。只是在开玩笑。

D=(B**2-4*A*C) 移至 READ 语句之后,并根据 FORTAN 90

Like duffymo said, you are evaluating D before A, B, and C 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 the READ statements, and modernize the style according to FORTAN 90

蓝海似她心 2024-10-11 13:26:04

程序的另一个问题是,一旦执行了第 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文