Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Calc Help

by Anonymous Monk
on Aug 19, 2014 at 15:20 UTC ( [id://1097985]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Write a program called calculate.pl which will operate as a primitive calculator operating on a single number (the 'accumulator') whose value may be modified by user commands. The program should meet the following requirements:

When called with arguments, interprets them as names of files containing commands.

When called without arguments, reads commands from standard input. You may find it easy or at least entertaining to run the program in this mode.

Commands are lines that match one of the following patterns:

  • EQUALS
  • CLEAR
  • PLUS number
  • MINUS number
  • TIMES number
  • OVER number

The intended effects of those commands can be seen in the following capture of program interaction. User input is EQUALS, CLEAR, PLUS, OVER, TIMES, MINUS. The accumulator starts out undefined.

calculate.pl dialog $ ./calculate.pl EQUALS (undefined) OK CLEAR OK EQUALS = 0 OK PLUS 42 OK EQUALS = 42 OK OVER 7 OK TIMES 3 OK MINUS 3 OK EQUALS = 15 OK explode Invalid statement

You can assume that there is exactly one space between the operator and the number in valid commands. You do not have to trap division by zero or warnings about operating on an undefined accumulator.

Use subroutines to keep the length of the main program and each of the subroutines below a standard screen (24 lines) at one statement per line.

Output a prompt string "> " before each input, like so: calculate.pl dialog

$ ./calculate.pl > EQUALS (undefined) OK > CLEAR OK > EQUALS = 0 OK > PLUS 42 OK > EQUALS = 42 OK > OVER 7 OK > TIMES 3 OK > MINUS 3 OK > EQUALS = 15 OK > explode Invalid statement >

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is what I have so far:

#!/usr/bin/perl use strict; use warnings; my $total; print "> "; while(my $line = <>){ chomp $line; my ($cmd, $num) = split " ", $line, 2; process($cmd,$num,$total); print "OK\n\>"; } sub process{ my($cmd,$num,$total) = @_; print "COMMAND: $cmd, VALUE: $num, TOTAL: $total \n"; }

I'm not planning on proceeding with learning much more Perl, however, I would just like to see how this project can be accomplished for piece of mind. Any help would be greatly appreciated!

Replies are listed 'Best First'.
Re: Calc Help
by MidLifeXis (Monsignor) on Aug 19, 2014 at 17:12 UTC

    I am proceeding on the assumption that this is a homework assignment (due to the formatting and wording of the 'requirements'). Given that, perhaps a few questions to guide you on how process() might work:

    • If you have CLEAR, what should it do to $total?
    • Same question for each of the other operations.
    • How do you pass update the value of $total outside of the process() subroutine?

    Others may spoon feed you a solution, but I prefer helping you find the answer yourself.

    • found in another location on the intertubes

    --MidLifeXis

Re: Calc Help
by Anonymous Monk on Aug 19, 2014 at 18:21 UTC
    I'm not planning on proceeding with learning much more Perl, however, I would just like to see how this project can be accomplished for piece of mind.

    Right... So I take it you can provide proof that the deadline for this assignment has passed? Did the instructor not provide you with a solution?

    If you're going to try to get people to do your assignments for you, at least be honest about it. Monks here have been perfectly willing to explain how to solve homework problems - but generally only to people willing to learn.

      Yes, I am no longer continuing with the course. If I could delete this post I would. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1097985]
Approved by Jim
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-24 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found