program main
implicit none
external roots1
external roots2
integer::x,y
real::a,b,c,m,n
write(*,*) "please input two positive numbers"
read(*,*) x,y
a=real(x)
b=real(y)
call roots2(a,b,c)
write(*,*) "the first root:"
call roots1(a,c,m)
write(*,*) "the second root:"
call roots1(c,b,n)
end program
subroutine roots1(a,b,c)
implicit none
real,intent(in)::a,b
real::p,q
real,intent(out)::c
p=a
q=b
do
if(f1(p)*f1(q)<0)then
c=(p+q)/2
if(abs(f1(c))<0.0000001)then
write(*,100) c
100 format(F10.4)
exit
elseif(f1(p)*f1(c)<0)then
q=c
else
p=c
end if
end if
end do
contains
real function f1(x)
implicit none
real,intent(in)::x
f1=x**3-5*x+3
end function
end subroutine
subroutine roots2(a,b,c)
real,intent(in)::a,b
real::p,q
real,intent(out)::c
p=a
q=b
do
if(f2(p)*f2(q)<0)then
c=(p+q)/2
if(abs(f2(c))<0.0000001)then
c=(p+q)/2
exit
elseif(f2(p)*f2(c)<0)then
q=c
else
p=c
end if
end if
end do
contains
real function f2(x)
implicit none
real,intent(in)::x
f2=3*x**2-5
end function
end subroutine
QQ截图20190919154500.png (221.98 KB, 下载次数: 239)
欢迎光临 Fortran Coder (http://bbs.fcode.cn/) | Powered by Discuz! X3.2 |