Exemplo n.º 1
0
//recursive algorithm to solve the towers of hanoi problem. Used in demo mode
void towers(int n, struct col *stackA, struct col *stackC, struct col *stackB){
	
	int val;
	char from, to;
	/* If only 1 disk, make the move and return */
	if(n==1){
		val = pop(stackA);        //pop from Stack A and
		push(val, stackC);        //push onto Stack C
		system("cls");
		if(stackA->size==100) from='A';
		else if(stackA->size==101) from='B';
		else from='C';
		if(stackC->size==100) to='A';
		else if(stackC->size==101) to='B';
		else to='C';
		printStacks();
		printf("\nMove disk %d from %c to %c",n, from, to);
		wait(playback);  //waits for the amount of time specified by the user. this is what determines the playback speed.
		return;
	}
	/* Move top n-1 disks from A to B, using C as auxiliary */
	towers(n-1,stackA,stackB,stackC);
	/* Move remaining disks from A to C */
	val = pop(stackA);
	push(val, stackC);
	system("cls");
	if(stackA->size==100) from='A';
	else if(stackA->size==101) from='B';
	else from='C';
	if(stackC->size==100) to='A';
	else if(stackC->size==101) to='B';
	else to='C';
	printStacks();
	printf("\nMove disk %d from %c to %c",n, from, to);
	wait(playback);
	/* Move n-1 disks from B to C using A as auxiliary */
	towers(n-1,stackB,stackC,stackA);
}
Exemplo n.º 2
0
int main(void) {
    const int MAX_STACK_SIZE = 10;
    stack_t * stack1 = stack_new();
    const char * dllName = userChoice();
    dynamic_t * dll = dynamic_init(dllName);
    if (NULL == dll) {
        printf("Can't load dynamic!\n");
        return 1;
    }
    if (NULL == dll->check) {
        printf("Can't get compare function!\n");
        return 1;
    }
    if (NULL == dll->react) {
        printf("Can't get reaction function!\n");
        return 1;
    }
    printf("Dynamic loaded!");
    Sleep(500);
    srand(time(NULL));
    while (stack_getCount(stack1) < MAX_STACK_SIZE) {
        stack_push(stack1, rand() % 100 - 50);
        printStacks(stack1);
        }
        if(dll->check(stack1)==1){
            dll->react(stack1);
        }
        else{
            return 0;
        }

        printStacks(stack1);
        Sleep(500);
        stack_free(stack1);
        return 0;
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    char word[20]; //primul cuvant de pe fiecare linie din fisierul de intrare
    FILE *f = fopen(argv[1], "r");

    int N;
    fscanf(f, "%d\n", &N);

    struct window *windows = new struct window[N]; //vector de ghisee
    Queue<person> *q = new Queue<person>[N]; //vector de cozii
    Stack<int> *s = new Stack<int>[N]; //vector de stive
    struct array arrayWindows; //retine cate ghisee au fost deschise si ID-urile
    arrayWindows.nrWindows = 0;
    arrayWindows.A = new int[N];

    //citirea si interpretarea fisierului de input
    while(fscanf(f, "%s", word) == 1)
    {
        if(strcmp(word, "OPEN_WINDOW") == 0)
        {
            int window_id, weight_min, weight_max, q_, k;
            fscanf(f, "%d%d%d%d%d", &window_id, &weight_min, &weight_max, &q_, &k);
            open_window(windows, &arrayWindows, q, s, window_id, weight_min,
                        weight_max, q_, k);
        }

        else if(strcmp(word, "ADD_PERSON") == 0)
        {
            int personal_id, package_weight, window_id;
            fscanf(f, "%d%d%d", &personal_id, &package_weight, &window_id);
            add_person(q, personal_id, package_weight, window_id);
        }

        else if(strcmp(word, "PROCESS") == 0)
        {
            int window_id, n_people;
            fscanf(f, "%d%d", &window_id, &n_people);
            process(windows, arrayWindows, q, s, window_id, n_people);
        }

        else if(strcmp(word, "PROCESS_ALL") == 0)
        {
            int window_id;
            fscanf(f, "%d", &window_id);
            processAll(windows, arrayWindows, q, s, window_id);
        }

        else if(strcmp(word, "PRINT_QUEUES") == 0)
            printQueues(q, arrayWindows);

        else if(strcmp(word, "PRINT_STACKS") == 0)
            printStacks(s, arrayWindows);

        else if(strcmp(word, "FLUSH_STACKS") == 0)
            flushStacks(s, arrayWindows);
    }

    fclose(f);

    return 0;
}
Exemplo n.º 4
0
//play mode
void play(){
	system("cls");
	printf("How many discs do you want to solve?\n>>");
	int n, val, moves=0;
	char choice;
	char name[15];
	scanf("%d", &n);  //get number of discs to solve from user
	fillA(n);         //fill stack A with the number, n
	time_t start;     //declare time variables used to count/time how long it takes to play the game till completion
	time_t end;
	float duration;
	time(&start);    //get the time at which he starts playing the game
	while(countNodes() != n){   //loops play for as long as stack C is not yet full. Game ends when Stack C is full
        int p = 1;
        while(p==1){
			system("cls");
			printStacks();     //print the stacks on the screen
			printf("\nPress A to pop from Stack A\nPress B to pop from Stack B\nPress C to pop from Stack C\n>>");
			choice = getch();
		
			switch(choice){
				case 'A':      //if he enters 'a' or 'A', pop disc from stack A
				case 'a':
					if(headA->next==tailA)break; //if stack A is empty, don't pop
					printf("popping from A");
                    val = pop(headA);            //pop node from stack A
					p--;
					break;
				case 'B':     //if he enters 'b' or 'B', pop disc from stack B
				case 'b':
					if(headB->next==tailB)break;   //if stack B is empty, don't pop
            	    printf("popping from B");
					val = pop(headB);              //pop node from stack B
					p--;
					break;
				case 'C':     //if he enters 'c' or 'C', pop disc from stack C
				case 'c':
					if(headC->next==tailC)break;  //if stack C is empty, don't pop
            	    printf("popping from C");
					val = pop(headC);             //pop node from stack C
					p--;
					break;
				default:
					printf("\b"); //if any other input is entered, delete it
					break;
			}
		}
		p=1;
		while(p==1){
			system("cls");    //clear the screen
  			printStacks();    //print the current state of the stacks
			printf("\nPress A to push to Stack A\nPress B to push to Stack B\nPress C to push to Stack C\n>>");
			choice = getch();
			switch(choice){
				case 'A':     //if he enter 'A' or 'a', push disc onto Stack A
				case 'a':
					if(val>headA->next->size)break; //prevent the user from pushing a bigger disc onto the one on stack A
					push(val, headA);               //push disc onto stack A
					p--;
					break;
				case 'B':     //if he enters 'B' or  'b', push disc onto stack B
				case 'b':
                    if(val>headB->next->size)break; //prevent the user from pushing a bigger disc onto the one on stack B
					push(val, headB);               //push disc onto stack B
					p--;
					break;
				case 'C':      //if he enter 'C' or 'c', push disc onto stack C
				case 'c':
                    if(val>headC->next->size)break; //prevent the user from pushing a bigger disc onto the one on stack C
					push(val, headC);               //push disc onto stack C
					p--;
					break;
				default:
					printf("\b"); //if any invalid input is entered, delete it
					break;
			}
		}
		moves++;
	}
	printStacks(); //print the current state of the stacks
	time(&end);    //get the time at which he completes the game
	duration = difftime(end, start);  //calculate the duration of gameplay
	printf("\nCongratulations!!\nCompleted in %d moves! (%.0f seconds)\n", moves, duration);
	printf("\nEnter you name: ");  //prompt for player's name
	scanf("%s", name);             //save the name
	FILE *fptr=NULL;               //create a file pointer
	fptr = fopen("HallofFame.txt", "a" );  //open HallofFame.txt to add details of the game
	fprintf(fptr, "%s,%d,%d,%.0f\n", name, n, moves, duration); //save player's info in the HallofFame.txt file
	fclose(fptr);                 //close the file pointer
	system("pause");
}
Exemplo n.º 5
0
void main(void) {
    char expr[1000] = " ";
    char *postfix;
    char *err;
    int ln = -2, cnt = 2;
    int width, height;
    char *title = "Expression Evaluator";
    WINDOW *title_win, *work_win;
    char *e;
    char *num = NULL;
    char curr[2] = {'\0', '\0'};
    char *prev;
    int r;

    // stacks for operands and operators.
    strstack *operands = createStrstack();
    strstack *operators = createStrstack();
    
    initscr();
    refresh();
    getmaxyx(stdscr, height, width);

    // create the title and working windows.
    title_win = create_newwin(3, width, 0, 0, true);
    work_win = create_newwin(height-2, width, 3, 0, false);
    
    // print title.
    mvwprintw(title_win, 1, (width - (int)strlen(title))/2, "%s", title);
    wrefresh(title_win);

    // REPL loop
    scrollok(work_win, TRUE);
    while(strcmp(expr, "exit") != 0) {
        if (ln > height/2) {
            wscrl(work_win, 2);
            refresh();
        } else {
            ln += cnt;
            cnt = 0;
        }
        mvwprintw(work_win, ln+cnt++, 1, ">> ", ln);
        wrefresh(work_win);
        wgetstr(work_win, expr);

        // check to make sure the expression is not empty.
        if (strlen(expr) == 0) {
            cnt--;
            continue;
        }

        // clean INFIX expression
        err = cleanInfixExpression(expr);
        if (strlen(err) != 0) {
            mvwprintw(work_win, ln+cnt++, 4, "Could not clean expression. [%s]", err);
            continue;
        } else {
            mvwprintw(work_win, ln+cnt, 4, "INFIX: ");
            // Print expression and go through character by character.
            wattron(work_win, A_BOLD);
            wattron(work_win, A_STANDOUT);
            mvwprintw(work_win, ln+cnt++, 11, "%s", expr);
            wattroff(work_win, A_STANDOUT);
            wattroff(work_win, A_BOLD);
        }

        // check INFIX expression
        err = checkInfixExpression(expr);
        if (strlen(err) != 0) { 
            mvwprintw(work_win, ln+cnt++, 4, "%s", err);
            continue;
        }

        emptyStrstack(operators);
        free(operators);
        operators = createStrstack();
        emptyStrstack(operands);
        free(operands);
        operands = createStrstack();

        e = malloc(sizeof(char) * (strlen(expr) + 1));
        strcpy(e, expr);
        num = e;
        for(r = 0; r < strlen(expr); r++) {
            printStacks(work_win, operands, operators, ln+cnt+3, 4);
            
            // check if the current character is an operator.
            wattron(work_win, A_DIM);
            if (isOperator(expr[r])) {
                mvwprintw(work_win, ln+cnt+1, 4, "This is an operator.");

                // save current operator.
                curr[0] = e[r];
                e[r] = 0;

                // push the current operand.
                if (strlen(num) != 0) {
                    mvwprintw(work_win, ln+cnt+2, 4, "Push the number.");
                    pushStrstack(operands, num);  
                    printStacks(work_win, operands, operators, ln+cnt+3, 4);
                }

                // check for operator precedence.
                for (prev = peekStrstack(operators);
                     prev != NULL && prev[0] != '(' && operateNow(prev[0], curr[0]);
                     prev = peekStrstack(operators)) {
                    reducePushPostfix(operands, operators);
                    printStacks(work_win, operands, operators, ln+cnt+3, 4);
                }
                // push the current opeator 
                pushStrstack(operators, curr);
                printStacks(work_win, operands, operators, ln+cnt+3, 4);
                num = e + r + 1;
            } else if (e[r] == '(') {
                // (, then just push the opening bracket and move the number
                // pointer.
                curr[0] = e[r];
                pushStrstack(operators, curr);
                printStacks(work_win, operands, operators, ln+cnt+3, 4);
                num = e + r + 1;
            } else if (e[r] == ')') {
                e[r] = 0;
                // push the last number.
                if (strlen(num) != 0) {
                    pushStrstack(operands, num);
                    printStacks(work_win, operands, operators, ln+cnt+3, 4);
                }
                for (prev = peekStrstack(operators);
                     prev != NULL && prev[0] != '(';
                     prev = peekStrstack(operators)) {
                    reducePushPostfix(operands, operators);
                    printStacks(work_win, operands, operators, ln+cnt+3, 4);
                }
                // pop the '(' out.
                popStrstack(operators, &num);
                printStacks(work_win, operands, operators, ln+cnt+3, 4);
                num = e + r + 1;
            } else {
                mvwprintw(work_win, ln+cnt+1, 4, "                    ");
                mvwprintw(work_win, ln+cnt+2, 4, "                    ");
            }

            // Print the two stacks.
            wattroff(work_win, A_DIM);

            wattron(work_win, A_BOLD);
            mvwprintw(work_win, ln+cnt, 11+r, "^");
            wattroff(work_win, A_BOLD);
            wrefresh(work_win);
            usleep(1000000);
            mvwprintw(work_win, ln+cnt, 11+r, " ");
        }
        // push the last number.
        if (strlen(num) != 0) {
            pushStrstack(operands, num);
            printStacks(work_win, operands, operators, ln+cnt+3, 4);
        }
        while(!isEmptyStrstack(operators)) {
            reducePushPostfix(operands, operators);
            printStacks(work_win, operands, operators, ln+cnt+3, 4);
        }
        // get the POSTFIX expression.
        popStrstack(operands, &num);
        mvwprintw(work_win, ln+cnt++, 4, "POSTFIX: %s", num);
        // clear the stack display.
        printStacks(work_win, NULL, NULL, ln+cnt+3, 0);
        wrefresh(work_win);
    }
    endwin();
    return;
}