0

这是一个执行基本转换的简单程序。我尝试使用循环和指令打印出值:

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
4

1 回答 1

1

如果您将过程(子例程和函数)放入模块中,然后use将模块放入需要这些过程的任何程序或过程中,则更容易。这会自动使接口显式。您不必编写接口...更少的工作,更少的错误机会。所以:

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)

修复这些可能会使您的程序正常工作。这至少是第一步。

于 2014-06-16T06:49:08.513 回答