Fortran Coder

查看: 4043|回复: 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] 纯文本查看 复制代码
01! NOTE:P209
02!  A sample code that reveals the principle of recursive functions and the calculation process
03      program ProgramName
04      implicit none
05      integer ExitKey
06 
07      integer(kind=4) :: n
08      integer(kind=4), external :: fact
09      write(unit=6, fmt="('N = ')")
10      read (unit=5, fmt=*) n
11      write(unit=6, fmt="(I3,'! = ', I8)") n, fact(n)
12 
13! Avoid having the command window exit before the results are output
14      write ( *, *) 'Press input any words to exit...'
15      read ( *, *) ExitKey
16      stop
17      end program ProgramName
18 
19 
20      recursive integer(kind=4) function fact(n) result(ans)
21        implicit none
22        integer(kind=4), intent(in) :: n
23        integer(kind=4), save :: count = 1
24        integer(kind=4) :: localcount, temp
25        localcount = count
26        count = count + 1
27        write(unit=6, fmt="(I2, 'th enter, n = ', I2)") localcount, n
28        !  The IF-ELSE combination of multiple judgments only executes the first module that matches the condition
29        !  and then jumps to END IF to leave the statement.
30        if ( n < 0 ) then
31          ans = -1
32          write(unit=6, fmt="(I2, 'th exit, n = ', I2, 'ans = ', I8)") localcount, n, ans
33          return
34        else if ( n <=1 ) then
35          ans = 1
36          write(unit=6, fmt="(I2, 'th exit, n = ', I2, 'ans = ', I8)") localcount, n, ans
37          return
38        end if
39        temp = n - 1
40        ans = n * fact(temp)
41        write(unit=6, fmt="(I2, 'th exit, n = ', I2, 'ans = ', I8)") localcount, n, ans
42        return
43      end function fact



















最佳答案

查看完整内容

gfortran的函数里面写输出会发生混乱,ifort应该没这个问题 不要同时输出即可
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

174

帖子

2

主题

1

精华

大师

Vim

F 币
1061 元
贡献
497 点

规矩勋章

沙发
发表于 2022-10-8 11:17:42 | 只看该作者
本帖最后由 Transpose 于 2022-10-8 11:28 编辑

gfortran的函数里面写输出会发生混乱,ifort应该没这个问题
[Fortran] 纯文本查看 复制代码
1integer(kind=4) :: n
2integer(kind=4), external :: fact
3integer::f
4write(unit=6, fmt="('N = ')")
5read (unit=5, fmt=*) n
6f=fact(n)
7write(unit=6, fmt="(I3,'! = ', I8)") n, f

     不要同时输出即可

评分

参与人数 1F 币 +1 收起 理由
布衣龙共 + 1 很给力!

查看全部评分

回复

使用道具 举报

54

帖子

0

主题

0

精华

实习版主

F 币
654 元
贡献
214 点

元老勋章新人勋章

QQ
板凳
发表于 2022-10-8 11:25:18 | 只看该作者
本帖最后由 布衣龙共 于 2022-10-8 11:27 编辑

输入 3 就行了。不要输入 3!

此外,可能存在递归IO占用问题,详解:
http://recursiveio.w.fcode.cn/
回复

使用道具 举报

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 点
5#
 楼主| 发表于 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, 2025-4-29 09:18

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

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