Exemple #1
0
int main(int argc, char **argv)
{
    tree *first_elem;
    char *str;
    int n;
    str = (char *) calloc(10, sizeof(char));
    first_elem = NULL;
    if (argc == 2) {
        if ((strcmp(argv[1], "-h")) == 0) {
            puts("Instructions for use:");
            puts("1.Launch file:");
            puts(" gcc-Wall-Wextra-o lab4.exe lab4.c");
            puts(" ./lab4.exe");
            puts("2.In the screen prompts you to enter the first element of a tree (root).");
            puts("    Enter it. Next, you are prompted to enter ocheredenoy the tree.");
            puts("    Enter it. Proposals will be displayed as long as you do not enter '0'.");
            puts("    Next, you are prompted to enter the level for which you want to calculate the number of nodes.");
            puts("    Enter it.");
            puts("3.The displayed the number of nodes at a given level or '0' if the current level");
            puts("    does not exist or at no nodes.");
            return 0;
        }
    }
    first_elem = create_tree(first_elem);
    add_elem(first_elem);
    puts("Enter level");
    enter_number(str,&n);
    printf("number of nod on level %d: %d", n,
           calculate_number_of_nod(first_elem, n));
    return 0;
}
Exemple #2
0
tree *create_tree(tree * first_elem)
{
    char *str;
    str = (char *) calloc(10, sizeof(char));
    if (first_elem) {
        puts("Tree already created");
        return (first_elem);
    }
    if (!(first_elem = (tree *) calloc(1, sizeof(tree)))) {
        puts("No free memory");
        return (NULL);
    }
    puts("Enter info in first element");
    enter_number(str,&first_elem -> info);
    first_elem->number_of_miting = 1;
    first_elem->next_left = NULL;
    first_elem->next_right = NULL;
    return first_elem;
}
Exemple #3
0
int main(int argc, char *argv[]) {
    int opt = 0;
    int longIndex = 0;

    /* Initialize globalArgs before we get to work. */
    globalArgs.version = NULL;    /* false */

	//prepare i18n
	setlocale(LC_ALL, "");
	bindtextdomain("4digits", LOCALE_PATH);
	textdomain("4digits");

    // Process the arguments with getopt_long(), then populate globalArgs
    opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    while(opt != -1) {
        switch(opt) {
            case 'v':
                globalArgs.version = VERSION_STRING;
                printf("%s\n%s\n%s", VERSION, _(COPYRIGHT), _(AUTHOR));
                exit(1);
            case 'h': 
                err_exit(_(HELP));
                break;
            case '?': /* fall-through is intentional */
                err_exit(_("Usage: 4digits [OPTION]...\n"
                        "Try `4digits --help' for more information."));
                break; 
            case 0:    /* long option without a short arg */
                if(strcmp("version", longOpts[longIndex].name) == 0)
                    break; 
                break;
            default:
                /* You won't actually get here. */
                break;
        }
        opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    }

    int ans_digits[4];
    gen_rand(ans_digits); /* array for the 4 digits of n*/
    time_t temp = time(NULL);
    time_t tic = temp;
    int guessed[8] = {0, 0, 0, 0, 0, 0, 0, 0};
    int i;
    bool dup = false;

    for (int num_guess = 0; num_guess < 8; num_guess++) {
        int A = 0, B = 0;
        int input = enter_number();

        for(int i=0; i < num_guess; i++)
            // duplicated input
            if (input == guessed[i]) {
                fprintf(stderr, _("You've already guessed it.\n"));
                --num_guess;
                dup = true;
                break;
            }

        if (dup == true) {
            dup = false;
            continue;
        }

        int in_digits[4]; /* arrays for the 4 digits of input*/
        for(i=0; i<4; i++) {
            in_digits[i]=(int) (input / tenpow(3-i) )%10;
        }

        compare(in_digits, ans_digits, &A, &B);
        printf("%dA%dB    ", A, B);
        if (num_guess != 7)
            printf(ngettext("\t %d time left.\n", "\t %d times left.\n", 7-num_guess), 7-num_guess);
        guessed[num_guess] = input;

        if(A == 4) {
            time_t toc = time(NULL);
            int score = (int)(toc-tic);
            printf(_("You win! :) Used %d sec.\n"), score);
            save_score(score); /* save score in the score file */
            return 0;
        }
    }
    printf(_("\nHaha, you lose. It is "));
    for(int i = 0; i < 4; i++)
        printf("%d", ans_digits[i]);
    printf(".\n");
    return 0;
}
Exemple #4
0
/*
 * Main driver for the game.
 */
int
main(int argc, char **argv) {
    // define usage
    const char *usage = "Usage: sudoku n00b|l33t [#]\n";

    // ensure that number of arguments is as expected
    if (argc != 2 && argc != 3) {
        fprintf(stderr, usage);
        return 1;
    }

    // ensure that level is valid
    if (!strcmp(argv[1], "debug"))
        g.level = "debug";
    else if (!strcmp(argv[1], "n00b"))
        g.level = "n00b";
    else if (!strcmp(argv[1], "l33t"))
        g.level = "l33t";
    else {
        fprintf(stderr, usage);
        return 2;
    }

    // n00b and l33t levels have 1024 boards; debug level has 9
    int max = (!strcmp(g.level, "debug")) ? 9 : 1024;

    // ensure that #, if provided, is in [1, max]
    if (argc == 3) {
        // ensure n is integral
        char c;
        if (!sscanf(argv[2], " %d %c", &g.number, &c) == 1) {
            fprintf(stderr, usage);
            return 3;
        }

        // ensure n is in [1, max]
        if (g.number < 1 || g.number > max) {
            fprintf(stderr, "That board # does not exist!\n");
            return 4;
        }

        // seed PRNG with # so that we get same sequence of boards
        srand(g.number);
    }
    else {
        // seed PRNG with current time so that we get any sequence of boards
        srand(time(NULL));

        // choose a random n in [1, max]
        g.number = rand() % max + 1;
    }

    // start up ncurses
    if (!startup()) {
        fprintf(stderr, "Error starting up ncurses!\n");
        return 6;
    }

    // register handler for SIGWINCH (SIGnal WINdow CHanged)
    signal(SIGWINCH, (void (*)(int)) handle_signal);

    // start the first game
    if (!restart_game()) {
        shutdown();
        fprintf(stderr, "Could not load board from disk!\n");
        return 7;
    }
    redraw_all();

    // let the user play!
    int ch;
    do {
        // refresh the screen
        refresh();

        // get user's input
        ch = getch();

        // capitalize input to simplify cases
        ch = toupper(ch);

        // process user's input
        switch (ch) {

            // left arrow key
            case KEY_LEFT:
            case 'H':
                if (--g.x < 0)
                    g.x += DIM;
                hide_banner();
                show_cursor();
                break;
            // right arrow key
            case KEY_RIGHT:
            case 'L': 
                if (++g.x >= DIM)
                    g.x -= DIM;
                hide_banner();
                show_cursor();
                break;
            // up arrow key
            case KEY_UP:
            case 'K':
                if (--g.y < 0)
                    g.y += DIM;
                hide_banner();
                show_cursor();
                break;
            // down arrow key
            case KEY_DOWN:
            case 'J':
                if (++g.y >= DIM)
                    g.y -= DIM;
                hide_banner();
                show_cursor();
                break;

            // enable user to enter numbers
            case '1': case '2': 
            case '3': case '4': 
            case '5': case '6': 
            case '7': case '8': 
            case '9':
                if (g.given[g.y][g.x] == false && validate(ch) == true) {
                    enter_number(ch);
                    draw_numbers();
                    show_cursor();
                    won();
                }
                break; 

            // delete a number
            case KEY_BACKSPACE:
            case KEY_DC:
            case '.':
            case '0':
                if (g.given[g.y][g.x] == false) {
                    g.board[g.y][g.x] = 0; 
                    draw_numbers();
                    hide_banner();
                    show_cursor();
                }
                break;
            // start a new game
            case 'N': 
                g.number = rand() % max + 1;
                if (!restart_game()) {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 7;
                }
                break;

            // restart current game
            case 'R': 
                if (!restart_game()) {
                    shutdown();
                    fprintf(stderr, "Could not load board from disk!\n");
                    return 7;
                }
                break;

            // let user manually redraw screen with ctrl-L
            case CTRL('l'):
                redraw_all();
                break;
        }   // end switch statement

        // log input (and board's state) if any was received this iteration
        if (ch != ERR)
            log_move(ch);

    } while (ch != 'Q');

    // shut down ncurses
    shutdown();

    // tidy up the screen (using ANSI escape sequences)
    printf("\033[2J");
    printf("\033[%d;%dH", 0, 0);

    // kthxbai
    printf("\nkthxbai!\n\n");
    return 0;
}