QUOTE(usamamuneeb @ 4 Oct, 2008 - 02:29 AM)

Here's a simple calculator:
CODE
#!/usr/bin/perl
$nbArguments = $#ARGV + 1;
print "number of arguments: $nbArguments\n";
exit(1) unless $nbArguments == 3;
$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;
}
print "$a $operation $b = $result\n";
The first line tells where is the perl binary. The second line gets the number of arguements (must be three, two numbers and an operator). Because the count starts from zero, one is added. the third line shows the num of args. 1 is a unix convention of error occurance. so the prog will exit unless there are three arguements. The next three subsequent lines store the arguements in an array. $ARGV[0] and $ARGV[2] store the numbers (operands) and are assigned $a and $b instances respectively while the $ARGV[1] stores the operator. Then there are if and elsif statements. The perform the action according to the operator. and store value in result. The last line prints the expression provided by the user and the result and \n is a newline.
im sorry about that it wasnt new to perl i meant to say new to python
thats a perl programming i asked about python how about doing a python program like building a calulator with python
This post has been edited by sniper24: 4 Oct, 2008 - 03:49 PM