Fortran Coder

查看: 14894|回复: 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 很给力!

查看全部评分

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

1961

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1350 元
贡献
570 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 2014-5-3 18:25:26 | 只看该作者
楼主很好学,乐于动脑筋,赞一个。

有两个问题与楼主分享。

1. Fortran 有一个 Z 编辑符,可以很方便的进行十六进制与十进制的转换。(八进制和二进制也有类似的)(可参考下面我修改的代码,第11,12两行)
2. 我不建议直接在代码里写ASCII的值,48,57,97,65 这些数字,反正我是记不住。阅读这样的代码还需要翻看ASCII表。(如果按照我的代码来写,是不是更直观易读呢?)

最后祝福楼主五一快乐
[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
  read( hex1 , '(Z8)' ) dec_number
  write(*,"('The input HEX is:',a10,'The output DEC is :',i10)")hex1,dec_number
  stop
end program main
!=============Sub  Program==============
integer function hex_to_dec(x)
  character(8) :: x
  integer :: sum,Length,i , m
  character :: hex
  length=len_trim(x)
  if(mod(length,2) /= 0)write(*,*)'Please type a correct Hex number:'
  sum = 0
  do i=1,length !从字符转化为十进制数值(0-16)
    hex = x(i:i)
    Select Case( hex )
    Case ( '0':'9'  )
      m= ichar(hex) - ichar('0')
    Case( 'A':'F' )
      m=ichar(hex) - ichar('A') + 10
    Case( 'a':'f' )
      m=ichar(hex) - ichar('a') + 10
    End Select
    sum = sum + m*16**(4-i) !将Hex转换为Dec
  end do  
  hex_to_dec=sum
end function  hex_to_dec
!=============End  of Program=============

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
板凳
发表于 2014-5-4 06:51:59 | 只看该作者
楼主的代码如果输入2位十六进制,结果与 read( hex1 , '(Z8)' ) dec_number 的不同哦。是不是顺序有问题?

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
5#
 楼主| 发表于 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
就行了,这行好像是重复了的,并且放错位置了。

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
6#
发表于 2014-5-5 06:24:04 来自移动端 | 只看该作者
哈哈,我猜你们老师并不希望你们用z编辑符完成。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-4-25 11:37

Powered by Tencent X3.4

© 2013-2024 Tencent

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