首页 >

Console界面字符进度条

作者:fcode  日期:12-28
来源:Fcode研讨团队
本代码实现了在console界面利用字符界面的进度条,可用于大量计算时的进度显示。在IVF , gfortran for windows , gfortran for linux , NAG for windows 上测试通过。其他编译器不保证。

代码不完全规范,如果您的编译器无法编译,您可以试试注释中的替代写法。如果您的编译器较老,可能无法编译。

效果如图:

大图
Module CMD_Progress
  Implicit None
  private
  Logical , parameter , public :: CMD_PROGRESS_ABSOLUTE = .true.
  Type , public :: CLS_CMD_Progress
    Integer , private :: N = 100 , lens = 25 , i = 0
    Character :: M = "*" , O = "."
    Character(len=64) :: Prefix = " Progress: "
  Contains
    Procedure :: Set
    Procedure :: Put
  End Type CLS_CMD_Progress
  
contains

  Subroutine Set( this , N , L )
    Class( CLS_CMD_Progress ) :: this
    Integer , Intent( IN ) :: N , L
    this%N    = N
    this%lens = L
    this%i    = 0 
  End Subroutine Set
  
  Subroutine Put( this , K , bAbsol )
    Class( CLS_CMD_Progress ) :: this
    Integer , Intent( IN ) :: K
    Logical , Intent( IN ) , optional :: bAbsol
    character :: br
    integer   :: jm
    this%i = this%i + K
    if ( present( bAbsol ) ) this%i = merge( k , this%i+K , bAbsol )
    this%i = min(this%i,this%n)
    jm = Nint( real( this%i * this%lens ) / real( this%N ) )
    br = merge( char(13) , char(10) , this%i < this%n )
    write( * , '(a,"[",2a,"]",f6.2,"%",a\)') trim(this%Prefix) , &
      repeat(this%M , jm ) , repeat( this%O , this%lens-jm ) , this%i*100.0/this%N , br
  End Subroutine Put
  
End Module CMD_Progress

Program www_fcode_cn
  use CMD_Progress
  Implicit None
  type( CLS_CMD_Progress ) ::Progress
  integer :: i
  call Progress%Set( N = 1700 , L = 35 )!// 1700次,显示长度25
  Progress%Prefix = "Test Progress:  "  !// 前方提示文字,不是必须
  Progress%M = "%" !// 已完成部分的字符,不是必须
  Progress%O = "-" !// 未完成部分的字符,不是必须
  write(*,*) 'Begin'
  Do i = 0 , 1700 , 50
    call Progress%Put( i , CMD_PROGRESS_ABSOLUTE ) !// 绝对方式
    !call Progress % Put( 50 ) !// 也可用相对方式
    call sleep(1) !// 如您的编译器不支持,请用下方的循环代替
    !Do j = 1 , 10000000
    !End Do
  End Do
  write(*,*) 'End'
End Program www_fcode_cn
常规|工具|专业|读物|
代码|教学|算法|
首页 >
FortranCoder手机版-导航