本帖最后由 东南子 于 2014-5-4 22:19 编辑
这个学期在学fortran,老师布置了一个进制转换的的作业,要求将十进制转换为十六进制。于是尝试做了一下,简单调试之后成功。
思路:
1、从键盘输入字符串
2、将字符串的每一个字母数字转换为0-16
3、将得到的数字转换为十进制
仅作为抛砖引玉,希望富友们能给出一点改进的建议。
代码如下:
[Fortran] 纯文本查看 复制代码 !=============Main Program==============
program main
implicit none
character(8) :: hex1 !分配字符长度,不然到后面只识别一个字符
integer :: dec_number
integer,external :: hex_to_dec
write(*,*)'Please type a Hex number:'
read(*,*)hex1
dec_number=hex_to_dec(hex1)
write(*,"('The input HEX is:',a10,'The output DEC is :',i10)")hex1,dec_number
stop
end program main
!=============Sub Program==============
function hex_to_dec(x)
character(8) :: x
integer :: hex_to_dec,sum,Length,i
integer,allocatable :: m(:)
character,allocatable :: hex(:)
length=len_trim(x)
if(mod(length,2) /= 0)write(*,*)'Please type a correct Hex number:'
allocate(hex(length))
allocate(m(length))
do i=1,length !从字符转化为十进制数值(0-16)
hex(i)=x(i:i)
if(48 <= ichar(hex(i)) .and. ichar(hex(i))<=57)then !判断是否是数字字符串
m(i)=ichar(hex(i))-48
elseif(97 <= ichar(hex(i)) .and. ichar(hex(i))<=102)then !判断是否是字母(a-f & A-F)
m(i)=ichar(hex(i))-87
elseif(65 <= ichar(hex(i)) .and. ichar(hex(i))<=70)then
m(i)=ichar(hex(i))-55
end if
end do
sum=0
do i=length,1,-1
sum=sum+m(i)*16**(4-i) !将Hex转换为Dec
end do
hex_to_dec=sum
end function hex_to_dec
!=============End of Program=============
!=====================================分隔符======================================
站长提醒下,查找到了相关的编辑符用法,粘贴如下:
1、Z EditingThe Z data edit descriptor transfers hexadecimal (base 16) values. It takes the following form:
Zw[.m]
The value of m (the minimum number of digits in the constant) must not exceed the value of w (the field width), unless w is zero. The m has no effect on input, only output.
The specified I/O list item can be of type integer, real, or logical.
2、H Editing
The H edit descriptor transfers data between the external record and the H edit descriptor itself. The H edit descriptor is a deleted feature in the Fortran Standard. Intel® Fortran fully supports features deleted in the Fortran Standard. An H edit descriptor has the form of a Hollerith constant, as follows: nHstring n
| Is an unsigned, positive default integer literal constant (with no kind parameter) indicating the number of characters in string (including blanks and tabs). The range of n is 1 through 2147483647 (2**31-1) on Intel® 64 architecture; 1 through 32767 (2**15-1) on IA-32 architecture. Actual useful ranges may be constrained by record sizes (RECL) and the file system. | string
| Is a string of printable ASCII characters. |
On input, the H edit descriptor transfers n characters from the external field to the edit descriptor. The first character appears immediately after the letter H. Any characters in the edit descriptor before input are replaced by the input characters.
3、O Editing The O data edit descriptor transfers octal (base 8) values. It takes the following form:
Ow[.m]
The value of m (the minimum number of digits in the constant) must not exceed the value of w (the field width), unless w is zero. The m has no effect on input, only output.
The specified I/O list item can be of type integer, real, or logical.
|