Fortran Coder

查看: 14906|回复: 5
打印 上一主题 下一主题

[讨论] Fortran实现进制转换

[复制链接]

3

帖子

1

主题

0

精华

入门

F 币
35 元
贡献
22 点
QQ
跳转到指定楼层
楼主
发表于 2014-5-3 15:50:00 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 东南子 于 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.






评分

参与人数 1F 币 +9 贡献 +9 收起 理由
fcode + 9 + 9 很给力!

查看全部评分

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

3

帖子

1

主题

0

精华

入门

F 币
35 元
贡献
22 点
QQ
沙发
 楼主| 发表于 2014-5-4 22:12:42 | 显示全部楼层
fcode 发表于 2014-5-3 18:25
楼主很好学,乐于动脑筋,赞一个。

有两个问题与楼主分享。

嗯,学习了。又学习一个知识点,查了一下document,了解到了Z编辑符还有H编辑符等。
站长select case那段确实更好理解了。

3

帖子

1

主题

0

精华

入门

F 币
35 元
贡献
22 点
QQ
板凳
 楼主| 发表于 2014-5-4 22:29:36 | 显示全部楼层
vvt 发表于 2014-5-4 06:51
楼主的代码如果输入2位十六进制,结果与 read( hex1 , '(Z8)' ) dec_number 的不同哦。是不是顺序有问题? ...

应该是删掉:
write(*,"('The input HEX is:',a10,'The output DEC is :',i10)")hex1,dec_number
就行了,这行好像是重复了的,并且放错位置了。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-4 23:58

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表