示例#1
0
void daRegressionTest()
{
	DynArr* DA = newDynArr();
	unsigned int i;
	int var[100];
	printf("============== daRegressionTest Begin ==============\n");
	for(i = 0; i < 100; i++)
	{
		daAdd(DA, (void*) &var[i]);
		assert(daGet(DA, i) == (void*) &var[i]);
		assert(daGetSize(DA) <= daGetCapacity(DA));
	}
	i = daGetSize(DA);
	daDel(DA, 50);
	assert(daGet(DA, 50) == (void*) &var[51]);
	assert(daGetSize(DA) == i - 1);
	daFastDel(DA, 50);
	assert(daGet(DA, 50) == (void*) &var[99]);
	assert(daGetSize(DA) == i - 2);
	delDynArr(DA);

	DA = newDynArr();
	daReserve(DA, 21);
	assert(daGetCapacity(DA) == 21);
	for(i = 0; i < 21; i++)
	{
		daAdd(DA, (void*) &var[i]);
		assert(daGet(DA, i) == (void*) &var[i]);
		assert(daGetSize(DA) <= daGetCapacity(DA));
	}
	assert(daGetCapacity(DA) == 21);
	delDynArr(DA);
	printf("============== daRegressionTest End ================\n");
}
示例#2
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;
}
示例#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)
{
	
	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
}
示例#4
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;
}
示例#5
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;
}
示例#6
0
文件: stackapp.c 项目: Merfoo/CS-261
/* 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);
}
示例#7
0
/* Resizes the underlying array to be the size cap

param: 	v		pointer to the dynamic array
param:	cap		the new desired capacity
pre:	v is not null
post:	v has capacity newCap
*/
void _dynArrSetCapacity(DynArr *v, int newCap)
{
	assert(v != 0);
	assert(newCap > v->capacity);

	DynArr* temp = newDynArr(newCap); //create a new array
	int size = sizeDynArr(v);	//get the size of the array

								//copy elements from old array into new array
	for (int i = 0; i < size; i++)
	{
		addDynArr(temp, v->data[i]);
	}

	freeDynArr(v); //deallocate memory tied to old array
	*v = *temp; //replace old array with new array by overwriting the original structure
	free(temp); //deallocate memory tied to the temporary struct
}
示例#8
0
文件: stackapp.c 项目: Mankee/CS261
/* 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;

}
int main(int argc, char* argv[])
{
	#ifdef MEMORY_TEST_INCLUDED
	// Memory used BEFORE creating LinkedList
	long m1 = getMemoryUsage();
	#endif

	if (argc != 2)
	{
		printf("Usage: %s <number of elements to add>\n", argv[0]);
		return 1;
	}
	
	DynArr *a = newDynArr(1024);
	
	int numElements = atoi(argv[1]);
	int i;
	for (i = 0 ; i < numElements; i++)
	{
		addDynArr(a, (TYPE)i);
	}

	#ifdef MEMORY_TEST_INCLUDED
	// Memory used AFTER creating LinkedList
	long m2 = getMemoryUsage();
	printf("Memory used by Dynamic Array : %ld KB \n", m2 - m1);
	#endif

	double t1 = getMilliseconds(); // Time before contains()
	for (i = 0; i < numElements; i++)
	{
		containsDynArr(a, i);
	}
	double t2 = getMilliseconds(); // Time after contains()
	printf("Time for running contains() on %d elements: %g ms\n", numElements, t2 - t1);

	deleteDynArr(a);

	return 0;
}
示例#10
0
文件: Menu.cpp 项目: posva/jump-n-run
void mnInit(Menu* M, s_SharedResources* SR)
{
	M->Menus = newDynArr();
	M->CurrentMenu = 0;
	M->PreviousMenu = 0;
	M->Active = TRUE;
	M->ItemHeight = 25.f;
	M->ItemSelectedZoomFactor = 1.2f;
	M->ItemNormalZoomFactor = 0.5f;
	M->spd[0] = 0.f;
	M->spd[1] = 0.f;
	M->MenuX = 0.f;
	M->MenuY = -100.f;
	M->SubAnim = 0.f;
	M->Force = 0.25f;
	M->Friction = 0.4f;
	M->Type = MENU_TYPE_DEFAULT;
	M->MessageScale = 0.f;
	M->Hide = FALSE;
	M->UseMouse = FALSE;
	M->SR = SR;
}
示例#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;
}
示例#12
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;
}
示例#13
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*/
}
示例#14
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;
}
示例#15
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;
	}

	
}
示例#16
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)
{
	DynArr* stack = newDynArr(DEFAULT_SIZE); //create a stack

	char bracket; //tracks bracket types
	char element; //tracks the current element in the given string

	//perform this loop until the end of the string is reached
	do
	{
		element = nextChar(s); //get the next element in the string

		//check the element to determine whether it is a closing or opening brace
		if (_isOpenBracket(element))
		{
			pushDynArr(stack, element); //push an opening brace onto the stack
		}
		else if (_isCloseBracket(element))
		{
			if (!isEmptyDynArr(stack)) //make sure the stack is not empty
			{
				bracket = topDynArr(stack); //check the top of the stack

				//make sure the brace at the top of the stack matches the right closing brace
				if ((bracket == '(' && element != ')') ||
					(bracket == '{' && element != '}') ||
					(bracket == '[' && element != ']'))
				{
					element = '\0'; //indicate the end the loop if the braces are mismatched
				}
				else
				{
					popDynArr(stack); //pop the top element off of the stack if the braces match
				}
			}
			else
			{
				/* 
				** if the stack is empty when a closing brace is encountered in the string, 
				** then free the stack and exit the function, indicating that the string was not balanced
				*/
				free(stack); 
				return 0;
			}
		}

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

	/*
	** if the stack still has elements after the end of the string has been reached, 
	** then free the stack and exit the function, indicating that the string was not balanced 
	*/
	if (sizeDynArr(stack) > 0)
	{
		free(stack);
		return 0;
	}

	//otherwise, free the stack and exit the function, indicating that the string is balanced 
	free(stack);
	return 1;
}