Ejemplo n.º 1
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)
{
	//declare necessary doubles for operator use on stack
	double operand_1, operand_2, add_result;

	//make sure the stack is not empty
	//assert(!isEmptyDynArr(stack));

	//store and pop first number off
	operand_1 = topDynArr(stack);
	popDynArr(stack);

	//make sure stack is not empty
	//assert(!isEmptyDynArr(stack));

	//store and pop second number off
	operand_2 = topDynArr(stack);
	popDynArr(stack);

	//store in result
	add_result = operand_2 + operand_1;

	//push back onto stack
	pushDynArr(stack, add_result);

}
Ejemplo n.º 2
0
/* 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 power(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 2);
    double num2 = topDynArr(stack);
    popDynArr(stack);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, pow(num1,num2));
}
Ejemplo n.º 3
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string 	
	pre: s is not null	
	post:	
*/
int isBalanced(char* s)
{
	/* FIXME: You will write this function */
    DynArr* work;
    work = newDynArr(sizeof(s));
    char testChar = 0;
    
    do{
        testChar = nextChar(s);
        
        if (testChar == '(') {
            pushDynArr(work, ')');
        }
        else if (testChar == ')') {
            if (isEmptyDynArr(work)) {
                return 0;
            }
        else if (testChar != topDynArr(work)) {
                return 0;
            }
        else
            popDynArr(work);
        }
        
        else if (testChar == '[') {
            pushDynArr(work, ']');
        }
        else if (testChar == ']') {
            if (isEmptyDynArr(work)) {
                return 0;
            }
            else if (testChar != topDynArr(work)) {
                return 0;
            }
            else
                popDynArr(work);
        }
        
        else if (testChar == '{') {
            pushDynArr(work, '}');
        }
        else if (testChar == '}') {
            if (isEmptyDynArr(work)) {
                return 0;
            }
            else if (testChar != topDynArr(work)) {
                return 0;
            }
            else
                popDynArr(work);
        }
    } while (testChar != '\0');

    if (!isEmptyDynArr(work))
        return 0;
	return 1;
}
Ejemplo n.º 4
0
Archivo: calc.c Proyecto: dot1q/Misc
void power (struct DynArr *stack)
{	
        double power;
	checkArrSize(stack);
        power = pow( stack->data[stack->size-2],  stack->data[stack->size-1]);
        popDynArr(stack);
	popDynArr(stack);
        addDynArr(stack, power);
        printf(" \n^\n");
}
Ejemplo n.º 5
0
Archivo: calc.c Proyecto: dot1q/Misc
void multiply (struct DynArr *stack)
{
        double multiply;
        checkArrSize(stack);
        multiply = stack->data[stack->size-2] * stack->data[stack->size-1];
        popDynArr(stack);
        popDynArr(stack);
        addDynArr(stack, multiply);
        printf(" \n*\n");
}
Ejemplo n.º 6
0
Archivo: calc.c Proyecto: dot1q/Misc
/*	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)
{
	double divide;
        checkArrSize(stack);
        divide = stack->data[stack->size-2] / stack->data[stack->size-1];
        popDynArr(stack);
        popDynArr(stack);
        addDynArr(stack, divide);
        printf(" /\n");
}
Ejemplo n.º 7
0
Archivo: calc.c Proyecto: dot1q/Misc
/*	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)
{
	double sum;
	checkArrSize(stack);
	sum = stack->data[stack->size-2] +  stack->data[stack->size-1];
	popDynArr(stack);
	popDynArr(stack);	
	addDynArr(stack, sum);
	//printf("size of stack is %d and temp sum is %f and stack value at size is %f\n",stack->size, sum, stack->data[stack->size-1]);
	printf(" +\n");
}
Ejemplo n.º 8
0
Archivo: calc.c Proyecto: dot1q/Misc
/*	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 minus;
	minus =  stack->data[stack->size-2] - stack->data[stack->size-1];
	popDynArr(stack);
	popDynArr(stack);
	addDynArr(stack, minus);
	//printf("size of stack is %d and temp minus is %f and stack value at size is %f\n",stack->size, minus, stack->data[stack->size-1]);
	printf(" -\n");
}
Ejemplo n.º 9
0
/*	param: stack is 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 */
	TYPE temp;
	TYPE temp2;
	TYPE temp3;
	temp = peekDynArr(stack);
	popDynArr(stack);
	temp2=peekDynArr(stack);
	popDynArr(stack);
	temp3=temp2-temp;
	pushDynArr(stack, temp3);
}
Ejemplo n.º 10
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string 	
	pre: s is not null	
	post:	
*/
int isBalanced(char* s)
{
	/* FIXME: You will write this function */		
	struct DynArr *arr = newDynArr(7); 
 	
	char a;
	
	if (s != 0)
	{	
		while (a = nextChar(s) != '\0')
		{
			
			if (&a ==  "(")
				pushDynArr(arr, a);		
			else if (&a == "{")
				pushDynArr(arr, a);
		
			else if (&a == "[")
                                pushDynArr(arr, a);
		 	
			else if (&a == ")")
				{
					if(topDynArr(arr) == '(')
						popDynArr(arr);
					else
						return 0;
				}
			else if (&a == "}")
				{
                                        if(topDynArr(arr) == '{')
                                                popDynArr(arr);
                                        else
                                                return 0;
                                }
			else if (&a == "]")
                                {
                                        if(topDynArr(arr) == '[')
                                                popDynArr(arr);
                                        else
                                                return 0;
                                }

			
		}

				

		
		
	}
	
	return 0;
}
Ejemplo n.º 11
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 power(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE secondNum = topDynArr(stack);
	popDynArr(stack);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = pow(firstNum, secondNum);
	pushDynArr(stack, sum);
}
Ejemplo n.º 12
0
/*	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)
{
	assert(stack != 0);

	TYPE secondNum = topDynArr(stack);
	popDynArr(stack);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = firstNum - secondNum;
	pushDynArr(stack, sum);
}
Ejemplo n.º 13
0
/*	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)
{
	assert(stack != 0);

	TYPE secondNum = topDynArr(stack);
	popDynArr(stack);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = firstNum / secondNum;
	pushDynArr(stack, sum);
}
Ejemplo n.º 14
0
/*	param: stack the stack being manipulated
pre: the stack contains at least two elements
post: the top two elements are popped and
their product is pushed back onto the stack.
*/
void multiply(struct DynArr *stack)
{
	assert(stack != 0);

	TYPE secondNum = topDynArr(stack);
	popDynArr(stack);

	TYPE firstNum = topDynArr(stack);
	popDynArr(stack);

	TYPE sum = firstNum * secondNum;
	pushDynArr(stack, sum);
}
Ejemplo n.º 15
0
/*	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){
     if (sizeDynArr(stack) < 2){
        printf( "\n ERROR \n");
        printf("You need to have at least 2 numbers for subtraction \n");
        printf("Please use:  number1 number2 - format \n");
        printf("number1 - number2 will not work \n");
    }
     assert(sizeDynArr(stack) >= 2);
    double number = topDynArr(stack);
    popDynArr(stack);
    double subtract = topDynArr(stack) - number;
    popDynArr(stack);
    pushDynArr(stack, subtract);
}
Ejemplo n.º 16
0
/*	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){
    if (sizeDynArr(stack) < 2){
    printf( "\n ERROR \n");
    printf("You need to have at least 2 numbers for division\n");
    printf("Please use:  number1 number2 / format \n");
    printf("number1 / number2 will not work \n");
    }
    assert(sizeDynArr(stack) >= 2);
    double number = topDynArr(stack);
    popDynArr(stack);
    double divide = topDynArr(stack)/number;
    popDynArr(stack);
    pushDynArr(stack, divide);
}
Ejemplo n.º 17
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){
    if (sizeDynArr(stack) < 2){
        printf( "\n ERROR \n");
        printf("You need to have at least 2 numbers for addition \n");
        printf("Please use:  number1 number2 + format \n");
        printf("number1 + number2 will not work \n");
    }
    assert(sizeDynArr(stack) >= 2);
    double first = topDynArr(stack);
    popDynArr(stack);
    double second = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, first + second);
}
Ejemplo n.º 18
0
/* 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 multiply(struct DynArr *stack){
    if (sizeDynArr(stack) < 2){
    printf( "\n ERROR \n");
    printf("You need to have at least 2 numbers for multiplication \n");
    printf("Please use:  number1 number2 x format \n");
    printf("number1 x number2 will not work \n");
    }
    assert(sizeDynArr(stack) >= 2);
    double num2 = topDynArr(stack);
    popDynArr(stack);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, num1 * num2);
}
Ejemplo n.º 19
0
void power(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 2) {
        TYPE firstNum = topDynArr(stack);
        popDynArr(stack);
        TYPE secondNum = topDynArr(stack);
        popDynArr(stack);
        
        TYPE result = pow(secondNum, firstNum);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
Ejemplo n.º 20
0
void time(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 2) {
        TYPE firstNum = topDynArr(stack);
        popDynArr(stack);
        TYPE secondNum = topDynArr(stack);
        popDynArr(stack);
        
        TYPE result = firstNum * secondNum;
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
Ejemplo n.º 21
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string
	pre: s is not null
	post:
*/
int isBalanced(char* s)
{
    char check;
    char cmp;
    DynArr *stck;
	stck = newDynArr(100);
    do
    {
        check = nextChar(s);

        if (check == '(' || check == '{' || check == '[')
        {
            pushDynArr(stck, check);
        }
        else if (check == ')' || check == '}' || check == ']')
        {
			if (isEmptyDynArr(stck))
			{
				return 0;
			}
            cmp = topDynArr(stck);
            if ((check == ')' && cmp == '(') || (check == '}' && cmp == '{') || (check == ']' && cmp == '['))
            {
                popDynArr(stck);
            }
        }
    } while (check != '\0');
    if (isEmptyDynArr(stck))
    {
        return 1;
    }
    else
        return 0;
}
Ejemplo n.º 22
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string 	
	pre: s is not null	
	post:	
*/
int isBalanced(char* s)
{
	
	char c;
	DynArr *Parens;
	Parens = newDynArr(5); // Arbitrary first length

	//printf("DEBUG 40 s = %p\n", s);
	while ( (c = nextChar(s)) != '\0' ) 
	{
		if (EQ(c, '(')) pushDynArr(Parens, ')');
		else if (EQ(c, '{')) pushDynArr(Parens, '}');
		else if (EQ(c, '[')) pushDynArr(Parens, ']');
		else if (EQ(c, ')') || EQ(c,'}') || EQ(c, ']'))
		{
			if(isEmptyDynArr(Parens))
				return 0; // We already know we're unbalanced
			else
				if ( EQ(topDynArr(Parens), c)) popDynArr(Parens);
		}
	}

	if (isEmptyDynArr(Parens))
		return 1;  // True, is balanced
  else
		return 0; // False, is not balanced
}
Ejemplo n.º 23
0
Archivo: calc.c Proyecto: dot1q/Misc
void square (struct DynArr *stack)
{
        double square;
        square = pow(stack->data[stack->size-1], 2);
        popDynArr(stack);
        addDynArr(stack, square);
        printf(" \nsquare\n");
}
Ejemplo n.º 24
0
Archivo: calc.c Proyecto: dot1q/Misc
void abso (struct  DynArr *stack)
{
        double abso;
        abso = fabs(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, abso);
        printf(" \nabs\n");
}
Ejemplo n.º 25
0
Archivo: calc.c Proyecto: dot1q/Misc
void sqf (struct DynArr *stack)
{
        double sqVar;
        sqVar = sqrt(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, sqVar);
        printf(" \nsqrt\n");
}
Ejemplo n.º 26
0
Archivo: calc.c Proyecto: dot1q/Misc
void expo (struct DynArr *stack)
{
        double ex;
        ex = exp(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, ex);
        printf(" \nexp\n");
}
Ejemplo n.º 27
0
Archivo: calc.c Proyecto: dot1q/Misc
void logg (struct DynArr *stack)
{
        double logger;
        logger = log(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, logger);
        printf(" \nln\n");
}
Ejemplo n.º 28
0
Archivo: calc.c Proyecto: dot1q/Misc
void logg10(struct DynArr *stack)
{
        double log;
        log = log10(stack->data[stack->size-1]);
        popDynArr(stack);
        addDynArr(stack, log);
        printf(" \nlog10\n");
}
Ejemplo n.º 29
0
Archivo: calc.c Proyecto: dot1q/Misc
void cube (struct DynArr *stack)
{
        double cube;
        cube = pow(stack->data[stack->size-1], 3);
        popDynArr(stack);
        addDynArr(stack, cube);
        printf(" \ncube\n");
}
Ejemplo n.º 30
0
/*	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 */
    if (sizeDynArr(stack) >= 2) {
        TYPE firstNum = topDynArr(stack);
        popDynArr(stack);
        TYPE secondNum = topDynArr(stack);
        popDynArr(stack);
        
        TYPE result = secondNum / firstNum;
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}