CS101 - LAB ACTIVITY 14

  In this week's lab work involves the completion of a hand calculator program named StackLab. This program will use the operators +, - , * , / , ^  and will use the trigonometric functions sin , cos , tan  and the sqrt function.  A demo program is provided. A partial program is provided however not all features have been implemented.

Objectives


References


Part 1: Reverse Polish Notation

1. Requirements Specification (Problem Definition)

We will complete the code for a hand calculator program. This program will not use parenthesis, to make our programming easier. So then a problem arises if you want to calculate,
(3 + 5) * 2
and type
3 + 5 * 2
the C programming language will interpret this problem (operator precedence rule) as,
3 + (5 * 2)
Here is where RPN comes to the rescue. In RPN the notation is,
3  5  + 2 *
which reads, first enter 3 into the calculator then enter 5 next press the +  button followed by a 2 then finally press the * button.
See RPN for more examples.
One way to view the RPN notation is by using the concept of a Stack. A LIFO (last in first out) Stack is like a spring loaded plate dispenser found in a cafeteria. The last (clean) plate that goes into the dispenser is the first one to be selected by a customer. We will use an array named 'stack' to implement the stack in our program. Here is how our calculator works with a Stack. Note our stack is upside down, but the numbers unlike plates won't fall out.
Push the 3 on the Stack
stack1

next push the 5,
stack2
pop the two values off of the stack and + them (3+5) and push the result back on the stack,
stack3
next push the 2 on the stack,
stack4
 now pop the two values and * them (8 * 2) and put the result on the stack.
stack5


Answer questions 1 - 4 on the answer sheet.

2. Analysis---Refine, Generalize, Decompose the problem definition


To save you time, we have provided the skeleton of the program in the file named stacklab.c, embedded in a project. First, download the project archive stacklab.zip into your home directory. Then, from Elcipse, import this project into your workspace in the usual way: under "File", select "Import...", then under "General" select "Existing projects into workspace', click "Next", then click the button "Select archive file" and browse for the file "stacklab.zip" in your home directory. Now open the imported project, "stacklab", inside which you will notice, among other things, the following files:

"stacklab.c", the source file that you have to edit;

"lab14", an executable that you can run to see how you program is supposed to work.
To run "lab14" from the Unix prompt, type lab14, then enter your RPN expression and hit <Enter> on the keyboard and then CNTRL-d on the keyboard.




For example
   
    > lab14
     3   4   5   +   -
    and then press the Enter key and then press the d key down while holding down the Control Key. You should get -6 as an answer.


3. Design---Develop Algorithm

Here is the flow chart for the stacklab recursive function.

flow chart 1
flow chart 2

4. Implementation --- Write the "Program" (Code)

Locate the code for the recursive function named "stacklab" located in your Eclipse editor in the the stacklab.c file.  You will have to complete the code for this function.
 1.  Complete the code for the logical condition. Find the comment that needs to be removed that states:

     if ( /*remove this comment and replace with a logical condition */ )
   Replace the comment with the correct code that corresponds to the flowchart above. You must use the "isnumber" function we provide for you. The code for this function is found in
   your file but we repeat it here too (important! note the datatype of ptr, so when you call isnumber what do you pass in the second argument?):

/* return 1 if string can be converted to a number else return 0 */
/* ptr points to a variable in the calling function. if string holds a number then */
/* (*ptr) is assigned that number */

int isnumber(char string[], double * ptr)
{


  double result;
 
  char *  end = string;
  char *  previous_end;


  previous_end = end;
  result = strtod(string, &end);

 
   if (previous_end  == end)
      return 0; /* not a number */
   else
      {
    *ptr = result;  /* assign number to variable in calling function */
        return 1;   /* is a number */
       }
 
}

2. Complete the cases for the other operators and functions.

     Hints: Use pow for ^, for functions like sin, cos, tan, sqrt, you will need to decrease the stack top by one and not by two.

5. Run the code

    Answer questions 5 - 11 on the answer sheet.
  

    Obtain a TA signature on the answer sheet.




That's all folks! Hand in your answer sheet to your TA.