! Programa é uma variante do programa (2) da lista anterior mostando ! os nomes comecando pelo sobrenome !-------------------------------------------------------------------------- PROGRAM ALUNOS_INVERSO IMPLICIT NONE INTEGER :: MATRIC CHARACTER (LEN = 30) :: NOME, NOMINVERSO REAL :: NOTA1, NOTA2, NOTA3, MEDIA OPEN (10, FILE='ALUNOS.DAT') ! LEITURA DO ARQUIVO GERADO READ (10, 1) MATRIC, NOME, NOTA1, NOTA2, NOTA3, MEDIA 1 FORMAT(I8, A30, 4F4.1) DO WHILE (NOME .NE. "FIM") CALL INVERTE(NOME, NOMINVERSO) !chama a rotina para colocar o sobrenome na frente WRITE(*, 2) MATRIC, " ", NOMINVERSO, NOTA1, NOTA2, NOTA3, MEDIA 2 FORMAT(I8, A1, A30, 4F5.1) READ (10, 1) MATRIC, NOME, NOTA1, NOTA2, NOTA3, MEDIA END DO ENDFILE 10 STOP END PROGRAM ALUNOS_INVERSO SUBROUTINE INVERTE(NOM, NINV) CHARACTER (LEN = 30) :: NOM, NINV INTEGER N,M M = LEN_TRIM(NOM) N=M DO WHILE(NOM(N:N) .NE. " ") N = N-1 END DO NINV = NOM(N+1:M) // ", " // NOM(1:N) RETURN END SUBROUTINE INVERTE ********************************************************* !Programa que converte um número na forma de um array de dígitos na forma não numérica ! em um número inteiro program converte_array_num integer numero, n, i, dig_to_num character (len=1) digitos(10) write (*,*) "entre com o tamanho da string" read(*,*) n write (*,*) "entre com os digitos, um a um" read(*,*) (digitos(i), i=1,n) !converte o primeiro dígito numero = dig_to_num(digitos(1)) !converte os díogitos restantes e acumula o número do i=2, n numero = numero*10 + dig_to_num(digitos(i)) end do write (*,*) "O resultado é o número ", numero stop end program converte_array_num !-------------------------------------------- function dig_to_num (d) !Esta função converte um digito character em um dígito inteiro integer dig_to_num character (len=1) d if (d .eq. "1") then dig_to_num = 1 end if if (d .eq. "2") then dig_to_num = 2 end if if (d .eq. "3") then dig_to_num = 3 end if if (d .eq. "4") then dig_to_num = 4 end if if (d .eq. "5") then dig_to_num = 5 end if if (d .eq. "6") then dig_to_num = 6 end if if (d .eq. "7") then dig_to_num = 7 end if if (d .eq. "8") then dig_to_num = 8 end if if (d .eq. "9") then dig_to_num = 9 end if if (d .eq. "0") then dig_to_num = 0 end if return end function dig_to_num ******************************************************************************** !Programa que converte uma cadeia de dígitos numéricos em um número inteiro program converte_char_num integer numero, n, char_to_num character (len=10) numchar write (*,*) "entre com o numero" read(*,*) numchar n = LEN_TRIM(numchar) numero = char_to_num(numchar, n) write (*,*) "***n=", n write (*,*) "A conversao de ", numchar, "eh o número ", numero stop end program converte_char_num !-------------------------------------------- function char_to_num (numchar,n) !esta função converte uma cadei de caracteres numéricos em um número inteiro integer i, n, char_to_num, dig_n character (len=10) numchar character (len=1) dig_c char_to_num = 0 do i= 1, n !coloca i-esimo dígito de numchar como dígito character em dig_c dig_c = numchar(i:i) !converte este dig_c em um dígito numérico (dig_n) if (dig_c .eq. "1") then dig_n = 1 end if if (dig_c .eq. "2") then dig_n = 2 end if if (dig_c .eq. "3") then dig_n = 3 end if if (dig_c .eq. "4") then dig_n = 4 end if if (dig_c .eq. "5") then dig_n = 5 end if if (dig_c .eq. "6") then dig_n = 6 end if if (dig_c .eq. "7") then dig_n = 7 end if if (dig_c .eq. "8") then dig_n = 8 end if if (dig_c .eq. "9") then dig_n = 9 end if if (dig_c .eq. "0") then dig_n = 0 end if !acumula o dígito numérico ao número char_to_num = char_to_num*10 + dig_n end do return end function char_to_num