How can I make a repeat structure here?(Fortran)

Asked

Viewed 92 times

2

program equacao
implicit none
real a,b,c,q
real x3,x2,x1
character s,n,read,if

complex sqrt

100 print*, "Esse programa calcula equacoes do 2 grau"

write (*,*) "De os valores de a, b, c(respectivamente)"
read (*,*) a,b,c

q = (b**2)-4*a*c
x1 = (-b+sqrt(q) ) / (2*a)
x2 = (-b-sqrt(q) ) / (2*a)
x3 = (-b) / 2*a

if (q .lt. 0) then
 print*, "Nao existe x real"
endif

if (q .gt. 0) then
 print*, "delta=",q
 print*, "X' e X'' respectivamente=",x1,"e",x2
elseif (q .eq. 0) then
 print*, "So existe uma raiz"
 print*, x3
endif

write (*,*) "Calcular mais uma?(s/n)"
read (*,*) s,n
if (read .eq. s) then
   do while (if .eq. s)
      goto 100
   enddo
elseif (read .eq. n) then
    do while (if .eq. n)
       goto 200
    enddo
endif
stop
200 end

1 answer

2

inserir a descrição da imagem aqui

• If I understand the question, try remove do while within of if:

Obs. 1 : What you want with variables read & if I didn’t understand, so I removed those variables and replaced them s & n for sn.

Obs. 2 : Added the variable ierr for a verification of input different of the kind of variable expected in the readings a,b,c and sn, doing the redirecting to the pertinent question.

program equacao
implicit none

integer ierr
complex sqrt
character(1) sn
real a,b,c,q,x3,x2,x1

100 write(*,*)char(10),'Esse programa calcula equacoes do 2 grau'

write (*,*)char(10),"De os valores de a, b, c (respectivamente)"
read (*,*,iostat=ierr) a,b,c
if (int(ierr) .ne. int(0.)) go to 100

 q = (b**2)-4*a*c
x1 = (-b+sqrt(q) ) / (2*a)
x2 = (-b-sqrt(q) ) / (2*a)
x3 = (-b) / 2*a

if (q .lt. 0.) print*, char(10),"Nao existe x real"

if (q .gt. 0.) then
   print*,char(13),"delta = ",q
   print*,"X e X respectivamente = ",x1," e ",x2
elseif (q .eq. 0.) then
   print*, "So existe uma raiz"
   print*, x3
endif

300 write (*,*) char(10),"Calcular mais uma?(s/n)"

read (*,'(a1)') sn
if (int(ierr) .ne. int(0.)) go to 300

if (sn .eq. "s" .or. sn .eq. "S") then
   goto 100
elseif (sn .eq. "n" .or. sn .eq. "N") then
   goto 200
else 
   goto 300
endif

200 end program equacao

Browser other questions tagged

You are not signed in. Login or sign up in order to post.