Woooo!

Only available on StudyMode
  • Topic: Return statement, Articles with example C code, The Stack
  • Pages : 11 (982 words )
  • Download(s) : 40
  • Published : April 21, 2013
Open Document
Text Preview
#include <stdlib.h>
#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 divide(struct DynArr *stack)
{
/* FIXME: You will write this function */

double elmt1;
double elmt2;
double divide;

assert(!isEmptyDynArr(stack));
elmt1 = topDynArr(stack);
popDynArr(stack);

assert(!isEmptyDynArr(stack));
elmt2 = topDynArr(stack);
popDynArr(stack);

divide = elmt1 / elmt2;
pushDynArr(stack, divide);

}

void mult(struct DynArr *stack)
{
double val1;
double val2;
double multiply;

assert(!isEmptyDynArr(stack));
val1 = topDynArr(stack);
popDynArr(stack);

assert(!isEmptyDynArr(stack));
val2 = topDynArr(stack);
popDynArr(stack);

multiply = val1 * val2;
pushDynArr(stack, multiply);

}

void power(struct DynArr *stack)
{
double val1;
double val2;
double pwr;

assert(!isEmptyDynArr(stack));
val1 = topDynArr(stack);
popDynArr(stack);

assert(!isEmptyDynArr(stack));
val2 = topDynArr(stack);
popDynArr(stack);

pwr = pow(val2, val1);
pushDynArr(stack, pwr);

}

void squares(struct DynArr *stack)
{
double val;
double sqr;

assert(!isEmptyDynArr(stack));
val = topDynArr(stack);
popDynArr(stack);

sqr = val * val;
pushDynArr(stack, sqr);

}

void cubes(struct DynArr *stack)
{
double val;
double cb;

assert(!isEmptyDynArr(stack));
val = topDynArr(stack);
popDynArr(stack);

cb = (val * val) * val;
pushDynArr(stack, cb);

}

void abso(struct DynArr *stack)
{
double val;
double ab;

assert(!isEmptyDynArr(stack));
ab = topDynArr(stack);
popDynArr(stack);

ab = abs(val);
pushDynArr(stack, ab);

}

void roots(struct DynArr *stack)
{
double val;
double root;

assert(!isEmptyDynArr(stack));
root = topDynArr(stack);
popDynArr(stack);

root = abs(val);
pushDynArr(stack, root);

}

void expo(struct DynArr *stack)
{
double val;
double ex;

assert(!isEmptyDynArr(stack));
ex = topDynArr(stack);
popDynArr(stack);

ex = exp(val);
pushDynArr(stack, ex);

}

void logs(struct DynArr *stack)...
tracking img