这是一个执行基本转换的简单程序。 我尝试使用循环和指令打印出值:
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
答案 0 :(得分: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)
修复这些可能会使您的程序正常运行。这至少是第一步。