! Main pentru AITKEN
implicit real(8) (a-h, o-z)

external G8
common p     ! parametru pt. G
logical exist_p/.false./
character(260) name, fname, text*80(-1:1)
integer width

character(132) g_text, g_source
logical new_rad, new_p, new_tol


text(-1)=' ** Numitor nul'
text(0) =' * Toleranta intalnita'
text(1) =' ** Numarul limita de iteratii atins'
!text(2) =' ** Proces stationar'

10 format(a, \)

g_source ='g8.f90'
call G_Expr(g_source, g_text, kod8)
write(*, '(1x, a)') trim(g_text)

print 10,' Nume fisier iesire (fara extensie): '
read '(a)', name
fname =trim(name)//'.rez'
open(1, file=fname, status='unknown')

! Write:
write(1, '(a)') ' Metoda AITKEN'
write(1, '(1x, a)') trim(g_text)

print 10,' Functia G contine parametrul "p"  (Y,y/char) ?: '
read '(a)',w
exist_p =(w.eq.'Y'.or.w.eq.'y')

nrad =1
new_p =.false.
new_tol =.true.

100 if(exist_p) then
print 10,' p =?: '
read *, p
write(1, '(/, a, 1pg14.7)') ' p = ',p
endif

200 continue
if(new_p) then
print '(a, \)',' Alt "xtol" si "nlim" ? (Y/ char): '
read '(a)',w
new_tol =w=='Y' .or. w =='y'
endif

if(new_tol) then
print 10,' xtol, nlim: '
read *, xtol, nlim0

if(nlim0 ==0) goto 500

endif

210 print 10,' x0, kod (0/#0): '
read *, x0, kod0

iwnrad =width(nrad)
write(1, '(/, a, I<iwnrad>, a1)') ' Radacina nr. ', nrad

x =x0
kod =kod0
nlim =nlim0

call AITKEN8(g8, x, xtol, nlim, rad, kod)

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

iwnlim =width(nlim)
print '(/, a, 1pg24.15, a, I<iwnlim>)', ' Rad =', rad,'; nr. iteratii =',nlim

write(1,'(/, a, 1pg24.15, a, I<iwnlim>)') ' Radacina =', rad,'; Numar iteratii = ', nlim
write(1, '(a, 1pg24.15)')                 ' g(Rad)   =', g8(rad)

! Alta radacina:
print*
print '(/, a, \)',' Alta radacina ? (Y/ char): '
read '(a)',w

new_rad =w=='Y' .or. w =='y'
new_p =.false.

IF(new_rad) then
nrad =nrad+1

	if(exist_p) then
	print '(a, \)',' Alt "p" ? (Y/ char): '
	read '(a)',w
	new_p =w=='Y' .or. w =='y'

	if(new_p) goto 100
	endif


print '(a, \)',' Alt "xtol" si "nlim" ? (Y/ char): '
read '(a)',w
new_tol =w=='Y' .or. w =='y'

if(new_tol) then
goto 200
else
goto 210
endif

ENDIF

close(1)

500 print '(/, a, a, /)', ' * Fisier de rezultate: ', trim(fname)

END
