解二次方程但得到奇怪的错误 [英] Solving quadratic equation but getting weird errors

查看:135
本文介绍了解二次方程但得到奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Fortran中尝试我的第一个程序,试图解决一个二次方程。我有两倍和三倍检查我的代码,并没有看到任何错误。我不断在各个位置收到(1)中名称无效的字符和(1)中的无法分类的语句。代码有什么问题?

 !这个程序解决二次方程
!的形式ax ^ 2 + bx + c = 0.
!记录:
!名称:日期:备注:
! Damon Robles 4/3/10原始码

程序quad_solv
隐式无

!变量
REAL :: a,b,c
REAL ::区别,root1,root2,
COMPLEX :: comp1,comp2
CHARACTER(len = 1):: correct

!提示系数用户。
WRITE(*,*)该程序解决ax ^ 2 + bx + c = 0形式的二次方程
WRITE(*,*)。
WRITE(* *)请输入系数a,b和
WRITE(*,*)c,用逗号分隔:
READ(*,*)a,b,c
WRITE(*,*)是否正确:a =,a,b =,b
WRITE(*,*)c =,c,[Y / N]?
READ(*,*)正确
IF正确= N STOP
IF正确= Y THEN

!定义
判别式= b ** 2 - 4 * a * c

!计算
IF判别> 0 THEN
root1 =(-b + sqrt(discriminm))/(2 * a)
root2 =(-b - sqrt(discriminm))/(2 * a)
WRITE *,*)这个方程有两个实根。
WRITE(*,*)x1 =,root1
WRITE(*,*)x2 =,root2
IF (*,*)这个方程有一个双根。
WRITE(*,*)x1 = ,root1
IF判别< 0 THEN
comp1 =(-b + sqrt(discriminm))/(2 * a)
comp2 =(-b - sqrt(discriminationm))/(2 * a)
WRITE (*,*)x2 =,comp2

程序结束quad_solv
pre

解决方案

如何循环回到重做输入的另一个问题 - 演示Fortran循环功能的示例程序> = 90.循环显然是无限的 - 由IF语句控制的 exit 退出循环并使循环有限。还会显示 cycle 控件,如果遇到无效输入,则会使用该控件,否则会导致程序崩溃。 (例如,输入A作为第一次读取的输入,而不是数字。)在这种情况下,iostat变量获取非零值,IF语句激活循环,​​DO循环的其余部分为

 程序test_readdata 

real :: a,b, c
integer :: ReturnCode
character(len = 1):: correct

ReadData:do

write(*,'(This program (*,'()输入系数a,b和c,用逗号分隔:)', (*,*,iostat = ReturnCode)a,b,c
if(ReturnCode / = 0)cycle ReadData
write(*,*)这正确:a =,a,b =,b,c =,c
write(*,'(Enter Y or N:)',advance ='no')
read(*,*,iostat = ReturnCode)正确
if(ReturnCode / = 0)cycle ReadData
if(correct =='Y'.or。correct == 'y')exit ReadData

end do ReadData

stop

end program test_readdata

我的书推荐:

Fortran 95/2003由Metcalf,Reid和Cohen解释。

I'm attempting my first program in Fortran, trying to solve a quadratic equation. I have double and triple checked my code and don't see anything wrong. I keep getting "Invalid character in name at (1)" and "Unclassifiable statement at (1)" at various locations. What is wrong with the code?

! This program solves quadratic equations
! of the form ax^2 + bx + c = 0.
! Record:
! Name:             Date:     Notes:
! Damon Robles      4/3/10    Original Code

PROGRAM quad_solv
IMPLICIT NONE

! Variables
REAL :: a, b, c
REAL :: discrim, root1, root2,    
COMPLEX :: comp1, comp2
CHARACTER(len=1) :: correct 

! Prompt user for coefficients.
WRITE(*,*) "This program solves quadratic equations "
WRITE(*,*) "of the form ax^2 + bx + c = 0. "
WRITE(*,*) "Please enter the coefficients a, b, and "
WRITE(*,*) "c, separated by commas:"
READ(*,*) a, b, c
WRITE(*,*) "Is this correct: a = ", a, " b = ", b
WRITE(*,*) " c = ", c, " [Y/N]? "
READ(*,*) correct
IF correct = N STOP
IF correct = Y THEN

! Definition
discrim = b**2 - 4*a*c

! Calculations
IF discrim > 0 THEN
root1 = (-b + sqrt(discrim))/(2*a)
root2 = (-b - sqrt(discrim))/(2*a)
WRITE(*,*) "This equation has two real roots. "
WRITE(*,*) "x1 = ", root1
WRITE(*,*) "x2 = ", root2
IF discrim = 0 THEN
root1 = -b/(2*a)
WRITE(*,*) "This equation has a double root. "
WRITE(*,*) "x1 = ", root1
IF discrim < 0 THEN
comp1 = (-b + sqrt(discrim))/(2*a)
comp2 = (-b - sqrt(discrim))/(2*a)
WRITE(*,*) "x1 = ", comp1
WRITE(*,*) "x2 = ", comp2

PROGRAM END quad_solv

解决方案

Re the additional question of how to loop back to redo input -- an example program demonstrating loop features of Fortran >= 90. The loop is apparently infinite -- exit controlled by the IF statement exits the loop and makes the loop finite. Also shown is the cycle control, which is used if invalid input is encountered, which would otherwise crash the program. (For example, type "A" as input to the first read, instead of a number.) In this case, the iostat variable acquires a non-zero value, the IF statement activates the cycle, and the rest of the DO loop is skipped, so that the loop cycles anew.

program test_readdata

real :: a, b, c
integer :: ReturnCode
character (len=1) :: correct

ReadData: do

   write (*, '( "This program solves quadratic equations of the form ax^2 + bx + c = 0. " )' ) 
   write (*, '( "Please enter the coefficients a, b, and c, separated by commas: " )', advance='no' )
   read (*,*, iostat=ReturnCode) a, b, c
   if ( ReturnCode /= 0 ) cycle ReadData
   write (*,*) "Is this correct: a = ", a, " b = ", b, " c = ", c
   write (*, '( "Enter Y or N: " )', advance='no' )
   read (*,*, iostat=ReturnCode) correct
   if ( ReturnCode /= 0 ) cycle ReadData
   if ( correct == 'Y'  .or.  correct == 'y' )  exit ReadData

end do ReadData

stop

end program test_readdata

My book recommendation: Fortran 95/2003 explained by Metcalf, Reid and Cohen.

这篇关于解二次方程但得到奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆