11月 29

在Perl程序中显示进度条之使用Term::ProgressBar模块

  • A really simple use

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    #!/usr/bin/perl -w
     
    use Term::ProgressBar 2.00;
    use constant MAX =100_0000;
     
    my $progress = Term::ProgressBar-new(MAX);
     
    for ( 0 .. MAX ) {
       my $is_power = 0;
       for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
           $is_power = 1
             if 2**$i == $_;
       }
     
       if ($is_power) {
           $progress-》update($_);
       }
    }
  • A smoother bar update

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    #!/usr/bin/perl -w
     
    use Term::ProgressBar 2.00;
     
    my $max = 100000;
     
    my $progress = Term::ProgressBar-new($max);
     
    for ( 0 .. $max ) {
       my $is_power = 0;
       for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
           $is_power = 1
             if 2**$i == $_;
       }
     
       $progress-》update($_);
    }

    Continue reading

11月 28

程序代码中的缩进与大括号风格

  • K&R style

Named after Kernighan & Ritchie, because the examples in K&R are formatted this way. Also called `kernel style’ because the Unix kernel is written in it, and the `One True Brace Style’ (abbrev. 1TBS) by its partisans. In C code, the body is typically indented by eight spaces (or one tab) per level, as shown here. Four spaces are occasionally seen in C, but in C++ and Java four tends to be the rule
rather than the exception.……【阅读全文】