y=x^2 or y=x**2 or y=3x^2 #### #!/usr/bin/perl -wT use strict; use constant pi => 3.14159265358979; use Tk; my $top = new MainWindow(-width => 800, -height => 600); my $cvs = $top->Canvas(-width => 800, -height => 550); my $frm = $top->Canvas(-width => 800, -height => 20); my $txt = $frm->Entry(-width => 40); my $min = $frm->Entry(-width => 5); my $max = $frm->Entry(-width => 5); my $stp = $frm->Entry(-width => 5); my $clr = $frm->Button(-text => 'Clear'); $clr->bind('' => [ sub { $cvs->delete($_) for $cvs->find('all') } ]); $txt->bind('', [ \&evaluate ]); $frm->pack(-side => 'bottom'); $min->pack(-side => 'left'); $max->pack(-side => 'left'); $stp->pack(-side => 'left'); $clr->pack(-side => 'right'); $txt->pack(-side => 'right'); $cvs->pack(-side => 'top'); $min->insert(0, "0"); $max->insert(0, "100"); $stp->insert(0, "1"); MainLoop; sub evaluate { my $text = $txt->get(); my $func = lc $text; $func =~ s/(\d+)(pi|x)/$1 * $2/g; $func =~ s/([xy])/\$$1/g; $func =~ s/\^/**/g; my $minx = $min->get(); my $maxx = $max->get(); my $step = $stp->get(); my $size = 1; for (my $x = $minx; $x <= $maxx; $x += $step) { my $y; eval "$func"; # warn "$func: $x, $y\n" if !($x % 50); $cvs->createRectangle( 800 * $x/$maxx, 300 - $y, 800 * $x/$maxx + $size, 300 - $y + $size, -width => 1); } $txt->delete(0, length $text); } __END__