calculator

Discussion in 'Perl' started by makko, Feb 28, 2005.

  1. makko

    makko Guest

    Hello all,
    my very first perl program, a calculator that takes formulas;

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    ##
    # CALC 1.0
    # running: $perl calc.pl
    # elmakko Feb 2005
    ##

    use strict;
    use warnings;

    my $delimit_arr = "\n\t++++++++++++\n\n";
    my $argc = scalar(@ARGV);
    if($argc != 1)
    {
    print "\n/*\n * usage: perl calc.pl <formula>\n */\n\n";
    exit -1;
    }
    my $ans = eval($ARGV[0]);
    system "clear";
    banner();
    print "\t$ans \n";
    print "$delimit_arr";

    sub banner()
    {
    print "\n\t************\n";
    print "\t* CALC 1.0 *\n";
    print "\t************\n";
    print "\n\n";
    }

    exit 0;

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    regards,
    makko.
     
    makko, Feb 28, 2005
    #1
    1. Advertising

  2. makko

    thundergnat Guest

    makko wrote:
    > Hello all,
    > my very first perl program, a calculator that takes formulas;
    >


    First of all, this newsgroup is defunct. In the future, use
    comp.lang.perl.misc, or perhaps even better, perl.beginners.

    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > ##
    > # CALC 1.0
    > # running: $perl calc.pl
    > # elmakko Feb 2005
    > ##
    >
    > use strict;
    > use warnings;


    Good!

    >
    > my $delimit_arr = "\n\t++++++++++++\n\n";
    > my $argc = scalar(@ARGV);


    No need for scalar: my $argc = @ARGV;

    > if($argc != 1)
    > {
    > print "\n/*\n * usage: perl calc.pl <formula>\n */\n\n";
    > exit -1;
    > }


    Doesn't take into acount spaces in the formulas

    > my $ans = eval($ARGV[0]);


    Ouch. Possibly very dangerous to eval() without any trapping of malicious
    entries. Not as big an issue for somthing YOU are going to run on YOUR
    computer, but definately not somthing you should get in the habit of doing.

    > system "clear";


    "clear" is not recognized as an internal or external command.

    > banner();
    > print "\t$ans \n";
    > print "$delimit_arr";
    >
    > sub banner()


    Don't use prototypes unless you absolutly need them.

    sub banner

    > {
    > print "\n\t************\n";
    > print "\t* CALC 1.0 *\n";
    > print "\t************\n";
    > print "\n\n";
    > }
    >
    > exit 0;
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >


    Not a horrible first script. Big plusses for use warnings and use strict.
    You need to be careful using eval()s though. They may not work exactly
    as you expect.

    For instance, try the following; does this return the answer you expect?

    perl calc.pl 01.5+01.5
     
    thundergnat, Feb 28, 2005
    #2
    1. Advertising

  3. makko

    makko Guest

    That ruturns 165 instead of 3, thanx for the advice.
     
    makko, Mar 1, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWFoc2E=?=

    Re: distance calculator

    =?Utf-8?B?bWFoc2E=?=, Jun 4, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    382
    =?Utf-8?B?bWFoc2E=?=
    Jun 5, 2004
  2. Maria Laura Re

    URGENT Help With Scientific Calculator!

    Maria Laura Re, Dec 13, 2003, in forum: Java
    Replies:
    4
    Views:
    524
    Andrew Thompson
    Dec 14, 2003
  3. Ike

    Calculator Applet

    Ike, Feb 18, 2004, in forum: Java
    Replies:
    1
    Views:
    433
    Andrew Thompson
    Feb 19, 2004
  4. Mc Kiernan, Daniel Kian

    Freeware Class File Programmer's Calculator

    Mc Kiernan, Daniel Kian, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    485
    Mc Kiernan, Daniel Kian
    Jul 17, 2004
  5. Replies:
    2
    Views:
    773
    Oliver Wong
    Feb 6, 2006
Loading...

Share This Page