主题:我来了。。。
linux 下的C和PERL的完美组合。。。!
真地很不错哟!
请看:
[code]
-----------------down.pl--------------------
#!/usr/bin/perl
$pre = 'http://acm.zju.edu.cn/list_problem.php?vol=';
for($i=1;$i<20;$i++){
$url = $pre . "$i";
`wget $url -O $i.html`;
}
-----------------parse.pl-------------------------
#!/usr/bin/perl
# 把下载完的文件拷到当前目录,并运行本程序
use GDBM_File;
use Data::Dumper;
@a = ();
open (FH,"<zju.sub") or die "can't open file: zju.sub";
while(<FH>){
@t = split /\s+/;
push @a,@t;
}
close(FH);
my %e;
foreach(@a){
$e{$_} = 1;
}
tie (%h, 'GDBM_File', 'zju.gdbm', &GDBM_WRCREAT, 0660);
$pat='<tr>.*?(\d+)</td>.*?pid=.*?>(.*?)</a></td>.*?><font color=green>(.*?)% \((\d+)\/(\d+)\).*?</tr>';
sub parse {
print "Before : ",scalar keys %h,"\n";
my $file = shift;
my @a;
open(FH,"<$file");
@a = <FH>;
chomp(@a);
$file = join(" ",@a);
while($file =~/$pat/g){
my $no = ;
if(exists $e{$no}){
next;
}
my $filename = ;
my $ratio = /100.0;
my $resolve = ;
my $total = ;
my $fail = 0;
my $prio = int($resolve * $ratio);
$v = "$no $ratio $resolve $total $fail $prio $filename";
$h{$no} = $v;
}
close(FH);
print "After : ",scalar keys %h,"\n";
}
#begin sdkf
for($i=1;$i<20;$i++){
parse "$i.html";
}
#end
#print Dumper(\%h);
print scalar keys %h,"\n";
untie %h;
----------------------run.pl------------------------
#!/usr/bin/perl
use Data::Dumper;
use GDBM_File;
my %zju;
sub getInput {
my %h;
tie (%h,'GDBM_File', 'zju.gdbm', &GDBM_READER, 0660);
foreach my $k(keys %h){
@a = split / /,$h{$k},7;
my $lh = \ {no=>$a[0], ratio=>$a[1], resolve=>$a[2], total=>$a[3], fail=>$a[4], prio=> $a[5] , name=>$a[6]};
$zju{$k} = $lh;
}
untie %h;
}
sub saveInput {
my %h;
tie %h, 'GDBM_File', 'zju.gdbm', &GDBM_NEWDB, 0660;
%h=();
foreach my $k(keys %zju){
my $ref = ${$zju{$k}};
my @a = ($ref->{no}, $ref->{ratio}, $ref->{resolve}, $ref->{total}, $ref->{fail}, $ref->{prio}, $ref->{name});
$v = join(" ", @a);
$h{$k} = $v;
}
untie %h;
}
sub Main(){
while(1){
my @a = keys %zju;
($top,@a) = sort { ${$zju{$b}}->{prio} <=> ${$zju{$a}}->{prio}} @a;
my $ref = ${$zju{$top}};
print "The current problem ID: [32;47;1m $ref->{no} [m, name: $ref->{name}\n";
my $r = $ref->{ratio} * 100;
print "prio: $ref->{prio}, ratio: $r%, $ref->{resolve}/$ref->{total}\n";
print "Left problems: ", 1 + (scalar @a),"\n";
print " A) Accept\n";
print " B) Wrong answer\n";
print " C) Ignore \n";
print " D) Exit\n";
my $input = <>;
my $inc;
$input = substr($input,0,1);
$input =uc($input);
if($input eq 'A'){ #ac
delete $zju{$top};
next;
} elsif($input eq 'B'){ #wrong
$inc = 2;
} elsif($input eq 'D'){ # exit
return;
} else {
$inc = 1;
}
$ref->{fail} += $inc;
$ref->{prio} = int ($ref->{resolve} * $ref->{ratio}) - 10 * $ref->{fail};
}
}
getInput ;
Main();
saveInput;
==============================================================
[/code]
真地很不错哟!
请看:
[code]
-----------------down.pl--------------------
#!/usr/bin/perl
$pre = 'http://acm.zju.edu.cn/list_problem.php?vol=';
for($i=1;$i<20;$i++){
$url = $pre . "$i";
`wget $url -O $i.html`;
}
-----------------parse.pl-------------------------
#!/usr/bin/perl
# 把下载完的文件拷到当前目录,并运行本程序
use GDBM_File;
use Data::Dumper;
@a = ();
open (FH,"<zju.sub") or die "can't open file: zju.sub";
while(<FH>){
@t = split /\s+/;
push @a,@t;
}
close(FH);
my %e;
foreach(@a){
$e{$_} = 1;
}
tie (%h, 'GDBM_File', 'zju.gdbm', &GDBM_WRCREAT, 0660);
$pat='<tr>.*?(\d+)</td>.*?pid=.*?>(.*?)</a></td>.*?><font color=green>(.*?)% \((\d+)\/(\d+)\).*?</tr>';
sub parse {
print "Before : ",scalar keys %h,"\n";
my $file = shift;
my @a;
open(FH,"<$file");
@a = <FH>;
chomp(@a);
$file = join(" ",@a);
while($file =~/$pat/g){
my $no = ;
if(exists $e{$no}){
next;
}
my $filename = ;
my $ratio = /100.0;
my $resolve = ;
my $total = ;
my $fail = 0;
my $prio = int($resolve * $ratio);
$v = "$no $ratio $resolve $total $fail $prio $filename";
$h{$no} = $v;
}
close(FH);
print "After : ",scalar keys %h,"\n";
}
#begin sdkf
for($i=1;$i<20;$i++){
parse "$i.html";
}
#end
#print Dumper(\%h);
print scalar keys %h,"\n";
untie %h;
----------------------run.pl------------------------
#!/usr/bin/perl
use Data::Dumper;
use GDBM_File;
my %zju;
sub getInput {
my %h;
tie (%h,'GDBM_File', 'zju.gdbm', &GDBM_READER, 0660);
foreach my $k(keys %h){
@a = split / /,$h{$k},7;
my $lh = \ {no=>$a[0], ratio=>$a[1], resolve=>$a[2], total=>$a[3], fail=>$a[4], prio=> $a[5] , name=>$a[6]};
$zju{$k} = $lh;
}
untie %h;
}
sub saveInput {
my %h;
tie %h, 'GDBM_File', 'zju.gdbm', &GDBM_NEWDB, 0660;
%h=();
foreach my $k(keys %zju){
my $ref = ${$zju{$k}};
my @a = ($ref->{no}, $ref->{ratio}, $ref->{resolve}, $ref->{total}, $ref->{fail}, $ref->{prio}, $ref->{name});
$v = join(" ", @a);
$h{$k} = $v;
}
untie %h;
}
sub Main(){
while(1){
my @a = keys %zju;
($top,@a) = sort { ${$zju{$b}}->{prio} <=> ${$zju{$a}}->{prio}} @a;
my $ref = ${$zju{$top}};
print "The current problem ID: [32;47;1m $ref->{no} [m, name: $ref->{name}\n";
my $r = $ref->{ratio} * 100;
print "prio: $ref->{prio}, ratio: $r%, $ref->{resolve}/$ref->{total}\n";
print "Left problems: ", 1 + (scalar @a),"\n";
print " A) Accept\n";
print " B) Wrong answer\n";
print " C) Ignore \n";
print " D) Exit\n";
my $input = <>;
my $inc;
$input = substr($input,0,1);
$input =uc($input);
if($input eq 'A'){ #ac
delete $zju{$top};
next;
} elsif($input eq 'B'){ #wrong
$inc = 2;
} elsif($input eq 'D'){ # exit
return;
} else {
$inc = 1;
}
$ref->{fail} += $inc;
$ref->{prio} = int ($ref->{resolve} * $ref->{ratio}) - 10 * $ref->{fail};
}
}
getInput ;
Main();
saveInput;
==============================================================
[/code]