#include <stdio.h>
#include <string.h>
#include "dynamicArray.h"
#include <assert.h>
/* param: s the string * param: num a pointer to double * returns: true (1) if s is a number else 0 or false. * postcondition: if it is a number, num will hold * the value of the number * */ int isNumber(char *s, double *num)
{
char *end; double returnNum;
returnNum = strtod(s, &end);
/* If there's anything in end, it's not a number */ if(strcmp(end, "") == 0) { *num = returnNum; return 1; } else { return 0; }
}
/* param: stack the stack being manipulated * pre: the stack contains at least two elements * post: the top two elements are popped and * their sum is pushed back onto the stack. * */ void add (struct DynArr *stack)
{
/* FIXME: You will write this function */
double elmt1; double elmt2; double sum;
assert(!isEmptyDynArr(stack)); elmt1 = topDynArr(stack); popDynArr(stack);
assert(!isEmptyDynArr(stack)); elmt2 = topDynArr(stack); popDynArr(stack);
sum = elmt1 + elmt2; pushDynArr(stack, sum);
}
/* param: stack the stack being manipulated * pre: the stack contains at least two elements * post: the top two elements are popped and * their difference is pushed back onto the stack. * */ void subtract(struct DynArr *stack)
{
/* FIXME: You will write this function */
double elmt1; double elmt2; double subtract;
assert(!isEmptyDynArr(stack)); elmt1 = topDynArr(stack); popDynArr(stack);
assert(!isEmptyDynArr(stack)); elmt2 = topDynArr(stack); popDynArr(stack);
subtract = elmt2 - elmt1; pushDynArr(stack, subtract);
}
/* param: stack the stack being manipulated * pre: the stack contains at least two elements * post: the top two elements are popped and * their quotient is pushed back onto the stack. * */
void