2014-06-15 84 views
0

這是一個執行基本轉換的簡單程序。 我嘗試使用一個循環,並指令打印出值:如何從Fortran函數獲取值?

write (*,'(i4,a,a)') it," = ",baseConversion(it,base) 

出於某種原因,使用該行

program echeverria_4 

implicit none 

interface 
    function baseConversion(anumber,abase) 
    character(8) :: baseConversion 
    integer,intent(in) :: anumber, abase 
    end function baseConversion 
end interface 

    integer :: firstNumbers,base,it, numero 
    character(8),dimension(100) :: rangeNumbers 

    !Part A 
    write(*,*) "Project 4 Part A" 
    firstNumbers = 20 
    base = 11 

    write(*,'(i4,i4)') firstNumbers, base 

    do it = 1, firstNumbers 
     write (*,'(i4,a,a)') it," = ",baseConversion(it,base) 
    end do 

end program echeverria_4 


function trans(anumber) 
implicit none 
    character :: trans 
    integer,intent(in):: anumber 

    integer :: conversor1 = 48 
    integer :: conversor2 = 55 
    if (anumber >= 10) then 
     trans = char(anumber+conversor2) 
    else 
     trans = char(anumber+conversor1) 
    endif 
end function trans 


function baseConversion(anumber, abase) 

    implicit none 

    interface 
     function trans(anumber) 
      character :: trans 
      integer,intent(in):: anumber 
     end function trans 
    end interface 

     character(8):: baseConversion 
     integer,intent(in):: anumber,abase 

     character(8) :: leftmost 
     logical :: is_process_complete = .false. 
     integer :: remainder,division,localNumber 

     localNumber = anumber 

     do while(.not. is_process_complete) 
      !Step 1: Find the remainder 
      remainder = mod(localNumber,abase) 
      !Step 2: Divide the number by the base 
      division = localNumber/abase 
      if (division>0) then 
       localNumber = division 
       leftmost=trans(remainder)//leftmost 
      else 
       is_process_complete=.true. 
       leftmost=trans(remainder)//leftmost 
      end if 
     end do 

     write(baseConversion,'(a)') leftmost 
end function baseConversion 

回答

1

它更容易,如果你把你的程序(我不能得到的值子程序和函數)模塊化,然後模塊用於任何需要這些程序的程序或過程。這會自動使接口顯式化。您不必編寫界面......減少工作量,減少錯誤發生的機會。所以:

module MyModule 

contains 


function trans(anumber) 
implicit none 
    character :: trans 
    integer,intent(in):: anumber 

    integer :: conversor1 = 48 
    integer :: conversor2 = 55 
    if (anumber >= 10) then 
     trans = char(anumber+conversor2) 
    else 
     trans = char(anumber+conversor1) 
    endif 
end function trans 


function baseConversion(anumber, abase) 

    implicit none 

     character(8):: baseConversion 
     integer,intent(in):: anumber,abase 

     character(8) :: leftmost 
     logical :: is_process_complete = .false. 
     integer :: remainder,division,localNumber 

     localNumber = anumber 

     do while(.not. is_process_complete) 
      !Step 1: Find the remainder 
      remainder = mod(localNumber,abase) 
      !Step 2: Divide the number by the base 
      division = localNumber/abase 
      if (division>0) then 
       localNumber = division 
       leftmost=trans(remainder)//leftmost 
      else 
       is_process_complete=.true. 
       leftmost=trans(remainder)//leftmost 
      end if 
     end do 

     write(baseConversion,'(a)') leftmost 
end function baseConversion 

end module MyModule 


program echeverria_4 

use MyModule 

implicit none 

    integer :: firstNumbers,base,it, numero 
    character(8),dimension(100) :: rangeNumbers 

    !Part A 
    write(*,*) "Project 4 Part A" 
    firstNumbers = 20 
    base = 11 

    write(*,'(i4,i4)') firstNumbers, base 

    do it = 1, firstNumbers 
     write (*,'(i4,a,a)') it," = ",baseConversion(it,base) 
    end do 

end program echeverria_4 

當我編譯這與廣泛的錯誤/警告選項gfortran,我得到以下警告消息:

test99.f90:42.51: 

       leftmost=trans(remainder)//leftmost 
                1 
Warning: CHARACTER expression will be truncated in assignment (8/9) at (1) 
test99.f90:45.51: 

       leftmost=trans(remainder)//leftmost 
                1 
Warning: CHARACTER expression will be truncated in assignment (8/9) at (1) 

修復那些可能使你的工作方案。這至少是第一步。