Esempio n. 1
0
void showMeFact( int n ) {
	Stack * s = makeStack( NULL );
	int i = n;
	int fact = 1;
	void ** frameVars;

	/*
	 * Push all frames onto the stack, emulating the process
	 * that would take place during recursion.
	 */
	for( ; i>=0; i-- ) {
		/*
		 * The next line is included to make clear the role of the base
		 * case test in this iterative implementation.
		 */
		if( i == 0 ) break;
		
		stackPush( s, 1, (uintptr_t)i );
	}

	/*
	 * Print thet stack after we hit our base cose to see what it
	 * actually looks like.
	 *
	 * We know we hit our base case here, because if we were writing
	 * a fact function recursively, we would stop at 0, which is where
	 * the loop ends.
	 *
	 * Add more calls to stackPrint around the function if you want to
	 * see the state of the stack at different points during the calculation.
	 */
	printf( "Stack contents at factorial base case:\n" );
	stackPrint( s );

	/*
	 * Pop the frames off of our stack, and perform the operation
	 * that we perform on the way back up from our base case.
	 */
	for( ; i < n; i++ ) {
		frameVars = stackPop( s );
		printf( "popped:\n" );
		fact *= (uintptr_t)frameVars[0];
		free( frameVars );

		stackPrint( s );
		printf( "Intermediate fact value: %d\n\n", fact ); 
	}

	printf( "\nfact %d: %d\n", n, fact );
	
}
Esempio n. 2
0
void looking (_node** tree, char** grid, char* string, stack* stack, table* allWords)
{
    int f = searchDictionary(tree,string);
    int i;

    if (f != -1)
    {
        if (f) tableAdd(allWords,string);
        stackPrint(stack);
        neighbors* n = getNeighbors (stackTop(stack));

        for (i = 0; i < neighSize(n); ++i)
        {
            position* newPos = neighGet(n,i);
            if (!stackFind(stack,newPos))
            {
                stackPush(stack,newPos);
                char newString[strlen(string)+1];
                strcpy(newString,string);
                strcat(newString,&(grid[newPos->x][newPos->y]));

                looking(tree,grid,newString,stack,allWords);
            }
        }
    }
}
Esempio n. 3
0
/*
* Parses the input into commands and arguments using whitespace
* Then calls the associated function or error
* Maximum number of arguments is 5
*/
void parse(char *in)
{
	int argNum = 1;
	char *arg, *argArr[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
	arg = (char *) malloc(sizeof(char) * (strlen(in) + 1));

	/* Parse command */
	arg = strtok(in, " \r\n\t");
	argArr[0] = arg;
	if (argArr[0] == NULL) {
		fprintf(stderr, "ERROR: unknown command\n");
		return;
	}

	/* Parse arguments */
	arg = strtok(NULL, " \r\n\t");
	while (arg != NULL && argNum < 6) {
		if (strcmp(arg, " ") && strcmp(arg, "\r")
			&& strcmp(arg, "\n")) {
			argArr[argNum] = arg;
			argNum++;
		}
		arg = strtok(NULL, " \r\n\t");
	}

	/* Execute according to command */
	if (strcmp(argArr[0], "exit") == 0) {
		printf("EXITING SHELL...\n");
		exit(EXIT_SUCCESS);
	} else if (strcmp(argArr[0], "cd") == 0) {
		cmd_cd(argArr[1]);
	} else if (strcmp(argArr[0], "pushd") == 0) {
		char *oldwd = (char *) malloc(sizeof(char) * strlen(cwd));
		strcpy(oldwd, cwd);
		if (cmd_cd(argArr[1]) == 0)
			dirStack = stackPush(dirStack, oldwd);
	} else if (strcmp(argArr[0], "popd") == 0) {
		if (dirStack != NULL) {
			cmd_cd(dirStack->str);
			dirStack = stackPop(dirStack);
		} else {
			fprintf(stderr, "ERROR: stack is empty\n");
		}
	} else if (strcmp(argArr[0], "dirs") == 0) {
		stackPrint(dirStack);
	} else if (strcmp(argArr[0], "path") == 0) {
		if (argArr[1] == NULL && argArr[1] == NULL)
			listPrint(pathStack);
		else if (strcmp(argArr[1], "+") == 0)
			pathFind(1, argArr[2]);
		else if (strcmp(argArr[1], "-") == 0)
			pathFind(-1, argArr[2]);
		else
			fprintf(stderr, "ERROR: bad flag: %s\n", argArr[1]);
	} else {
		cmd_exec(argArr, argNum);
	}
};
Esempio n. 4
0
void execute()
{
    switch ( ir.op)
        {
        //lit
        case 1:
            lit(ir.m);
            break;
        // opr will require another sub-function to decide
        // which operation to run
        case 2:
            opr();
            break;
        //lod
        case 3:
            lod(ir.l, ir.m);
            break;
        //sto
        case 4:
            sto(ir.l, ir.m);
            break;
        //cal
        case 5:
            cal(ir.l, ir.m);
            break;
        //inc
        case 6:
            inc(ir.m);
            break;
        //jmp
        case 7:
            jmp(ir.m);
            break;
        //jpc
        case 8:
            jpc(ir.m);
            break;
        //sio
        //this will require another sub function to decide
        //which i/o to run
        case 9:
            sio();
            break;

        default:
            fprintf(output, "OP code input was invalid. ");
            sio3();
        }
        //print pc, bp, sp
        //use loop to print stack
        fprintf(output, "\t%d\t\t%d\t%d\t", pc, bp, sp);
        stackPrint();
}
Esempio n. 5
0
int main (int argc, char* argv[]) {
	printf ("C202 - Stack Implemented Using an Array - Basic Tests\n");
	printf ("-----------------------------------------------------\n");

	STACK_SIZE = 8;
	ptrstack = (tStack*) malloc(sizeof(tStack));
	stackUndef(ptrstack);

	printf("\n[TEST01] Stack initialization\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	use_stack_init(ptrstack);
	stackPrint(ptrstack);

	printf("\n[TEST02] Checking a state of the stack\n" );
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	use_stack_empty(ptrstack);
	use_stack_full(ptrstack);

	printf("\n[TEST03] Pushing a first item 'A'\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	use_stack_push (ptrstack, 'A');
	stackPrint(ptrstack);

	printf("\n[TEST04] Checking a state of the stack again\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	use_stack_empty(ptrstack);
	use_stack_full(ptrstack);

	printf("\n[TEST05] Reading and removing the top item\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	use_stack_top(ptrstack);
	use_stack_pop(ptrstack);
	stackPrint(ptrstack);

	printf("\n[TEST06] Pushing items from '0' to '7'\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	for (int i=0; i<8; i++)
		use_stack_push(ptrstack, '0'+i);
	stackPrint(ptrstack);

	printf("\n[TEST07] Checking a state of the stack\n" );
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	use_stack_empty(ptrstack);
	use_stack_full(ptrstack);

	printf("\n[TEST08] Removing all items one by one\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	for (int i=0; i<8; i++) {
    use_stack_top(ptrstack);
  	use_stack_pop(ptrstack);
  }
	stackPrint(ptrstack);

	printf("\n[TEST09] Final check of the stack\n" );
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	use_stack_empty(ptrstack);
	use_stack_full(ptrstack);
 
	printf("\n\n----- C202 - The End of Basic Tests -----\n");

	free(ptrstack);
	return(0);
}
Esempio n. 6
0
void showMeDepth( int n ) {
	Stack * s = makeStack( &printTreeVerticalMask );
	Tree * t;
	Tree * tmpNode;
	int i;
	char dir;
	void ** args;
	
	/* Populate random tree of size n */
	srand( time( 0 ) );
	srand( rand( ) );
	tmpNode = t = makeTree( );

	for( i=1; i<n; i++ ) {
		while( ( CHILD( tmpNode,
										/*Choose a random direction at every branch*/
										( dir = rand() % 2 ) ) ) )
			tmpNode = CHILD( tmpNode, dir );

		if( dir )
			tmpNode->right = makeTree( );
		else
			tmpNode->left  = makeTree( );
		
		tmpNode = t;
	}

	/* End tree generator */

	
	tmpNode = t;
	while( 1 ) {
		/* We've reached a leaf */
		if( !tmpNode ) {
			/* Popped last element(head) off the stack */
			if( !stackSize( s ) ) break;


			/* Print it for us(move this or add more for better resolution */
			stackPrint( s );
			printf( "\n" );
			/* Get back to parent */
			args = stackPop( s );

			switch ((uintptr_t)args[0]) {
			case 0: /* Been down left side. Visiting right */
				stackPush( s, 3, 1, i, args[2] );
				tmpNode = ((Tree *)args[2])->right;
				break;
			case 1: /* Done with left and right. finish up */
				/* i is whatever depth was for right side. left depth
					 is stored in args[1] */
				i = 1+ max( (uintptr_t)args[1], i );
				tmpNode = NULL;
				break;
			}
			
			free( args );
			continue;
		}

		/* If we're still going down a side of the tree, push this
			 node on the stack and continue onward. Worry about depth later */
		stackPush( s, 3, 0, 0, tmpNode );
		tmpNode = tmpNode->left;
		/* i is our temp counter for depth. Since we haven't reached
			 the bottom, don't start counting just yet. */
		i = 0;
	}

	freeTree( t );

	printf( "depth: %d\n", i );
}
Esempio n. 7
0
//Third standard i/o halts the system
void sio3()
{
    fprintf(output, "\t%d\t\t%d\t%d\t", pc, bp, sp);
    stackPrint();
    exit(0);
}