如何以相反的顺序(从最后到第一个)将字符从字符变量写入整数变量?
How to write characters in reverse order ( from last to first ) from character variable into integer variable?
我的 IDE 是:CodeBlocks 20.03 ( MinGW 9.3.0 )
我的代码是:
function fun_conversion( n_numb_tmp, n_base_tmp ) result( data_tmp )
integer, intent(in) :: n_numb_tmp
integer, intent(in) :: n_base_tmp
integer :: data_tmp
integer :: n_div, n_div_res
character(1) :: char_01
character(4) :: char_02
n_div = 0
n_div = n_numb_tmp / n_base_tmp
write(char_01,'(i0)') ( n_div * n_base_tmp ) - n_numb_tmp
char_02 = char_01
do while ( n_div /= 0 )
n_div_res = n_div
n_div = n_div / n_base_tmp
write(char_01,'(i0)') n_div_res - ( n_div * n_baza_tmp )
char_02 = trim(char_02) // char_01
end do
! data_tmp = ?????
end function fun_conversion
如何在变量数据tmp中输入从最后一个余数到第一个余数的所有整数除法余数?算法在图片附件
如果你只需要反转一个固定长度的字符串,你可以这样做
module foo_m
implicit none
contains
integer function str2reverted_int(str) result(res)
character(*), intent(in) :: str
character(len(str)) :: rev_str
integer :: i, n
n = len(str)
rev_str(1:1) = str(n:n)
do i = 2, n
rev_str(i:i) = str(n+1-i:n+1-i)
end do
read (rev_str, *) res
end function
end module
program main
use foo_m
implicit none
print *, str2reverted_int('1234') ! -> '4321'
print *, str2reverted_int('12345') ! -> '54321'
end program
我的 IDE 是:CodeBlocks 20.03 ( MinGW 9.3.0 )
我的代码是:
function fun_conversion( n_numb_tmp, n_base_tmp ) result( data_tmp )
integer, intent(in) :: n_numb_tmp
integer, intent(in) :: n_base_tmp
integer :: data_tmp
integer :: n_div, n_div_res
character(1) :: char_01
character(4) :: char_02
n_div = 0
n_div = n_numb_tmp / n_base_tmp
write(char_01,'(i0)') ( n_div * n_base_tmp ) - n_numb_tmp
char_02 = char_01
do while ( n_div /= 0 )
n_div_res = n_div
n_div = n_div / n_base_tmp
write(char_01,'(i0)') n_div_res - ( n_div * n_baza_tmp )
char_02 = trim(char_02) // char_01
end do
! data_tmp = ?????
end function fun_conversion
如何在变量数据tmp中输入从最后一个余数到第一个余数的所有整数除法余数?算法在图片附件
如果你只需要反转一个固定长度的字符串,你可以这样做
module foo_m
implicit none
contains
integer function str2reverted_int(str) result(res)
character(*), intent(in) :: str
character(len(str)) :: rev_str
integer :: i, n
n = len(str)
rev_str(1:1) = str(n:n)
do i = 2, n
rev_str(i:i) = str(n+1-i:n+1-i)
end do
read (rev_str, *) res
end function
end module
program main
use foo_m
implicit none
print *, str2reverted_int('1234') ! -> '4321'
print *, str2reverted_int('12345') ! -> '54321'
end program