[Fortran] 纯文本查看 复制代码
program decimal_to_binary
implicit none
integer :: decimal_number
character(len=32) :: binary_string
integer :: i, remainder
! 输入十进制数字
write(*, *) "请输入一个十进制数字:" !这里我输入了3
read(*, *) decimal_number
! 将十进制数字转换为二进制字符串
binary_string = ""
do i = 31, 0, -1
remainder = mod(decimal_number, 2) !remainder的值为1
binary_string = binary_string // char(remainder + 48) !这一行 binary_string 应该为“1”,但是实际为“”
decimal_number = decimal_number / 2
end do
! 输出二进制字符串
write(*, *) "对应的二进制字符串为:", binary_string
end program decimal_to_binary
[Fortran] 纯文本查看 复制代码
program main
implicit none
integer :: decimal_number
character(len=32) :: binary_string
read(*,*)decimal_number
write(binary_string,"(b32.32)")decimal_number
write(*,*)binary_string
end program main
[Fortran] 纯文本查看 复制代码
program decimal_to_binary
implicit none
integer :: decimal_number
character(len=32) :: binary_string
integer :: i, remainder
write(*, *) "请输入一个十进制数字:"
read(*, *) decimal_number
binary_string = ""
do i = 31, 0, -1
remainder = mod(decimal_number, 2)
binary_string = char(remainder + 48)//binary_string !//放前面
decimal_number = decimal_number / 2
if(decimal_number<1)exit !//需要中止循环条件
end do
write(*, *) "对应的二进制字符串为:", binary_string
end program decimal_to_binary