• Fortran语言程序设计01 函数与子例行程序


    目录

    函数例子:计算n个城市,d天的最高气温,最低气温,平均气温

    子例行程序改写上一个例子

    子例行程序排序冒泡算法

    综合问题:有些时候,气温统计丢失了,记录中用-999代替,如何计算?

     递归子程序

    语句函数


    函数例子:计算n个城市,d天的最高气温,最低气温,平均气温

    1. program example1 for chapter7
    2. implicit None
    3. external calavg,calmax,calmin
    4. integer,parameter::n=3,d=3
    5. real calavg,calmax,calmin
    6. real temp(n,d),tavg(n),tmax(n),tmin(n)
    7. integer i,j
    8. do i=1,n
    9. read(*,*),temp(i,:)
    10. end do
    11. do i=1,n
    12. tavg(i) = calavg(temp(i,:),d)
    13. tmax(i) = calmax(temp(i,:),d)
    14. tmin(i) = calmin(temp(i,:),d)
    15. end do
    16. write(*,*)'average is ',tavg
    17. write(*,*)'maxium is ',tmax
    18. write(*,*)'minium is ',tmin
    19. read(*,*)
    20. end program
    21. function calmax(a,m)
    22. implicit none
    23. integer m,i
    24. real a(m)
    25. real calmax
    26. calmax = a(1)
    27. do i = 2,m
    28. if (calmax.LT.a(i))then
    29. calmax = a(i)
    30. end if
    31. end do
    32. end function calmax
    33. function calmin(a,m)
    34. implicit none
    35. integer m,i
    36. real a(m)
    37. real calmin
    38. calmin = a(1)
    39. do i=2,m
    40. if(calmin.GT.a(i))then
    41. calmin = a(i)
    42. end if
    43. end do
    44. end function calmin
    45. function calavg(a,m)
    46. implicit none
    47. integer m,i
    48. real::a(m)
    49. real calavg
    50. real sum
    51. sum = 0
    52. do i = 1,m
    53. sum = sum + a(i)/m
    54. end do
    55. calavg = sum
    56. end function calavg

    子例行程序改写上一个例子

    1. program example2
    2. implicit none
    3. external calavg,calmax,calmin
    4. integer,parameter::n=3,d=3
    5. real temp(n,d),tavg(n),tmax(n),tmin(n)
    6. integer i,j
    7. do i=1,n
    8. read(*,*)temp(i,:)
    9. end do
    10. do i=1,n
    11. call calavg(temp(i,:),d,tavg(i))
    12. call calmax(temp(i,:),d,tmax(i))
    13. call calmin(temp(i,:),d,tmin(i))
    14. end do
    15. write(*,*)"ave",tavg
    16. write(*,*)"max",tmax
    17. write(*,*)"min",tmin
    18. read(*,*)
    19. end program
    20. subroutine calavg(a,d,x)
    21. implicit none
    22. integer d,i
    23. real a(d),sum,x
    24. sum = 0.
    25. do i=1,d
    26. sum = sum + a(i)
    27. end do
    28. x = sum/d
    29. end subroutine calavg
    30. subroutine calmax(a,d,x)
    31. implicit none
    32. integer d,i,j
    33. real a(d),x
    34. x = a(1)
    35. do i=2,d
    36. if(x.LT.a(i))then
    37. x = a(i)
    38. end if
    39. end do
    40. end subroutine calmax
    41. subroutine calmin(a,d,x)
    42. implicit none
    43. integer d,i,j
    44. real a(d),x
    45. x = a(1)
    46. do i=2,d
    47. if(x.GT.a(i))then
    48. x = a(i)
    49. end if
    50. end do
    51. end subroutine calmin

    子例行程序排序冒泡算法(引入)

    1. program example1
    2. implicit none
    3. external sort
    4. integer,parameter::N = 10
    5. real,dimension(N)::ain=(/0,9,6,3,5,8,2,1,4,7/),aout
    6. write(*,*),ain
    7. call sort(ain,aout)
    8. write(*,*)"after",aout
    9. read(*,*)
    10. end program
    11. subroutine sort(ain,aout)
    12. implicit none
    13. integer,parameter::N=10
    14. real ain(N),aout(N)
    15. real tmp
    16. integer i,j
    17. do i = 1,N-1
    18. do j = i+1,N
    19. if (ain(i).GE.ain(j))then
    20. tmp = ain(i)
    21. ain(i) = ain(j)
    22. ain(j) = tmp
    23. end if
    24. end do
    25. end do
    26. do i = 1,N
    27. aout(i) = ain(i)
    28. end do
    29. end subroutine sort

    综合问题:有些时候,气温统计丢失了,记录中用-999代替,如何计算?

    1. program example2
    2. implicit none
    3. external calavg,calmax,calmin
    4. integer,parameter::n=1,d=5
    5. real temp(n,d),tavg(n),tmax(n),tmin(n)
    6. integer i,j
    7. do i=1,n
    8. read(*,*)temp(i,:)
    9. end do
    10. do i=1,n
    11. call calavg(temp(i,:),d,tavg(i))
    12. call calmax(temp(i,:),d,tmax(i))
    13. call calmin(temp(i,:),d,tmin(i))
    14. end do
    15. write(*,*)"ave",tavg
    16. write(*,*)"max",tmax
    17. write(*,*)"min",tmin
    18. read(*,*)
    19. end program
    20. subroutine calavg(a,d,x)
    21. implicit none
    22. external correct
    23. integer d,i,m
    24. real a(d),aout(d),sum,x
    25. sum = 0.
    26. call correct(a,d,m,aout)
    27. do i=1,m
    28. sum = sum + aout(i)
    29. end do
    30. x = sum/m
    31. end subroutine calavg
    32. subroutine calmax(a,d,x)
    33. implicit none
    34. external correct
    35. integer d,i,j,m
    36. real a(d),aout(d),x
    37. call correct(a,d,m,aout)
    38. x = aout(1)
    39. !write(*,*),aout
    40. do i=2,m
    41. if(x.LT.aout(i))then
    42. x = aout(i)
    43. end if
    44. end do
    45. end subroutine calmax
    46. subroutine calmin(a,d,x)
    47. implicit none
    48. external correct
    49. integer d,i,j,m
    50. real a(d),aout(d),x
    51. call correct(a,d,m,aout)
    52. x = aout(1)
    53. write(*,*)m,"min!!"
    54. do i=2,m
    55. if(x.GT.aout(i))then
    56. x = aout(i)
    57. end if
    58. end do
    59. end subroutine calmin
    60. subroutine correct(a,d,m,aout)
    61. implicit none
    62. integer d,i,p,k
    63. real a(d),aout(d)
    64. integer m
    65. p = 1
    66. m = d
    67. do k=1,d
    68. aout(k) = a(k)
    69. end do
    70. do while(p.LT.m)
    71. do while(-999.NE.aout(p))
    72. p = p + 1
    73. if(p.GE.m)then
    74. exit
    75. end if
    76. end do
    77. if(p.GE.m)then
    78. exit
    79. end if
    80. do i=p,m
    81. aout(i) = aout(i+1)
    82. end do
    83. m = m - 1
    84. end do
    85. if(aout(d).EQ.-999)then
    86. m = m-1
    87. end if
    88. write(*,*),m,"!!*"
    89. write(*,*),aout,"!!**"
    90. end subroutine correct

     

     递归子程序

    用递归子例行程序做阶乘运算

    1. program example12
    2. implicit none
    3. integer f,n
    4. write(*,*)'input n'
    5. read*,n
    6. call fact(f,n)
    7. write(*,*)'factorial=',f
    8. read(*,*)
    9. end program
    10. recursive subroutine fact(f,i)
    11. implicit none
    12. integer f,i
    13. if(i.EQ.1)then
    14. f = 1
    15. else
    16. call fact(f,i-1)
    17. f = f * i
    18. end if
    19. end subroutine fact

    语句函数(支持整数)

    1. program example8
    2. integer f,x
    3. real g
    4. f(x) = 3*(0.1*x)**2+6*x + 5
    5. g(x) = 3*(0.1*x)**2+6*x + 5
    6. write(*,*),f(6),g(6)
    7. read(*,*)
    8. end program

     

    1. program example8
    2. F(i,j,k) = 3.1415926 /180. * (i+j/60.+k/3600.)
    3. write(*,*),F(60,20,30)
    4. read(*,*)
    5. end program

  • 相关阅读:
    一次ES检索的性能优化经验记录
    C++ 对 C 兼容是什么意思?
    神经网络算法处理器设计,神经网络是机器算法吗
    移动互联网进销存系统成就数智化升级
    【无标题】
    短视频矩阵系统源码---php搭建
    AutoLabel(自动标签)
    kotlin实现ArrayDeque
    谈谈 Spring 的过滤器和拦截器
    复制带随机指针的链表
  • 原文地址:https://blog.csdn.net/Chandler_river/article/details/125448139