CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Making a Calculator

 



ZeroShade
New User

Jun 29, 2006, 9:20 AM

Post #1 of 4 (427 views)
Making a Calculator Can't Post

My first perl application was "Hello World"... so I decided to do something a bit tougher with my second. I am making a calculator just for the fun of it but I'm now stuck. I have created the GUI and I have an idea of how to make the calculator calculate the numbers which I have put at the bottom of the code underneath the extensive ammount of "#'s". Can somebody tell me how to implement it into my code?

Code
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;
}


rork
User

Jun 30, 2006, 12:46 AM

Post #2 of 4 (423 views)
Re: [ZeroShade] Making a Calculator [In reply to] Can't Post

It's not clear to me what you want?
Do you want to enter an expression on the commandline? (@ARGV).

Make a subroutine you execute after making the gui and set $expression to $result.

Do you want to show the results of the expression you've entered with the buttons? (there's no such button)

execute "evalexpression" rather then "insert '='" with the $buttonequals button.

# To bad I lost my roman calculator once Unsure
--
Don't reinvent the wheel, use it, abuse it or hack it.


ZeroShade
New User

Jun 30, 2006, 6:46 AM

Post #3 of 4 (420 views)
Re: [rork] Making a Calculator [In reply to] Can't Post

No... I don't want to run it on the command line... I want it to work just like Microsofts where you put the two expressions and an operator in... then when you push equals.... the answer prints out in the text bar.


rork
User

Jun 30, 2006, 9:11 AM

Post #4 of 4 (416 views)
Re: [ZeroShade] Making a Calculator [In reply to] Can't Post

Ok then go for my advice on that one:

replace

$buttonequals->configure(-command => sub { insert '='; });

by

$buttonequals->configure(-command => sub { evalexpression; });
--
Don't reinvent the wheel, use it, abuse it or hack it.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives