#!/usr/bin/perl -w
#use strict;
require 5.002;
use Tk;
use Tk::Text;
use Tk::Menu;
use Tk::Scrollbar;
use Tk::DialogBox;
use Tk::ROText;
use Tk::Pixmap;
use Tk::Bitmap;
our $x=0;
our $y=0;
our $z=0;
our $t=0;
our $res='n';
our $sto='n';
our $rcl='n';
our $pnt='n';
our $sto0=0;
our $sto1=0;
our $sto2=0;
our $sto3=0;
our $sto4=0;
our $sto5=0;
our $sto6=0;
our $sto7=0;
our $sto8=0;
our $sto9=0;
our $stop0=0;
our $stop1=0;
our $stop2=0;
our $stop3=0;
our $stop4=0;
our $stop5=0;
our $stop6=0;
our $stop7=0;
our $stop8=0;
our $stop9=0;
our $xxx='17';
my $mw = MainWindow->new;
$mw->maxsize(qw(400 200));
$mw->minsize(qw(400 200));
$mw->title("Calculadora Financeira");
$mw->configure(-bg=>"black");
$label = $mw->Text(
-background => 'light goldenrod',
-borderwidth => '0',
-width => '50',
-height => '4',
-state => 'disable'
)->pack;
#my $fundo = $mw->Pixmap(
# -data => '/* XPM*/',
# -background => 'yellow',
# -file => 'calc.xpm',
# -width => '350',
# -height => '50'
# )->pack(-side => 'top');
########################################################
our $dis = $mw->Entry(
-width => '10',
)->pack;
$dis->place(-x=>'105',-y=>'20');
##############################################################################################################
my $enter = $mw->Button(
-text => 'Enter',
-command => sub{entersub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$enter->place(-x=>'197',-y=>'155');
my $soma = $mw->Button(
-text => '+',
-command => sub{somasub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$soma->place(-x=>'353',-y=>'170');
my $subitracao = $mw->Button(
-text => '-',
-command => sub{subtrasub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$subitracao->place(-x=>'353',-y=>'140');
my $multiplicacao = $mw->Button(
-text => 'x',
-command => sub{multisub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$multiplicacao->place(-x=>'353',-y=>'110');
my $divisao = $mw->Button(
-text => '/',
-command => sub{divisub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$divisao->place(-x=>'353',-y=>'80');
#####################################################################################################################
my $CLx = $mw->Button(
-text => 'CLx',
-command => sub{CLxsub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$CLx->place(-x=>133 + $xxx,-y=>'140');
my $xsy = $mw->Button(
-text => 'xsy',
-command => sub{xsysub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$xsy->place(-x=>100 + $xxx,-y=>'140');
my $Rs = $mw->Button(
-text => 'Rs',
-command => sub{Rssub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$Rs->place(-x=>66 + $xxx,-y=>'140');
my $SST = $mw->Button(
-text => 'SST',
# -command => sub{Rssub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$SST->place(-x=>33 + $xxx,-y=>'140');
my $RbS = $mw->Button(
-text => 'R/S',
# -command => sub{Rssub()},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$RbS->place(-x=> $xxx,-y=>'140');
my $RCL = $mw->Button(
-text => 'RCL',
-command => sub{$rcl='s'},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$RCL->place(-x=>133 + $xxx,-y=>'170');
my $STO = $mw->Button(
-text => 'STO',
-command => sub{$sto='s'},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$STO->place(-x=>100 + $xxx,-y=>'170');
my $g = $mw->Button(
-text => 'g',
# -command => sub{$sto='s'},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'dark blue'
)->pack;
$g->place(-x=>66 + $xxx,-y=>'170');
my $f = $mw->Button(
-text => 'f',
# -command => sub{$sto='s'},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'dark goldenrod'
)->pack;
$f->place(-x=>33 + $xxx,-y=>'170');
my $OFF = $mw->Button(
-text => 'OFF',
-command => sub{exit},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$OFF->place(-x=>$xxx,-y=>'170');
#################################################################################################################
my $zero=$mw->Button(
-text => '0',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop0=$dis->get:
$sto0=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop0):
$dis->insert('end',$sto0);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','0')},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$zero->place(-x=>'245',-y=>'170');
my $ponto=$mw->Button(
-text => '.',
-command => sub
{
if($sto eq 's')
{
$pnt='s';
return;
}
if($rcl eq 's')
{
$pnt='s';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','.')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$ponto->place(-x=>'275',-y=>'170');
my $somatorio=$mw->Button(
-text => 'E+',
# -command => sub{$dis->insert('end','E')},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$somatorio->place(-x=>'305',-y=>'170');
my $um=$mw->Button(
-text => '1',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop1=$dis->get:
$sto1=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop1):
$dis->insert('end',$sto1);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','1')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$um->place(-x=>'245',-y=>'140');
my $dois=$mw->Button(
-text => '2',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop2=$dis->get:
$sto2=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop2):
$dis->insert('end',$sto2);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','2')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$dois->place(-x=>'275',-y=>'140');
my $tres=$mw->Button(
-text => '3',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop3=$dis->get:
$sto3=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop3):
$dis->insert('end',$sto3);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','3')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$tres->place(-x=>'305',-y=>'140');
my $quatro=$mw->Button(
-text => '4',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop4=$dis->get:
$sto4=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop4):
$dis->insert('end',$sto4);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','4')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$quatro->place(-x=>'245',-y=>'110');
my $cinco=$mw->Button(
-text => '5',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop5=$dis->get:
$sto5=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop5):
$dis->insert('end',$sto5);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','5')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$cinco->place(-x=>'275',-y=>'110');
my $seis=$mw->Button(
-text => '6',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop6=$dis->get:
$sto6=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop6):
$dis->insert('end',$sto6);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','6')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$seis->place(-x=>'305',-y=>'110');
my $sete=$mw->Button(
-text => '7',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop7=$dis->get:
$sto7=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop7):
$dis->insert('end',$sto7);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','7')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$sete->place(-x=>'245',-y=>'80');
my $oito=$mw->Button(
-text => '8',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop8=$dis->get:
$sto8=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop8):
$dis->insert('end',$sto8);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','8')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$oito->place(-x=>'275',-y=>'80');
my $nove=$mw->Button(
-text => '9',
-command => sub
{
if ($sto eq 's')
{
($pnt eq 's')?
$stop9=$dis->get:
$sto9=$dis->get;
$sto='n';
$res='s';
$pnt='n';
return;
} elsif ($rcl eq 's')
{
$dis->delete('0.0','end');
($pnt eq 's')?
$dis->insert('end',$stop9):
$dis->insert('end',$sto9);
$rcl='n';
$pnt='n';
return;
}
if ($res eq 's')
{
$dis->delete('0.0','end');
$res='n';
}
$dis->insert('end','9')
},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$nove->place(-x=>'305',-y=>'80');
MainLoop;
###################################################################################################################
sub entersub
{
$t=$z;
$z=$x;
$x=$dis->get;
$y=$x;
#$dis->delete('0.0','end');
$res='s';
}
sub somasub
{
if ($res eq 's')
{
$x+=$y;
} else
{
$x+=$dis->get;
}
$dis->delete('0.0','end');
$dis->insert('end',$x);
$y=$z;
$z=$t;
$res='s';
}
sub subtrasub
{
if ($res eq 's')
{
$x-=$y;
} else
{
$x-=$dis->get;
}
$dis->delete('0.0','end');
$dis->insert('end',$x);
$y=$z;
$z=$t;
$res='s';
}
sub multisub
{
if ($res eq 's')
{
$x*=$y;
} else
{
$x*=$dis->get;
}
$dis->delete('0.0','end');
$dis->insert('end',$x);
$y=$z;
$z=$t;
$res='s';
}
sub divisub
{
if ($res eq 's')
{
$x/=$y;
} else
{
$x/=$dis->get;
}
$dis->delete('0.0','end');
$dis->insert('end',$x);
$y=$z;
$z=$t;
$res='s';
}
###################################################################################################################
sub CLxsub
{
$x=0;
$dis->delete('0.0','end');
}
sub xsysub
{
$x = $y;
$y = $dis->get;
$dis->delete('0.0','end');
$dis->insert('end',$x);
$res='s';
}
sub Rssub
{
my $aj=$dis->get;
$dis->delete('0.0','end');
$dis->insert('end',$y);
$x=$y;
$y=$z;
$z=$t;
$t=$aj;
$res='s';
}
What is it supposed to do, compared to what it is currently doing? If it isn't a finished working script, you probably should have posted this as a question instead of a Code Contribution.
The first thing to do is enable strict, all you need to do is put a "my" before $label on line 52.
Otherwise it runs for me, but what sort of calculations is it supposed to perform?
Some suggestions:
my %button_label =
(
Sum => '+',
Subtra => '-',
Multi => 'x',
Divi => '/',
Rs => 'R/S',
);
my %placement =
(
Enter => [ 197, 155 ],
Sum => [ 353, 170 ],
Subtra => [ 353, 140 ],
Multi => [ 353, 110 ],
Divi => [ 353, 80 ],
CLx => [ 150, 140 ],
XsY => [ 117, 140 ],
Rs => [ 83, 140 ],
SST => [ 50, 140 ],
RbS => [ 17, 140 ],
RCL => [ 150, 170 ],
STO => [ 117, 170 ],
OFF => [ 17, 170 ],
);
for (qw(
Enter Sum Subtra Multi Divi
CLx XsY Rs SST RbS RCL STO OFF
))
{
my $w = $mw->Button( -text => $button_label{$_} || $_, )->pack;
$w->place( -x => $placement{$_}[0], -y => $placement{$_}[1] );
}
The only suggestion I would make with regards its presentation is to perhaps make the result window (the Entry widget) larger:
our $dis = $mw->Entry( -width => '30' )->pack; # Changed width from 10 to 30
As for the actual code, I very much agree with [jdporter]'s points. Here are some further suggestions I would add:
Wouldn't this:
sub CLxsub
{
$x=0;
$dis->delete('0.0','end');
}
sub xsysub
{
$x = $y;
$y = $dis->get;
$dis->delete('0.0','end');
$dis->insert('end',$x);
$res='s';
}
sub Rssub
{
my $aj=$dis->get;
$dis->delete('0.0','end');
$dis->insert('end',$y);
$x=$y;
$y=$z;
$z=$t;
$t=$aj;
$res='s';
}
be so much easier to read like this ...?
sub CLxsub {
$x = 0;
$dis->delete( '0.0', 'end' );
}
sub xsysub {
$x = $y;
$y = $dis->get;
$dis->delete( '0.0', 'end' );
$dis->insert( 'end', $x );
$res = 's';
}
sub Rssub {
my $aj = $dis->get;
$dis->delete( '0.0', 'end' );
$dis->insert( 'end', $y );
$x = $y;
$y = $z;
$z = $t;
$t = $aj;
$res = 's';
}
my $STO = $mw->Button(
-text => 'STO',
-command => sub{$sto='s'},
-width =>'1',
-height =>'1',
-foreground => 'white',
-background => 'black'
)->pack;
$STO->place(-x=>100 + $xxx,-y=>'170');
Instead, write a "wrapper" for a button, such as:
sub button($$$$$;$$$$) {
my ($parent, $text, $pcmd, $x, $y, $fg, $bg, $width, $height) = @_;
# Defaults
$width ||= 1;
$height ||= 1;
$fg ||= 'white';
$bg ||= 'black';
# Color abbreviations
my $p_abb = {
'db' => 'darkblue',
'dg' => 'darkgoldenrod',
};
defined($p_abb->{$fg}) and $fg = $p_abb->{$fg};
defined($p_abb->{$bg}) and $bg = $p_abb->{$bg};
# Create the button widget (note command need not be given)
my $b = $parent->Button(-text => $text);
$pcmd and $b->configure(-command => $pcmd);
$b->configure(-width => $width, -height => $height);
$b->configure(-foreground => $fg, -background => $bg);
# Place the button (no need to "pack" it first!), and return it
$b->place(-x => $x, -y => $y);
return $b;
}
One advantage of this is that you can default those parameters which are (almost) always the same, such as the height, width and colors. Another is that, since you're now doing the place within the subroutine (and you don't need the "pack" first, by the way), you don't need to assign the button outside of the subroutine in most cases. Finally, the button definition becomes much cleaner:
button($mw, 'Enter', [\&entersub], 197, 155);
button($mw, '+', [\&somasub], 353, 170);
button($mw, '-', [\&subtrasub], 353, 140);
button($mw, 'x', [\&multisub], 353, 110);
button($mw, '/', [\&divisub], 353, 80);
button($mw, 'CLx', [\&CLxsub], 133, 140);
button($mw, 'xsy', [\&xsysub], 100, 140);
button($mw, 'Rs', [\&Rssub], 66, 140);
button($mw, 'SST', [\&Rssub], 33, 140);
button($mw, 'R/S', [\&Rssub], $xxx, 140);
button($mw, 'RCL', sub{$rcl = 's'}, $xxx+133, 140);
button($mw, 'STO', sub{$sto = 's'}, $xxx+100, 170);
button($mw, 'g', 0, $xxx+66, 170, 0, 'db');
button($mw, 'f', 0, $xxx+33, 170, 0, 'dg');
button($mw, 'OFF', sub{exit}, $xxx, 170);
perlmonks.org content © perlmonks.org and jdporter, liverpole, smokemachine, zentara
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03