Ejemplo n.º 1
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void absolute(struct DynArr *stack)
{
	assert(stack != 0);

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

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

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

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

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

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

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

	TYPE sum = log(firstNum);
	pushDynArr(stack, sum);
}
Ejemplo n.º 5
0
/* Checks whether the parentheses are balanced or not
	param: 	s pointer to a string
	pre: s is not null
	post:
*/
int isBalanced(char* s)
{
    struct DynArr *array = newDynArr(10);
    char c;
    while((c = nextChar(s)) != '0'){
        switch(c){
            case '{' :
            case '[' :
            case '(' :
                pushDynArr(array, c);
                break;

            case '}' :
                if(topDynArr(array) == '{'){
                    popDynArr(array);
                    break;
                }
                return 0;

            case ']' :
                if(topDynArr(array) == '['){
                    popDynArr(array);
                    break;
                }
                return 0;

            case ')' :
                if(topDynArr(array) == '('){
                    popDynArr(array);
                    break;
                }
                return 0;

        }
    }
    if(array->size != 0){
        return 0;
    }
    deleteDynArr(array);
        return 1;

}
Ejemplo n.º 6
0
void squareRoot(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = sqrt(topDynArr(stack));
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
Ejemplo n.º 7
0
void baseLog(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = log10(topDynArr(stack));
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
Ejemplo n.º 8
0
void exponential(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = exp(topDynArr(stack));
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
Ejemplo n.º 9
0
void cubing(struct DynArr *stack) {
    if (sizeDynArr(stack) >= 1) {
        TYPE result = pow(topDynArr(stack), 3);
        popDynArr(stack);
        pushDynArr(stack, result);
        
        flag = 1;
    }
    else
        flag = 0;
}
Ejemplo n.º 10
0
/*	param: stack the stack being manipulated
pre: the stack contains at least one element
post: the top two elements are popped and
their sum is pushed back onto the stack.
*/
void cube(struct DynArr *stack)
{
	assert(stack != 0);

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

	TYPE secondNum = 3;

	TYPE sum = pow(firstNum, secondNum);
	pushDynArr(stack, sum);
}
Ejemplo n.º 11
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 = ' ';
	int ret_val = 0;
	struct DynArr* stack = newDynArr(10);
	assert(stack != 0);
	assert(s != 0);
	while (c != '\0'){
		ret_val = 1;
		c = nextChar(s);

		if (c == '(' || c == '[' || c == '{') pushDynArr(stack,c);
		if (c == ')' || c == ']' || c == '}') {
			if (c == ')' && topDynArr(stack) == '(') popDynArr(stack);
			else if (c == ']' && topDynArr(stack) == '[') popDynArr(stack);
			else if (c == '}' && topDynArr(stack) == '{') popDynArr(stack);
			else return 0;
		}
	}
	if (ret_val) ret_val = !(isEmptyDynArr(stack));
	printf("return value %d\n", ret_val);
	deleteDynArr(stack);
	return ret_val;
}
Ejemplo n.º 12
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 */

    /* Check if s is null */
    assert(s);    

    /* Create stack to hold letters */
    DynArr* stack = newDynArr(16);
    char c = nextChar(s);
		
    while(c)
    {
        if(c == '(' || c == '{' || c == '[')
            pushDynArr(stack, c);

        else if(c == ')' || c == '}' || c == ']')
        {
            int balanced = 0;

            if(!isEmptyDynArr(stack))
            {
                char top = topDynArr(stack);

                if(top == '(' && c == ')')
                    balanced = 1;

                else if(top == '{' && c == '}')
                    balanced = 1;

                else if(top == '[' && c == ']')
                    balanced = 1;
            }

            if(balanced)
                popDynArr(stack);

            else
                return 0;
        }

        c = nextChar(s);
    }

	return isEmptyDynArr(stack);
}
Ejemplo n.º 13
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least 1 element
	post: the top element is popped, log10'd,
	and is pushed back onto the stack.
*/
void logTen(struct DynArr *stack)
{
	//declare necessary doubles for operator use on stack
	double operand_1, logTen_result;

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

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

	//store in result
	logTen_result = log10(operand_1);

	//push back onto stack
	pushDynArr(stack, logTen_result);
}
Ejemplo n.º 14
0
/*	param: stack the stack being manipulated
	pre: the stack contains at least 1 element
	post: the top element is popped, 'absolute valued',
	and is pushed back onto the stack.
*/
void absoluteValue(struct DynArr *stack)
{
	//declare necessary doubles for operator use on stack
	double operand_1, abs_result;

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

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

	//store in result
	abs_result = abs(operand_1);

	//push back onto stack
	pushDynArr(stack, abs_result);
}
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 quotient is pushed back onto the stack.
*/
void exponential(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 1);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, exp(num1));
}
Ejemplo n.º 16
0
int main(int argc, char* argv[]){
	
	DynArr *dyn;
	dyn = createDynArr(2);
	double a,b,c,d,e,f,g,h;
	a = 3;
	b = 4;
	c = 10;
	d = 6;
	e = 5; 
	f = 20;
	printf("\n\nTesting addDynArr...\n");
	addDynArr(dyn, a);
	addDynArr(dyn, b);
	addDynArr(dyn, c);
	addDynArr(dyn, d);
	addDynArr(dyn, e);
	
	printf("The array's content: [3,4,10,6,5]\n");
	assertTrue(EQ(getDynArr(dyn, 0), a), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 1), b), "Test 2nd element == 4");
	assertTrue(EQ(getDynArr(dyn, 2), c), "Test 3rd element == 10");
	assertTrue(EQ(getDynArr(dyn, 3), d), "Test 4th element == 5");
	assertTrue(EQ(getDynArr(dyn, 4), e), "Test 5th element == 6");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting add...\nCalling addDynArr(dyn)\n");
	add(dyn); 
	printf("The array's content: [3,4,7,6,5]\n");
	assertTrue(EQ(getDynArr(dyn, 3), (double)11), "Test 3rd element == 11");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
	
	//removing result of add test and restoring array
	removeDynArr(dyn, 3);
	addDynArr(dyn, d);
	addDynArr(dyn, e);
	
	printf("\n\nTesting sub...");
	subtract(dyn); 
	printf("The array's content: [3,4,7,6,5]\n");
	assertTrue(EQ(getDynArr(dyn, 3), (double)1), "Test 3rd element == 1");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");	
	//printf("%d \n", sizeDynArr(dyn));
	printf("Top: %f \n", topDynArr(dyn));	
		//removing result of add test and restoring array
	popDynArr(dyn);
	printf("Top: %f \n", topDynArr(dyn));
	pushDynArr(dyn, f);
	pushDynArr(dyn, e);
	printf("Top: %f \n", topDynArr(dyn));
	
	printf("\n\nTesting divide...");
	divide(dyn); 
	//printf("The array's content: [3,4,10,20,5]\n");
	//assertTrue(EQ(getDynArr(dyn, 3),(double)4), "Test 3rd element == 4");
	//assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");	
/* 	printf("At 4: %f \n", topDynArr(dyn));
	removeDynArr(dyn,4);
	printf("At 3: %f \n", topDynArr(dyn));
	removeDynArr(dyn,3);
	printf("At 2: %f \n", topDynArr(dyn));	
	removeDynArr(dyn,2);
	printf("At 1: %f \n", topDynArr(dyn));	
	printf("%d \n",sizeDynArr(dyn)); */
	
			//removing result of add test and restoring array
	printf("\nTop: %f \n", topDynArr(dyn));
	popDynArr(dyn);
	printf("Top: %f \n", topDynArr(dyn));
	pushDynArr(dyn, f);
	pushDynArr(dyn, e);
	printf("Top: %f \n", topDynArr(dyn));
	
	printf("\n\nTesting multiply...\nCalling addDynArr(dyn)\n");
	multiply(dyn); 
	//printf("The array's content: [3,4,10,6,5]\n");
	//assertTrue(EQ(getDynArr(dyn, 3),(float)30), "Test 3rd element == 30");
	//assertTrue(sizeDynArr(dyn) == (float)4, "Test size = 4");
	
	
	printf("Before pop Top: %f \n", topDynArr(dyn));			//removing result of add test and restoring array
	popDynArr(dyn);
	printf("After pop Top: %f \n", topDynArr(dyn));
	pushDynArr(dyn,(double) 2);
	pushDynArr(dyn,(double) 1);
	printf("After 2 push Top: %f \n", topDynArr(dyn));	
	printf("\n\nTesting power of...n");
	powerOf(dyn); 
	//printf("The array's content: [3,4,10,6,5]\n");
	//assertTrue(EQ(getDynArr(dyn, 3),(float)2), "Test 3rd element == 2");
	//assertTrue(sizeDynArr(dyn) == (float)4, "Test size = 4");
	


	
}
Ejemplo n.º 17
0
/*	param: integer size of input and char
pre: argc is not equal to 1
post: the final number is calculated without errors. 
*/
double calculate(int numInputTokens, char **inputString)
{
	int i;
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	//set up the stack
	stack = createDynArr(20);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++) 
	{
		s = inputString[i];

		if (strcmp(s, "+") == 0)			// add
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to add together
			{
				add(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "-") == 0)		// subtract
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to subtract together
			{
				subtract(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "/") == 0)		// divide
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to divide together
			{
				divide(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "x") == 0)		// multiply
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to multiply together
			{
				multiply(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "^") == 0)		// calculate power
		{
			if (sizeDynArr(stack) >= 2)		// make sure stack has two numbers to use for power
			{
				power(stack);
			}
			else
			{
				printf("Not enough numbers. %s is causing an error. Exiting.\n", s);
				exit(0);
			}
		}
		else if (strcmp(s, "^2") == 0)		// calculate square
		{
			square(stack);
		}
		else if (strcmp(s, "^3") == 0)		// calculate cube
		{
			cube(stack);
		}
		else if (strcmp(s, "abs") == 0)		// calculate absolute
		{
			absolute(stack);
		}
		else if (strcmp(s, "sqrt") == 0)	// calculate square root
		{
			squareRoot(stack);
		}
		else if (strcmp(s, "exp") == 0)		// calculate exponetial
		{
			exponential(stack);
		}
		else if (strcmp(s, "ln") == 0)		// calculate natural log
		{
			naturalLog(stack);
		}
		else if (strcmp(s, "log") == 0)		// get log
		{
			baseTenLog(stack);
		}
		else 
		{
			if (strcmp(s, "pi") == 0)			// convert pi to a number
			{
				s = "3.14159265";
			}
			else if (strcmp(s, "e") == 0)		// convert e to a number
			{
				s = "2.7182818";
			}

			if (isNumber(s, &result) != 0)		// if a number see if you can push on stack
			{
				pushDynArr(stack, result);	
			}
			else                               // exit if not a number
			{
				printf("Error. Following input causing an error: %s \n", s);
				exit (0);
			}
			
		}
	}	

	assert(!isEmptyDynArr(stack));		// check to make sure stack isn't empty
	if (sizeDynArr(stack) > 1)
	{
		printf("Not enough operators. Too many numbers. ");
		for (int i = 1; i < numInputTokens; i++)
		{
			printf("%s ", inputString[i]);
		}
		printf(" \n");

		exit(0);
	}

	result = topDynArr(stack);			// get final number and make it it result

	return result;
}
Ejemplo n.º 18
0
Archivo: calc.c Proyecto: dot1q/Misc
double calculate(int numInputTokens, char **inputString)
{
	int i, limit=0;
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	//set up the stack
	stack = createDynArr(20);
	
	printf("testing info! numInputTOkesn: %d\n", numInputTokens);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++) 
	{
		s = inputString[i];

		// Hint: General algorithm:
		// (1) Check if the string s is in the list of operators.
		//   (1a) If it is, perform corresponding operations.
		//   (1b) Otherwise, check if s is a number.
		//     (1b - I) If s is not a number, produce an error.
		//     (1b - II) If s is a number, push it onto the stack

		if(strcmp(s, "+") == 0){
			add(stack);
			limit = 0; }
		else if(strcmp(s,"-") == 0){
			subtract(stack);
			limit = 0; }
		else if(strcmp(s, "/") == 0){
			divide(stack);
			limit = 0; }
		else if(strcmp(s, "x") == 0){
			multiply(stack);
			limit = 0; }
		else if(strcmp(s, "^") == 0){
			power(stack);
			limit = 0;
			}
		else if(strcmp(s, "^2") == 0){
			square(stack);
			limit = 0;
			}
		else if(strcmp(s, "^3") == 0 ){
			cube(stack);
			limit = 0;
			}
		else if(strcmp(s, "abs") == 0){
			abso(stack);
			limit = 0;
			}
		else if(strcmp(s, "sqrt") == 0){
			sqf(stack);
			limit = 0;
			}
		else if(strcmp(s, "exp") == 0){
			expo(stack);
			limit = 0;
			}
		else if(strcmp(s, "ln") == 0){
			logg(stack);
			limit = 0;
			}
		else if(strcmp(s, "log") == 0){
			logg10(stack);
			limit = 0;
			}
		else 
		{
			if(limit >=2){
				printf("\nLimit has been reached! You did something wrong!\n");
				break;
			}
			//printf("%c is not an operator! converting to a a number!\n",*s);
			int isNum;
			double num = 0;
			isNum = isNumber(s, &num);
			if(isNum == 1){
				//Is infact a number, push to stack
				pushDynArr(stack, num);
				limit++;
				//printf("value %f is now pushed to the statck\n", num);
				printf("\n%f", num);
				
			}else{
				if(strcmp(s, "pi") == 0){
					pushDynArr(stack, 3.144159265);
					printf("\n3.144159265");
					limit++;
				}else if(strcmp(s, "e") == 0){
					pushDynArr(stack, 2.7182818);
					limit++;
					printf("\n2.7182818");
				}else{
					//ignore because it is not a number
					printf("\nPosition %d is not a number, ignoring...\n", i+1);
				}
			}
				//limit is more than 2, this should not happen!
			// Remember to deal with special values ("pi" and "e")
			
		}
	}	//end for 
	
	/* FIXME: You will write this part of the function (2 steps below) 
	 * (1) Check if everything looks OK and produce an error if needed.
	 * (2) Store the final value in result and print it out.
	 */
	if( stack->size > 1){
		//something went wrong
		printf("\nError computing value, check your entry!\n");
	}else{
	result = topDynArr(stack);
	}
	
	return result;
}
Ejemplo n.º 19
0
/* Checks whether the (), {}, and [] are balanced or not
	param: 	s pointer to a string
	pre: s is not null
	post: Returns 1 or 0 and displays corresponding response in main
*/
int isBalanced(char* s)
{
	/* FIXME: You will write this function */
	if (s == 0)
		return 0;
	char ch;

	struct DynArr *balArray = newDynArr(10);
	while((ch = nextChar(s)) != '\0')
	{
		
		//printf("%c\n",ch);	//test statement 
		
		//Push when ch = [,{,( 
		if(ch == '[' || ch == '(' || ch == '{')
			pushDynArr(balArray, ch);

		//Pop when ],},) and when array is not empty
		if(ch == ']' || ch == ')' || ch == '}')
		{		
			if (isEmptyDynArr(balArray))
				return 0;
			//Check the individual situations and pop if matching
			if (ch == ']')
			{
				if (topDynArr(balArray) == '[')
				{
					popDynArr(balArray);
					

				}
				else
					return 0;
				
			}
			else if (ch == '}') 
			{
				if (topDynArr(balArray) == '{')
				{
					popDynArr(balArray);
				}
				else
					return 0;
			}
			else if (ch == ')')
			{
				if (topDynArr(balArray) == '(')
				{
					popDynArr(balArray);
				}
				else
					return 0;
			}
			
				
		}
		//printf("%d", sizeDynArr(balArray));//  test statement
		//printf("Top of Stack: %c\n", topDynArr(balArray));// test statement
	}
	
	 	

		
	if (!isEmptyDynArr(balArray))
	{
		return 0;
	}
	else
	{
		deleteDynArr(balArray);
		return 1;
	}

	
}
Ejemplo n.º 20
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 cubed(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 1);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, pow(num1,3));
}
Ejemplo n.º 21
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 absoluteValue(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 1);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, fabs(num1));
}
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)
{
	
    assert(s != NULL);
    
    int returnValue = 0;
    char testChar;

    struct DynArr *stringBalance = newDynArr((sizeof(s)/sizeof('a'))+1);
    do{

	   testChar = nextChar(s);
	   if(testChar == '(' || testChar == '{' ||testChar == '[' ){
		  pushDynArr(stringBalance, testChar);
		  returnValue = 0;
	   }else if(testChar == ')' || testChar == '}' ||testChar == ']' ){
		  
		  if(isEmptyDynArr(stringBalance)){ //if the first match is a close then the string is imbalanced
			 returnValue = 0;
			 break;
		  }
		  

		  char top = topDynArr(stringBalance);
		  switch(testChar){
			 
		  case ')':
			 if( top == '('){
				popDynArr(stringBalance);
				returnValue = 1;
			 }else {
				returnValue = 0;
				testChar = '\0';
			 }
			 break;
		  case '}':
			 if( top == '{'){
				popDynArr(stringBalance);
				returnValue = 1;
			 }else {
				returnValue = 0;
				testChar = '\0';
			 }
			 break;
		case ']':
			 if( top == '['){
				popDynArr(stringBalance);
				returnValue = 1;
			 }else {
				returnValue = 0;
				testChar = '\0';
			 }
			 break;
		  }

	   }else if(testChar == '\0'){
		  if(!isEmptyDynArr(stringBalance)){//if at the end of the string it is not empty then it is false
			 returnValue = 0;
		  }
	   }

    }while(testChar != '\0');

    return returnValue;
}
Ejemplo n.º 23
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 squareRoot(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 1);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, sqrt(num1));
}
Ejemplo n.º 24
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)
{
	
	int curlyCount = 0;
	int squareCount = 0;
	int perenCount = 0;
        int i;
	/* FIXME: You will write this function */
       /*for(j = 0; s[j]; j++){
                size++;
		
	}*/     
        	
	                         
        DynArr stack = *newDynArr(strlen(s)+1);
        stack.size = strlen(s);
	   	
	for(i=0; i < strlen(s); i++){
		
		stack.size--;	
	
	}
        
        for(i=0; i < strlen(s); i++){
       		  pushDynArr(&stack, s[i]);        	
	   
			
  		switch(topDynArr(&stack)){
			case '{':
                        curlyCount++;
			popDynArr(&stack);
			break;
	
			case '}':
			curlyCount--;
			popDynArr(&stack);
			break;

			case '[':
			squareCount++;
			popDynArr(&stack);
			break;

			case ']':
			squareCount--;
			popDynArr(&stack);
			break;

			case '(':
			perenCount++;
			popDynArr(&stack);
			break;
			
			case ')':
			perenCount--;
			popDynArr(&stack);
			break;

			default:
		        popDynArr(&stack);
			break;
 	       }
      
        
        }
        if((curlyCount==0)&&(squareCount == 0)&&(perenCount==0)){		
       	        return 1; /*It is balanced*/
     	}   
	return 0; /*Is not balanced*/
}
Ejemplo n.º 25
0
double calculate(int numInputTokens, char **inputString)
{
	int i;
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	//for keeping track of # of operands and operators
	int numOfOperands = 0;
	int numOfOperators = 0;
	int check = 0;

	//set up the stack
	stack = createDynArr(20);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++) 
	{
		s = inputString[i];

		if(strcmp(s, "+") == 0) {
			numOfOperators++;		//increase for binary operators
			add(stack);
		}
		else if(strcmp(s,"-") == 0) {
			numOfOperators++;
			subtract(stack);
		}
		else if(strcmp(s, "/") == 0) {
			numOfOperators++;
			divide(stack);
		}
		else if(strcmp(s, "x") == 0) {
			numOfOperators++;
			multiply(stack);
		}
		else if(strcmp(s, "^") == 0) {
			numOfOperators = numOfOperators;	//for unary operators don't increase
			power(stack);
		}
		else if(strcmp(s, "^2") == 0) {
			numOfOperators = numOfOperators;
			square(stack);
		}
		else if(strcmp(s, "^3") == 0) {
			numOfOperators = numOfOperators;
			cube(stack);
		}
		else if(strcmp(s, "abs") == 0) {
			numOfOperators = numOfOperators;
			absoluteValue(stack);
		}
		else if(strcmp(s, "sqrt") == 0) {
			numOfOperators = numOfOperators;
			squareRoot(stack);
		}
		else if(strcmp(s, "exp") == 0) {
			numOfOperators = numOfOperators;
			exponential(stack);
		}
		else if(strcmp(s, "ln") == 0) {
			numOfOperators = numOfOperators;
			naturalLog(stack);
		}
		else if(strcmp(s, "log") == 0) {
			numOfOperators = numOfOperators;
			logTen(stack);
		}
		else 
		{
			//check if its a number (or pi or e)
			if(isNumber(s, &result)) {
				numOfOperands++;			//increase count of operands
				pushDynArr(stack, result); //if it is push onto stack
			}
			else {
			printf("You entered bad characters, exiting!\n"); //else print error message
			exit(0);
			}
			
		}
	}	//end for 

	//check for correct balance of operands and operators
	//must be one more operand than operators in sequence
	check = numOfOperands - numOfOperators; //this must be equal to 1 for valid input
	if (check > 1) {
		printf("Illegal Input, too many operands or too few operators, exiting\n");
		exit(0);
	}
	else if (check < 1) {
		printf("Illegal Input, too few operands or too many operators, exiting\n");
		exit(0);
	}

	//if passed check store final result and return
	result = topDynArr(stack);
	return result;
}
Ejemplo n.º 26
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 naturalLog(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 1);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, log(num1));
}
Ejemplo n.º 27
0
// this main function contains some
int main(int argc, char* argv[]){

	DynArr *dyn;
	dyn = newDynArr(2);
        int i; 

	printf("\n\nTesting addDynArr...\n");
	addDynArr(dyn, 3);
	addDynArr(dyn, 4);
	addDynArr(dyn, 10);
	addDynArr(dyn, 5);
	addDynArr(dyn, 6);
	
	printf("The array's content: [3,4,10,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
	assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
	assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
	assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
	putDynArr(dyn, 2, 7); 
	printf("The array's content: [3,4,7,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
	swapDynArr(dyn, 2, 4);
	printf("The array's content: [3,4,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
	assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
	
	printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
	removeAtDynArr(dyn, 1);
	printf("The array's content: [3,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
	
	printf("\n\nTesting stack interface...\n");
	printf("The stack's content: [3,6,5,7] <- top\n");
	assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
	assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
	
	popDynArr(dyn);
	printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
	
	pushDynArr(dyn, 9);
	printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
	
	printf("\n\nTesting bag interface...\n");
	printf("The bag's content: [3,6,5,9]\n");
	assertTrue(containsDynArr(dyn, 3), "Test containing 3");
	assertTrue(containsDynArr(dyn, 6), "Test containing 6");
	assertTrue(containsDynArr(dyn, 5), "Test containing 5");
	assertTrue(containsDynArr(dyn, 9), "Test containing 9");
	assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
	
	removeDynArr(dyn, 3);
	printf("Removing 3...\nThe stack's content: [6,5,9]\n");
	assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
	
	return 0;
}
Ejemplo n.º 28
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 logBase10(struct DynArr *stack){
    assert(sizeDynArr(stack) >= 1);
    double num1 = topDynArr(stack);
    popDynArr(stack);
    pushDynArr(stack, log10(num1));
}
Ejemplo n.º 29
0
// this main function contains some
int main(int argc, char* argv[]){

	DynArr *dyn;
	dyn = createDynArr(2);
	
	printf("\n\nTesting addDynArr...\n");
	addDynArr(dyn, 3);
	addDynArr(dyn, 4);
	addDynArr(dyn, 10);
	addDynArr(dyn, 5);
	addDynArr(dyn, 6);
	
	
	printf("The array's content: [3,4,10,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 1), 4), "Test 2nd element == 4");
	assertTrue(EQ(getDynArr(dyn, 2), 10), "Test 3rd element == 10");
	assertTrue(EQ(getDynArr(dyn, 3), 5), "Test 4th element == 5");
	assertTrue(EQ(getDynArr(dyn, 4), 6), "Test 5th element == 6");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting putDynArr...\nCalling putDynArr(dyn, 2, 7)\n");
	putDynArr(dyn, 2, 7); 
	printf("The array's content: [3,4,7,5,6]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 7), "Test 3rd element == 7");
	assertTrue(sizeDynArr(dyn) == 5, "Test size = 5");
	
	printf("\n\nTesting swapDynArr...\nCalling swapDynArr(dyn, 2, 4)\n");
	swapDynArr(dyn, 2, 4);
	printf("The array's content: [3,4,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 2), 6), "Test 3rd element == 6");
	assertTrue(EQ(getDynArr(dyn, 4), 7), "Test 5th element == 7");
	
	printf("\n\nTesting removeAtDynArr...\nCalling removeAtDynArr(dyn, 1)\n");
	removeAtDynArr(dyn, 1);
	printf("The array's content: [3,6,5,7]\n");
	assertTrue(EQ(getDynArr(dyn, 0), 3), "Test 1st element == 3");
	assertTrue(EQ(getDynArr(dyn, 3), 7), "Test 4th element == 7");
	assertTrue(sizeDynArr(dyn) == 4, "Test size = 4");
	
	printf("\n\nTesting stack interface...\n");
	printf("The stack's content: [3,6,5,7] <- top\n");
	assertTrue(!isEmptyDynArr(dyn), "Testing isEmptyDynArr");
	assertTrue(EQ(topDynArr(dyn), 7), "Test topDynArr == 7");
	
	popDynArr(dyn);
	printf("Poping...\nThe stack's content: [3,6,5] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 5), "Test topDynArr == 5");
	
	pushDynArr(dyn, 9);
	printf("Pushing 9...\nThe stack's content: [3,6,5,9] <- top\n");
	assertTrue(EQ(topDynArr(dyn), 9), "Test topDynArr == 9");
	
	printf("\n\nTesting bag interface...\n");
	printf("The bag's content: [3,6,5,9]\n");
	assertTrue(containsDynArr(dyn, 3), "Test containing 3");
	assertTrue(containsDynArr(dyn, 6), "Test containing 6");
	assertTrue(containsDynArr(dyn, 5), "Test containing 5");
	assertTrue(containsDynArr(dyn, 9), "Test containing 9");
	assertTrue(!containsDynArr(dyn, 7), "Test not containing 7");
	
	removeDynArr(dyn, 3);
	printf("Removing 3...\nThe stack's content: [6,5,9]\n");
	assertTrue(!containsDynArr(dyn, 3), "Test not containing 3");
	
	printf("Executing test functions...\n");
	printf("\nTesting addDynArr()\n");
	addDynArr_TEST(dyn);  //Testing the add functionality under 3 conditions
	printf("\nTesting popDynArr()\n");
	popDynArr_TEST(dyn);  //Testing the pop function under 2 conditions
	printf("\nTesting topDynArr()\n");
	topDynArr_TEST(dyn);  //Testing the top function under 2 conditions
	printf("\nTesting pushDynArr()\n");
	pushDynArr_TEST(dyn); //Testing the push function under 3 conditions
	printf("\nTesting containsDynArr()\n");
	containsDynArr_TEST(dyn); //Testing the contains function under 2 conditions
	printf("\nTesting removeDynArr()\n"); 
	removeDynArr_TEST(dyn); //Testing the remove function under 2 conditions
	return 0;
}
Ejemplo n.º 30
0
double calculate(int numInputTokens, char **inputString){
	int i;
	double result = 0.0;
	char *s;
	struct DynArr *stack;
	double num;
	//set up the stack
	stack = createDynArr(20);

	// start at 1 to skip the name of the calculator calc
	for(i=1;i < numInputTokens;i++)
	{
		s = inputString[i];

		// Hint: General algorithm:
		// (1) Check if the string s is in the list of operators.
		//   (1a) If it is, perform corresponding operations.
		//   (1b) Otherwise, check if s is a number.
		//     (1b - I) If s is not a number, produce an error.
		//     (1b - II) If s is a number, push it onto the stack

		if(strcmp(s, "+") == 0){
			add(stack);
			printf("Adding\n");
		}
		else if(strcmp(s,"-") == 0){
			subtract(stack);
			printf("Subtracting\n");
		}
		else if(strcmp(s, "/") == 0){
			divide(stack);
			printf("Dividing\n");
		}
		else if(strcmp(s, "x") == 0){
			multiply(stack);
			printf("Multiplying\n");
		}
        else if(strcmp(s,"^") == 0){
			power(stack);
			printf("Power\n");
        }
		else if(strcmp(s, "^2") == 0){
			squared(stack);
			printf("Squaring\n");
		}
		else if(strcmp(s, "^3") == 0){
			cubed(stack);
			printf("Cubing\n");
		}
		else if(strcmp(s, "abs") == 0){
			absoluteValue(stack);
			printf("Absolute value\n");
		}
		else if(strcmp(s, "sqrt") == 0){
			squareRoot(stack);
			printf("Square root\n");
		}
		else if(strcmp(s, "exp") == 0){
			exponential(stack);
			printf("Exponential\n");
		}
		else if(strcmp(s, "ln") == 0){
			naturalLog(stack);
			printf("Natural Log\n");
		}
		else if(strcmp(s, "log") == 0){
			logBase10(stack);
			printf("Log\n");
		}

        else{
    // FIXME: You need to develop the code here (when s is not an operator)
    // Remember to deal with special values ("pi" and "e")
    //check if not a number
            if (isNumber(s, &num) == 0){
                if (strcmp(s, "pi") == 0){
                    num = 3.14159265;
                }
                else if (strcmp(s, "e") == 0){
                    num = 2.7182818;
                }
                else{	//wrong
                    printf("%s is not valid (number or operator) \n", s);
                    break;
                }
            }
            pushDynArr(stack, num);
        }
    }	//end for
/* FIXME: You will write this part of the function (2 steps below)
* (1) Check if everything looks OK and produce an error if needed.
* (2) Store the final value in result and print it out.
*/
    if (sizeDynArr(stack) != 1) {
        printf("Incorrect count of numbers is detected! Calculations CANNOT be preformed. ");
        return 0;
    }
    else {
        result = topDynArr(stack);
    }
    return result;
}