[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
|