Need assistance with a FORTRAN 77 program
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>
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
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
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
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.
精彩评论