use Tk;
my $expression= '';
sub evalexpression
{
my $result;
$result = eval($expression);
if ($@)
{
$expression = $@;
}
else
{
$expression = $result;
}
}
sub all_clear
{
$expression = '';
}
sub insert
{
$expression .= $_[0];
}
sub calctest_ui
{
my ($root) = @_;
# Widget Creation.
my ($expression) = $root->Entry (-textvariable => \$expression,);
my ($button7) = $root->Button (-text => '7',);
my ($button8) = $root->Button (-text => '8',);
my ($button9) = $root->Button (-text => '9',);
my ($buttonoff) = $root->Button (-text => 'off',);
my ($buttonAC) = $root->Button (-text => 'AC',);
my ($button4) = $root->Button (-text => '4',);
my ($button5) = $root->Button (-text => '5',);
my ($button6) = $root->Button (-text => '6',);
my ($buttontimes) = $root->Button (-text => '*',);
my ($buttondivide) = $root->Button (-text => '/',);
my ($button1) = $root->Button (-text => '1',);
my ($button2) = $root->Button (-text => '2',);
my ($button3) = $root->Button (-text => '3',);
my ($buttonplus) = $root->Button (-text => '+',);
my ($buttonminus) = $root->Button (-text => '-',);
my ($button0) = $root->Button (-text => '0',);
my ($buttonperiod) = $root->Button (-text => '.',);
my ($buttonleft) = $root->Button (-text => '(',);
my ($buttonright) = $root->Button (-text => ')',);
my ($buttonequals) = $root->Button (-text => '=',);
# Widget Commands.
$button7->configure(-command => sub { insert '7'; });
$button8->configure(-command => sub { insert '8'; });
$button9->configure(-command => sub { insert '9'; });
$buttonoff->configure(-command => sub { exit(); });
$buttonAC->configure(-command => \&all_clear);
$button4->configure(-command => sub { insert '4'; });
$button5->configure(-command => sub { insert '5'; });
$button6->configure(-command => sub { insert '6'; });
$buttontimes->configure(-command => sub { insert '*'; });
$buttondivide->configure(-command => sub { insert '/'; });
$button1->configure(-command => sub { insert '1'; });
$button2->configure(-command => sub { insert '2'; });
$button3->configure(-command => sub { insert '3'; });
$buttonplus->configure(-command => sub { insert '+'; });
$buttonminus->configure(-command => sub { insert '-'; });
$button0->configure(-command => sub { insert '0'; });
$buttonperiod->configure(-command => sub { insert '.'; });
$buttonleft->configure(-command => sub { insert '('; });
$buttonright->configure(-command => sub { insert ')'; });
$buttonequals->configure(-command => sub { insert '='; });
# Geometry Management.
$expression->grid(-in => $root, -column => '1', -row => '1', -columnspan => '5');
$button7->grid(-in => $root, -column => '1', -row => '2');
$button8->grid(-in => $root, -column => '2', -row => '2');
$button9->grid(-in => $root, -column => '3', -row => '2');
$buttonoff->grid(-in => $root, -column => '4', -row => '2');
$buttonAC->grid(-in => $root, -column => '5', -row => '2');
$button4->grid(-in => $root, -column => '1', -row => '3');
$button5->grid(-in => $root, -column => '2', -row => '3');
$button6->grid(-in => $root, -column => '3', -row => '3');
$buttontimes->grid(-in => $root, -column => '4', -row => '3');
$buttondivide->grid(-in => $root, -column => '5', -row => '3');
$button1->grid(-in => $root, -column => '1', -row => '4');
$button2->grid(-in => $root, -column => '2', -row => '4');
$button3->grid(-in => $root, -column => '3', -row => '4');
$buttonplus->grid(-in => $root, -column => '4', -row => '4');
$buttonminus->grid(-in => $root, -column => '5', -row => '4');
$button0->grid(-in => $root, -column => '1', -row => '5');
$buttonperiod->grid(-in => $root, -column => '2', -row => '5');
$buttonleft->grid(-in => $root, -column => '3', -row => '5');
$buttonright->grid(-in => $root, -column => '4', -row => '5');
$buttonequals->grid(-in => $root, -column => '5', -row => '5');
# Resize Behavior Managment.
$root->gridColumnconfigure(1, -weight => 0, -minsize => 2);
$root->gridColumnconfigure(2, -weight => 0, -minsize => 13);
$root->gridColumnconfigure(3, -weight => 0, -minsize => 13);
$root->gridColumnconfigure(4, -weight => 0, -minsize => 30);
$root->gridColumnconfigure(5, -weight => 0, -minsize => 30);
$root->gridRowconfigure(1, -weight => 0, -minsize => 30);
$root->gridRowconfigure(2, -weight => 0, -minsize => 17);
$root->gridRowconfigure(3, -weight => 0, -minsize => 8);
$root->gridRowconfigure(4, -weight => 0, -minsize => 7);
$root->gridRowconfigure(5, -weight => 0, -minsize => 2);
}
$main = MainWindow->new();
$main->title("Calculator");
calctest_ui($main);
MainLoop();
#################################
$a = $ARGV[0];
$b = $ARGV[2];
$operation = $ARGV[1];
if ($operation eq "+") {
$result = $a + $b;
}
elsif ($operation eq "-") {
$result = $a - $b;
}
elsif ($operation eq "/") {
$result = $a / $b;
}
elsif ($operation eq "x") {
$result = $a * $b;
}