! Main pt. NEWTON = Dubla Precizie
Program Mnew
implicit real(8) (a-h, o-z)

external SUB
common p	! parametru pt. F(x)
logical exist_p/.false./; character(80) text(-1:1), w*1
character(26) fname0, fname
integer width
logical new_rad
character(132) f_text(2), f_source


text(-2)=' ** F-derivat <1E-15:'
text(0) =' * Toleranta EPS s-a atins:'
text(1) =' ** Numarulul limita de iteratii atins:'
text(-1)=' ** Divergenta'

10 format(a, \)

f_source ='Dsub.f90'
call F_Expr(f_source, f_text)
write(*, '(1x, a)') trim(f_text(1))
write(*, '(1x, a)') trim(f_text(2))

print 10, ' Nume fisier rezultate (Fara extensie): '
read *, fname0
fname =trim(fname0)//'.rez'
open(1, file =fname, form ='formatted', status ='unknown')

print 10,' Functia f(x) include parametrul "p" (Y / char) ?: '
read'(a)',w
exist_p =(w =='Y' .or. w =='y')

new_rad =.false.
nrad =1

write(1, '(1x, a)') trim(f_text(1))
write(1, '(1x, a)') trim(f_text(2))

write(1, '(/, a, /)') ' Metoda NEWTON'

200 if(exist_p) then
print 10,' p=? : '
read*,p
endif

print 10,' Introdu eps, nlim : '
read *, eps, nlim0

print 10,' Introdu x0 si cod tiparire (0/#0): '
read *, x00, kod0

iwn =width(nrad); iwnlim0 =width(nlim0);
write(1, '(/, a, I<iwn>, a1)') ' Radacina nr.', nrad


if(exist_p) then
write(1,*) 'p:', p
endif

write(1,*) 'Aproximatia initiala:', x00
write(1,*) 'EPS:', EPS, '; NLIM:', nlim0
write(1,*)

x0 =x00; nlim=nlim0; kod=kod0
call NEWTON(SUB, x0, eps, nlim, rad, kod)

call sub(f, f1, rad)

if(kod ==-1) then
print '(/,a)', trim(text(1))
write(1,'(/,a,\)'), trim(text(1))
endif

if(kod /=2) then
print '(/a)', trim(text(kod))
write(1, '(/, a)'), trim(text(kod))
endif

if(kod ==0) then
write(1,*) ' Radacina =', rad,'; f(Radacina) =', f
write(*,*) ' Radacina =', rad,'; f(Radacina) =', f

else
write(1,*) ' Aproximatia curenta: x =', rad
write(1,*) ' f(x) = ', f

write(*,*) ' Aproximatia curenta: x =', rad
write(*,*) ' f(x) = ', f
endif

iwnlim =width(nlim)
write(1, '(a, I<iwnlim>)') '  Numar de iteratii = ', nlim

! Alta radacina:
print*
print 10,' Alta radacina ? (Y/ char): '
read '(a)',w
new_rad =w=='Y' .or. w =='y'

if(new_rad) then
nrad =nrad+1
goto 200
endif

close(1)
END program Mnew

