免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 无风之谷
打印 上一主题 下一主题

ChinaUnix技术实践之三——Perl编程大赛 [复制链接]

论坛徽章:
0
51 [报告]
发表于 2011-03-01 12:14 |只看该作者
第7题:
  1. my $secr=int(1+rand 100);
  2. print "Please enter a number between 1 to 100:\n";

  3. while (<STDIN>){
  4. exit if /^\s*$/ or /^(exit|quit)$/ or $secr==$_;
  5. if ($secr<$_) {
  6. print "too high!\n";
  7. next;
  8. } elsif ($secr>$_){
  9. print "too low!\n";
  10. next;
  11. }
  12. }
复制代码

论坛徽章:
0
52 [报告]
发表于 2011-03-01 12:44 |只看该作者
第6题:
  1. print "please enter some numbers:\n";
  2. chomp($_=<STDIN>);
  3. my @arr=split;
  4. my $aver;
  5. my $sum=0;
  6. map {$sum+=$_;$aver=$sum/@arr;} @arr;
  7. my @bignum=grep {$_ if $_>$aver;} @arr;
  8. print join(" ",@bignum),"\n";
复制代码

论坛徽章:
0
53 [报告]
发表于 2011-03-01 13:05 |只看该作者
第5题:
  1. opendir my $DIR,"." or die "can't open directary:$!";
  2. my @arr=readdir $DIR;
  3. @arr=sort {-M $a<=>-M $b} @arr;
  4. print join("\n",@arr);
复制代码

论坛徽章:
0
54 [报告]
发表于 2011-03-01 16:24 |只看该作者
第3题:
  1. my $str='1,2,3,5,6,7,8';
  2. my @arr=split /,/,$str;
  3. my $temp=0;
  4. my $flag=1;
  5. my %hash;
  6. map {if ($_-$temp==1) {push @{$hash{"$flag"}},$_;} else {$flag++;push @{$hash{"$flag"}},$_;} $temp=$_;}  @arr;
  7. foreach my $k (keys %hash){
  8. $_.=$hash{$k}->[0].'-'.$hash{$k}->[-1].',';
  9. }
  10. chop;
  11. print;
复制代码

论坛徽章:
0
55 [报告]
发表于 2011-03-01 19:29 |只看该作者

论坛徽章:
0
56 [报告]
发表于 2011-03-01 23:12 |只看该作者
  1. ### 1题
  2. sub myfunc{
  3.     $x = ...;
  4.     return $x ? 1 : undef;
  5. }

  6. ### 4题
  7. 标准用法和不标准用法的区别吧

  8. ### 5题
  9. print `ls -t1`;

  10. ### 6题
  11. my @nums = qw(1 3 5 7 -10 0 18);
  12. my @result = grep $_ > eval(join '+', @nums) / ($#nums + 1), @nums;
  13. #print Dumper(\@result);

  14. ### 7题
  15. my $secretNum = int(1 + rand 100);
  16. print "I have a secret number, guess what:\n";
  17. while(<STDIN>){
  18.     exit if(m/^(quit|exit|\s)/i);
  19.     chomp;
  20.     if($_ == $secretNum){
  21.         print "you get it\n";
  22.         exit;
  23.     }
  24.     print $_ > $secretNum ? "too high\n" : "too low\n";
  25. }
复制代码

论坛徽章:
0
57 [报告]
发表于 2011-03-02 12:08 |只看该作者
第八题
写一个简单的TCP Echo Server,在linux/unix环境运行,侦听在特定端口,接受用户的网络输入,并返回同样的数据给对方,能接受quit命令。
基本要求:
(1)无阻塞IO。
(2)daemon方式运行。
(3)能接受kill -HUP重启信号。

Usage: ./s.pl { start | stop | reload | restart | help | version | check }

server: s.pl

  1. use strict;
  2. use warnings;
  3. use Daemon::Generic;

  4. newdaemon(
  5.     progname => 'echoserver',
  6.     pidfile => '/tmp/echoserver.pid',
  7.     );

  8. sub gd_run {
  9. use AnyEvent::Socket;
  10. use AnyEvent::Handle;
  11.         my $cv = AE::cv;

  12.         tcp_server undef, 1234, sub {
  13.                 my $hdl;
  14.                 $hdl = AnyEvent::Handle->new(
  15.                         fh => shift,
  16.                         on_read => sub {
  17.                                 $hdl->push_write("sever:");
  18.                                 $hdl->push_write(delete $hdl->{rbuf});
  19.                         },
  20.                         on_eof => sub {},
  21.                 );
  22.         };
  23.         $cv->recv;
  24. }

  25. sub gd_preconfig{}
复制代码
client测试:c.pl

  1. use strict;
  2. use warnings;
  3. use IO::Socket;

  4. my $sock = new IO::Socket::INET (PeerAddr => '127.0.0.1',
  5.                                  PeerPort => 1234,
  6.                                  Proto    => 'tcp');
  7. die "disconnet....$!\n" unless $sock;

  8. print $sock "hello \n";

  9. my $data = <$sock>;
  10. print $data;

  11. exit;
复制代码

论坛徽章:
0
58 [报告]
发表于 2011-03-02 12:27 |只看该作者
本帖最后由 hitsubunnu 于 2011-03-02 12:28 编辑

第八题 做成方法2  ## POE

  1. use strict;
  2. use warnings;
  3. use Daemon::Generic;

  4. newdaemon(
  5.     progname => 'echoserver',
  6.     pidfile => '/tmp/echoserver.pid',
  7.     );

  8. sub gd_run {

  9. use POE qw(Component::Server::TCP);

  10. POE::Component::Server::TCP->new(
  11.   Alias       => "echo_server",
  12.   Port        => 1234,
  13.   ClientInput => sub {
  14.     my ($session, $heap, $input) = @_[SESSION, HEAP, ARG0];
  15.     $heap->{client}->put("server:".$input);
  16.   }
  17. );

  18. $poe_kernel->run();

  19. }

  20. sub gd_preconfig{}
复制代码

论坛徽章:
0
59 [报告]
发表于 2011-03-02 13:25 |只看该作者
本帖最后由 surpass_li 于 2011-03-02 13:29 编辑

1. 请正确的精简如下代码。

sub myfunc {
    # $x = ...;

    if ( $x ) {
        return 1;
    } else {
        return;
    }
}
答:
sub myfunc {
        return $x and 1;
}
 


6.传入一串数字并返回所有大于平均值的数字。
sub greThanAver
{
    my ($average,$sum)=(0,0);
    my @gre=();
    foreach(@_)
    {
        $sum+=$_;
    }
    $average =$sum/@_;
   
    foreach(@_)
    {
        if($_>$average)
        {
            push @gre,$_;
        }
    }
   
    return @gre;
}
7.让用户不断猜测范围从1到100的秘密数字,直到猜中为止。程序应该以魔术公式lnt(1+rand 100)来随机产生秘密数字。
当用户猜错时,程序应相应“too high”或者“too low”。如果用户quit或者exit或者键入一个空白行程序就中止。当然如果用户猜到了,程序也应该中止。

#!/usr/bin/perl
use Getopt::Std;
@orig_num;
@guess_num;

sub show_usage() {
        print "Usage: $0 ";
        print "         -p play game ";
        print "         -r Show rules of the game ";
        exit 0;
}

sub show_rules() {
        print "Welcome to This GUESS NUMBER GAME~! ";
        print "====猜测范围从1到100的秘密数字====! ";
        print "=输入quit或exit或键入一个空白行中止=! ";
}

sub generate_rand() {
        my $seed=time();
        srand($seed);
        my $rand = int(1+rand(100));
        $orig_num = $rand;
}

sub get_guess_number() {
        my $num_tmp;
        print "输入您猜的数字: ";
        read(STDIN,$num_tmp);
        chomp($num_tmp);
        return $num_tmp;
        print " ";
}

sub check_res() {        
        if($orig_num > $guess_num) {
                return 1;
        }
        if($orig_num < $guess_num) {
                return -1;
        }
        return 0;

}

sub play_game() {
        generate_rand();
        my $i = 0;
        my $num_guess_tmp;
        while(1) {
                $guess_num = get_guess_number();
                if("$guess_num" eq "quit") {
                        exit 0;
                }
                if("$guess_num" eq "exit") {
                        exit 0;
                }
                if("$guess_num" eq " ") {
                        exit 0;
                }
                $result=check_res();
                if("$result" eq "0") {
                        print "中了! ^_^ ";
                        exit 0;
                }
                $result=check_res();
                if("$result" eq "0") {
                        print "中了! ^_^ ";
                        exit 0;
                }
                if("$result" eq "-1") {
                        print "too high";
                }
                if("$result" eq "1") {
                        print "too low";
                }               
        }
}

sub get_opts() {
        getopts(''hrp'');
        if(defined($opt_p)) {
                play_game();
        }
        elsif(not defined($opt_r)) {
                show_usage();
        }
        if(defined($opt_h)) {
                show_usage();
        }

}

get_opts();

论坛徽章:
0
60 [报告]
发表于 2011-03-02 16:54 |只看该作者
这个有没有人想出来只用正则表达式就可以完成的?
  1. ### 3题
  2. my $string = '1,2,3,5,6,7,8,10,11,12,13,14';
  3. my ($start, $end) = undef;
  4. my $result = '';
  5. while($string =~ m/(\d+)/g){
  6.     ($start = $end = $1) && next unless $start;
  7.     if($1 - $end == 1){
  8.         $end = $1;
  9.         next;
  10.     }else{
  11.         $result .= (($start == $end) ? "$start," : "$start-$end,");
  12.         $start = $end = $1;
  13.     }
  14. }
  15. $result .= "$start-$end";
  16. print "$result\n";
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP