CS204 Lab 2 : Bench Calculator part 1
This is the first part of a three-part lab that aims to build a bench
calculator, that is a program that computes the value of simple
expressions on the command line. No work needs to be submitted this
week, but it is important that you do the work, because you will need
it next week.
The first piece of software that we need to create the bench
calculator is a stack of floating point numbers. A stack is a data
structure where you can "push" values onto the stack, and "pop" values
from the stack in a last-in-first-out (LIFO) pattern.
Write a stack data structure that uses the following prototypes:
// type declaration for stack of doubles
struct double_stack {
double * items;
int max_size;
int top;
};
// prototypes of functions that operate on the double stack
// create a new empty stack
struct double_stack * double_stack_new(int max_size);
// push a value onto the stack
void double_stack_push(struct double_stack * this, double value);
// pop a value from the stack
double double_stack_pop(struct double_stack * this);
To get you started, here is some code for creating a new empty stack:
// create a new empty stack
struct double_stack * double_stack_new(int max_size) {
struct double_stack * result;
// allocate space for the stack header
result = malloc(sizeof(struct double_stack));
result->max_size = max_size;
result->top = 0;
// allocate space for the data stored in the stack
result->items = malloc(sizeof(double)*max_size);
// return a pointer to the newly-allocated stack
return result;
}
// end of code
Note that we covered the basics of writing this stack in lectures, so
if you missed the lecture where we did it, you may need extra help.
If your program is in a file calc.c, then you compile your program using
gcc:
gcc -o calc calc.c
Or you can use the clang compiler:
clang -o calc calc.c
Once you have compiled your program, you can run it with:
./calc
If you don't have a main function in your program, the compiler
will complain that it can't find a main function.
Finally, write a main function that takes in command-line parameters.
You should pass some floating point numbers as command line parameters,
and push those values onto your stack.
The following is some simple example code that reads in command line
parameters.
// include parts of the C standard library
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, char ** argv) {
int i;
if ( argc == 1 ) {
printf("Please try adding some command-line parameters\n");
printf("Usage: %s <param1> <param2> ...\n", argv[0]);
exit(1);
}
printf("The number of command line parameters is stored in argc: %d\n", argc);
printf("The value of argc counts the name of the program itself as a parameter\
n");
printf("The name of the program is stored in argv[0]: %s\n", argv[0]);
printf("The parameters are:\n");
for ( i = 1; i < argc; i++ ) {
printf("%s\n", argv[i]);
}
return 0;
}
// end of code
To run your program with command line parameters, you will write something
like:
./calc 45 27 13.2 8