Fortran Coder

查看: 2301|回复: 4
打印 上一主题 下一主题

[输入输出] 递归函数运行,输入数据后,没报错但程序不动

[复制链接]

4

帖子

2

主题

0

精华

新人

计算固体力学研发

F 币
6 元
贡献
15 点
跳转到指定楼层
楼主
发表于 2022-10-8 11:17:41 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
1F 币
本帖最后由 xiejihong0306 于 2022-10-8 11:36 编辑

彭国论教材EX0829.F90,用递归函数实现阶乘。


可我遇到了下面这个问题,按照粘贴的源代码那样,运行后输入n!,程序没报错,也没输出结果,想了半天实在没看懂哪有问题,


如果可以的话,想请高人解惑,万分感谢

-----------------------------

补充:  不好意思题干描述有点问题,我输入的9 ,没加感叹号


[Fortran] 纯文本查看 复制代码
! NOTE:P209
!  A sample code that reveals the principle of recursive functions and the calculation process
      program ProgramName
      implicit none
      integer ExitKey

      integer(kind=4) :: n
      integer(kind=4), external :: fact
      write(unit=6, fmt="('N = ')") 
      read (unit=5, fmt=*) n
      write(unit=6, fmt="(I3,'! = ', I8)") n, fact(n)

! Avoid having the command window exit before the results are output
      write ( *, *) 'Press input any words to exit...'
      read ( *, *) ExitKey
      stop
      end program ProgramName


      recursive integer(kind=4) function fact(n) result(ans)
        implicit none
        integer(kind=4), intent(in) :: n
        integer(kind=4), save :: count = 1
        integer(kind=4) :: localcount, temp
        localcount = count 
        count = count + 1
        write(unit=6, fmt="(I2, 'th enter, n = ', I2)") localcount, n
        !  The IF-ELSE combination of multiple judgments only executes the first module that matches the condition
        !  and then jumps to END IF to leave the statement.
        if ( n < 0 ) then
          ans = -1
          write(unit=6, fmt="(I2, 'th exit, n = ', I2, 'ans = ', I8)") localcount, n, ans
          return
        else if ( n <=1 ) then
          ans = 1
          write(unit=6, fmt="(I2, 'th exit, n = ', I2, 'ans = ', I8)") localcount, n, ans
          return
        end if
        temp = n - 1
        ans = n * fact(temp)
        write(unit=6, fmt="(I2, 'th exit, n = ', I2, 'ans = ', I8)") localcount, n, ans
        return
      end function fact



















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

4

帖子

2

主题

0

精华

新人

计算固体力学研发

F 币
6 元
贡献
15 点
沙发
 楼主| 发表于 2022-10-8 11:36:47 | 显示全部楼层
布衣龙共 发表于 2022-10-8 11:25
输入 3 就行了。不要输入 3!

此外,可能存在递归IO占用问题,详解:

不好意思我题干描述有点问题,我输入的9 ,没加感叹号
回复

使用道具 举报

4

帖子

2

主题

0

精华

新人

计算固体力学研发

F 币
6 元
贡献
15 点
板凳
 楼主| 发表于 2022-10-8 11:42:10 | 显示全部楼层
Transpose 发表于 2022-10-8 11:17
gfortran的函数里面写输出会发生混乱,ifort应该没这个问题
[mw_shl_code=fortran,true]      integer(kind= ...

牛逼,分开写之后完美解决了,原来是gFortran输出混乱的问题,谢谢你!

--------------------------------

N =
5
1th enter, n =  5
2th enter, n =  4
3th enter, n =  3
4th enter, n =  2
5th enter, n =  1
5th exit, n =  1ans =        1
4th exit, n =  2ans =        2
3th exit, n =  3ans =        6
2th exit, n =  4ans =       24
1th exit, n =  5ans =      120
  5! =      120
Press input any words to exit...


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-5-11 12:56

Powered by Tencent X3.4

© 2013-2024 Tencent

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