为了使用FizzBuzz作为一种语言的介绍,我向FizzBuzz介绍了Fortran95:
对于3的倍数,打印Fort
,5's倍数打印Ran
。对于两者的倍数,请打印FortRan
。
特别是寻找风格和常见的实践批评。关于如何用标准的方式对这些值进行左对齐的提示也将受到赞赏。
以下代码为也以Ideone的形式呈现:
program fizzbuzz
implicit none
integer ::i
! Loop through integers 1 through 100
! Multiples of 3 print Fort
! Multiples of 5 print Ran
! Multiples of both print FortRan
! 15 is the lowest common multiple of 3, and 5
! and thus is a shortcut to FortRan
do i=1,100
if (MODULO(i, 15) == 0) then
write (*,'(A8)') 'FortRan'
else if (MODULO(i, 3) == 0) then
write (*,'(A8)') 'Fort'
else if (MODULO(i, 5) == 0) then
write (*,'(A8)') 'Ran'
else
write (*,'(I8)') i
end if
end do
end program fizzbuzz
发布于 2014-09-12 07:48:20
据我所知,没有实质性的改进。
这是很清楚的,而且这个程序的规模很小。因此,任何关于未来扩展性的想法都是无关紧要的。
挑剔:
100是一个神奇的数字,只能通过评论、使用fizzBuzzLimit
或任何fortran的案例约定来解释。
这个写函数看起来很奇怪(免责声明:我不是一个fortran程序员),也许把它封装在一个助手函数中(您在fortran中有这些函数吗?)所以你的代码读起来更干净。
发布于 2014-10-12 07:28:48
没有什么可批评的。如前所述,您还可以使用i0格式描述符来调整整数。我也会准备主程序。
由于Fortran实际上代表公式翻译,我认为使用拆分为for和Tran会更有吸引力。如果使用Trans,那么甚至会用数字值的字母替换数字。我还在Fortran代码的文档中使用了also,并且认为它的语法相当平易近人。因此,使用它通常是无害的。最后,将功能应用到例程中,将例程放入模块是很好的实践。不过,对于这样一个小例子来说,这可能有点过头了,我认为用它来说明问题也是很有意义的。因此,我的建议是:
!> A module that implements the FizzBuzz series.
module fizzbuzz_module
use iso_fortran_env, only: output_unit
implicit none
private
public :: write_fizzbuzz
contains
!> Write a series of fizzbuzz to unit.
!!
!! This writes all the numbers from lb to ub to unit, but replaces:
!! - multiples of 3 by For
!! - multiples of 5 by Tran
!! - multiples of both, 5 and 3 by ForTran
subroutine write_fizzbuzz(ub, lb, unit)
!> Upper bound up to which the series is to be written.
integer, intent(in) :: ub
!> Lower bound, where the series should start, defaults to 1.
integer, optional, intent(in) :: lb
!> File unit to write the fizzbuzz series to, defaults to stdout.
!!
!! The file has to be opened as formatted file with sequential access
!! and an allowed action to write.
integer, optional, intent(in) :: unit
integer :: i
integer :: loc_lb
integer :: loc_unit
if (present(lb)) then
loc_lb = lb
else
loc_lb = 1
end if
if (present(unit)) then
loc_unit = unit
else
loc_unit = output_unit
end if
do i=loc_lb,ub
! 15 is the lowest common multiple of 3, and 5
! and thus is a shortcut to ForTran
if (modulo(i, 15) == 0) then
write (loc_unit,'(A7)') 'ForTran'
else if (modulo(i, 3) == 0) then
write (loc_unit,'(A3)') 'For'
else if (modulo(i, 5) == 0) then
write (loc_unit,'(A4)') 'Tran'
else
write (loc_unit,'(I0)') i
end if
end do
end subroutine write_fizzbuzz
end module fizzbuzz_module
!> Small program to illustrate and test the fizzbuzz module.
program test_fizzbuzz
use fizzbuzz_module, only: write_fizzbuzz
implicit none
! Write a fizzbuzz series from 1 to 100 to stdout:
call write_fizzbuzz(ub=100)
! Write the next 100 entries of the fizzbuzz series to stdout:
call write_fizzbuzz(lb=101, ub=200)
end program test_fizzbuzz
通过在模块中使用私有语句,您不会将output_unit导出到使用程序单元,但是限制那些应该可见的部分,您将显式声明为public。use语句中的唯一子句也限制从模块导入的内容,但我主要使用它来指示来自何处。
https://codereview.stackexchange.com/questions/62702
复制相似问题