コード例 #1
0
ファイル: 4-4.c プロジェクト: markmontymark/knr-c
void calc_clear(stk_t * s)
{
	if( s == NULL)
		return;
	while( ! stk_is_empty(s) )
		stk_pop(s);
}
コード例 #2
0
ファイル: 4-4.c プロジェクト: markmontymark/knr-c
void calc_print_top(stk_t * s, FILE * f)
{
	if( stk_is_empty(s) )
	{
		fprintf(f,"Stack is empty, no top element\n");
		return;
	}
	fprintf(f, "Top element is %d\n", stk_get_top(s));
}
コード例 #3
0
ファイル: 4-4.c プロジェクト: markmontymark/knr-c
void calc_swap_top(stk_t * s)
{
	if( stk_is_empty(s) )
	{
		fprintf(stderr,"Stack is empty, can't swap top two elements\n");
		return;
	}
	stk_swap( s, 0, 1 );
	stk_print( s , stdout);
}
コード例 #4
0
ファイル: hw1p3.c プロジェクト: carmi/comp356
/*
 * Our idle function. Does a step of mergesort and quicksort, and calls the
 * display functions.
 *
 */
void update_lists(void) {
    // Mergesort part.
    // we only want to display if we've performed a merge.
    bool performed_a_merge = false;
    // if mergesort isn't finished, perform a step of it.
    if (!stk_is_empty(mergesort_stack)) {
        Node *cur_node = pop(mergesort_stack);
        State cur_state = cur_node->state;
        if (cur_state == unsorted) {
            size_t cur_size = cur_node->size;
            // If length 0 or 1, change state to sorted and put back on stack.
            if (cur_size == 1 || cur_size == 0) {
                cur_node->state = sorted;
                push(mergesort_stack, cur_node);
            }
            else {
                // Split and place back on stack.
                // Make two new arrays, and fill them.
                size_t first_array_size = (cur_size / 2);
                size_t second_array_size = (cur_size - first_array_size);
                int *first = malloc(first_array_size * sizeof(int));
                int *second = malloc(second_array_size * sizeof(int));
                half(cur_node->array, first, first_array_size, second,
                    second_array_size);
                // Push new arrays on stack as nodes.
                Node *first_node = make_node(first, first_array_size,
                    unsorted);
                int cur_start_index = cur_node->start_index;
                first_node->start_index = cur_start_index;
                first_node->end_index = cur_start_index + first_array_size - 1;
                push(mergesort_stack, first_node);
                Node *second_node = make_node(second, second_array_size,
                    unsorted);
                second_node->start_index = cur_start_index + first_array_size;
                second_node->end_index = cur_node->end_index;
                push(mergesort_stack, second_node);

                free_node(cur_node);
            }
        } else if (cur_state == sorted) {
            if (!stk_is_empty(mergesort_stack)) {
                // If stack isn't empty, get next_node and merge or swap.
                Node *next_node = pop(mergesort_stack);
                // If next_node's sorted, merge them together; else switch
                // them.
                if (next_node->state == sorted) {
                    // Merge them into a new node.
                    size_t first_half_size = cur_node->size;
                    size_t second_half_size = next_node->size;
                    size_t merged_size = first_half_size + second_half_size;
                    int *merged_array = malloc(merged_size * sizeof(int));
                    merge(cur_node->array, first_half_size, next_node->array,
                        second_half_size, merged_array, compare);
                    Node *merged_node = make_node(merged_array, merged_size,
                        sorted);
                    merged_node->start_index = cur_node->start_index;
                    merged_node->end_index = next_node->end_index;
                    push(mergesort_stack, merged_node);
                    free_node(cur_node);
                    free_node(next_node);
                    performed_a_merge = true;
                } else if (next_node->state == unsorted) {
                    // Swap position of nodes.
                    push(mergesort_stack, cur_node);
                    push(mergesort_stack, next_node);
                } else {
                    printf("Unrecognized state.");
                }
            } else {
                free_node(cur_node);
            }
        } else {
            printf("Unrecognized state.");
        }
        // If we performed a merge, the newly merged element should be at the
        // top of the stack.
        if (performed_a_merge) {
            mergesort_counter++;
            Node *mergedNode = peek(mergesort_stack);
            int *cur_array = mergedNode->array;
            int start_index = mergedNode->start_index;
            int end_index = mergedNode->end_index;
            // Put the merged array into the appropriate place in xs_mergesort.
            for (int i = start_index; i <= end_index; i++) {
                xs_mergesort[i] = cur_array[i - start_index];
            }
            glutSetWindow(upper_win);
            glutPostRedisplay();
        }
    }
    /* We want to run and display quicksort if either mergesort is done or if
     * we just performed a merge. We could also run quicksort regardless of
     * what the mergesort algorithm did, but this way we can compare how the
     * display iterations of the algorithms look since they're running at
     * the same time.
     */
    if (performed_a_merge || stk_is_empty(mergesort_stack)) {
    // if (true) {
        if (!stk_is_empty(quicksort_stack)) {
            Sublist *current_sublist = pop(quicksort_stack);
            int start_index = current_sublist->start_index;
            int end_index = current_sublist->end_index;
            int pivot_index = partition(xs_quicksort, start_index, end_index,
                compare);
            // Create two sublists and push them on the stack.
            int left_start_index = start_index;
            int left_end_index = pivot_index - 1;
            if (left_end_index - left_start_index >= 1) {
                Sublist *left_list = malloc(sizeof(Sublist));
                left_list->start_index = left_start_index;
                left_list->end_index = left_end_index;
                push(quicksort_stack, left_list);
            }
            int right_start_index = pivot_index + 1;
            int right_end_index = end_index;
            if (right_end_index - right_start_index >= 1) {
                Sublist *right_list = malloc(sizeof(Sublist));
                right_list->start_index = right_start_index;
                right_list->end_index = right_end_index;
                push(quicksort_stack, right_list);
            }
            free(current_sublist);
            quicksort_counter++;
        }
        // Partition happens at every step, so we'll always display.
        glutSetWindow(lower_win);
        glutPostRedisplay();
    }
}
コード例 #5
0
ファイル: stack.c プロジェクト: markmontymark/knr-c
void stk_dump(stk_t * s,FILE * out)
{
	fprintf(out,"stk_dump:\n" );
	while( ! stk_is_empty(s) )
		fprintf(out,"%d\n", stk_pop(s) );
}
コード例 #6
0
ファイル: 4-4.c プロジェクト: markmontymark/knr-c
void impl( )
{

	stk_t * k = stk_new();	

	char cur;
	int lhs;
	int rhs;
	int val;

	int total = 0;

	while( (cur = fgetc(stdin)) != EOF )
	{
		if(isdigit(cur))
		{
			int digits[MAXDIGITS];
			int n_digits = 0;
			digits[ n_digits++ ] = cur - '0';
			while( (cur = fgetc(stdin)) != EOF && isdigit(cur) && n_digits < MAXDIGITS)
				digits[ n_digits++ ] = cur - '0';
			int val =  0;
			for(int i =  0, j = n_digits - 1; i < n_digits; i ++, j-- )
				val += digits[i] * pow(10, j);
			stk_push(k, val);
		}

		switch(cur)
		{
			// handle operators
			case '%': 
			case '+': 
			case '-': 
			case '*': 
			case '/':
				//printf("got op, %c\n",cur);
				if(! stk_is_empty(k)	)
					if( (lhs = stk_pop(k)) == -1 )
					{
						printf("Bad lhs pop\n");
						return;
					}
				if(! stk_is_empty(k)	)
				{
					if( (rhs = stk_pop(k)) == -1 )
					{
						printf("Bad rhs pop\n");
						return;
					}
					fprintf(stdout, "lhs %d, rhs %d, op %c\n",lhs,rhs,cur);
					stk_push(k, calc_op(cur, lhs, rhs));
				}
				else 
				{
					stk_push(k,lhs);
					stk_push(k,cur);
				}
				break;

			// clear stack with _ underscore
			case '_': 
				calc_clear(k); 
				break;
			case '.': 
				calc_print_top(k,stdout); 
				break;

			case '>': 
				calc_swap_top(k); 
				break;
		}
	
	}
	stk_dump(k,stdout);
	stk_free(k);
}
コード例 #7
0
/*
 * Check if the given string is balanced
 */
int checkBalanced(StackPtr stack, char* n, int debug) {

    //go through the string
    int x = 0; //keep track of where the unbalanced symbol is
    int unbalanced = 0;
    int miss = 0;
    int i;
    for(i = 0; i < strlen(n) - 1; i++) {

        //If we encounter an opening symbol
        if(n[i] == '(' || n[i] == '{' || n[i] == '[' || n[i] == '<') {

            if(debug) {
                printf("Debug mode: Pushing to stack: %c\n", n[i]);
            }
            stk_push(stack, n[i], debug);
            x++;

        }

       //if we encounter a closing symbol
        if(n[i] == ')' || n[i] == '}' || n[i] == ']' || n[i] == '>'){

            //if closing is encountered next, if correct closing bracket pop from stack
            if(n[i] == ')') {

                if(stk_top(stack) == '(') {
                    if(debug) {
                        printf("Debug mode: Popping from stack: %c\n", stk_top(stack));
                    }
                    stk_pop(stack);
                } else {
                    miss = 1;
                    unbalanced = 1;
                }

            } else if(n[i] == '}') {

                if(stk_top(stack) == '{') {
                    if(debug) {
                        printf("Debug mode: Popping from stack: %c\n", stk_top(stack));
                    }
                    stk_pop(stack);
                } else {
                    miss = 2;
                    unbalanced = 1;
                }

            } else if(n[i] == ']') {

                if(stk_top(stack) == '[') {
                    if(debug) {
                        printf("Debug mode: Popping from stack: %c\n", stk_top(stack));
                    }
                    stk_pop(stack);
                } else {
                    miss = 3;
                    unbalanced = 1;
                }

            } else if(n[i] == '>') {

                if(stk_top(stack) == '<') {
                    if(debug) {
                        printf("Debug mode: Popping from stack: %c\n", stk_top(stack));
                    }
                    stk_pop(stack);
                } else {
                    miss = 4;
                    unbalanced = 1;
                }

            }

        }

    }

    if(stk_is_empty(stack) && !unbalanced) {

        printf("\n%s", n);
        printf("Expression is Balanced!\n\n\n");
        return 1;

    } else {

        printf("\n%s\n", n);

        int i;
        for(i = 0; i < x; i++) {
            printf(" ");
        }
        printf("^");

        if(miss == 1) {
            printf(" missing (");
        } else if(miss == 2) {
            printf(" missing {");
        } else if(miss == 3) {
            printf(" missing [");
        } else if(miss == 4) {
            printf(" missing <");
        }

        printf("\n\n\n");

        return 0;

    }

}