[ create a new paste ] login | about

Link: http://codepad.org/0vT3NSvn    [ raw code | output | fork ]

tek6 - C++, pasted on Oct 24:
#include <stdio.h>
#include <time.h>
#include "Stack.h"


// Check if the element exists in the stack
bool search(TStack pilha, int elemento) {
    while (emptystack(pilha)) {
        if (top(pilha) == elemento) {
            pop(&pilha);
            return true;
        }
    }

    return false;
}

int main(void) {
    TStackKey elem; // Will store elements to the stack
    TStack pilha1, pilha2; // Stacks
    register int i; // Counter
    int tam; // Size stack
    int op; // Option
    int flag;

    srand(time(NULL));
    createstack(&pilha1); // Create stack1
    createstack(&pilha2); // Create stack2

    // Read the stack size
    do {
        printf("Enter a number for stack: ");
        scanf("%d", &tam);
    } while(tam < 0 || tam > 100);

    // Read the numbers for stack
    for (i = 0; i < tam; i++) {
            elem = rand() % 10 + 1;
            push(elem, &pilha1);
    }
    
    // Writer the stack
    printstack(&pilha1, (char*)"Stack generated: ");

    // Remove numbers from the stack.
    do {
        printf ("Entre com uma nĂºmero para ser removido: ");
        scanf ("%d", &elem);

        flag = 0;

        if (search(pilha1, elem)) {
            while (emptystack(pilha1)) {
                if (elem == top(pilha1)) {
                    pop(&pilha1);
                    flag = 1;
                } else {
                    push(pop(&pilha1), &pilha2);
                }
            }
        }

        printstack(&pilha1, (char*)"Stack 1: "); // Test

        if (flag == 1) {
            while (emptystack(pilha2)) {
                push(top(pilha2), &pilha1);
                pop(&pilha2);
            }
        } else {
            printf ("\nThe number %d does not exist.\n", elem);
        }

        printstack(&pilha2, (char*)"\nNew stack 2: "); // Test

        printf ("\n");

        printstack(&pilha1, (char*)"New stack 1: ");

        do {
            printf ("1 - Continue\n2 - Exit\n");
            scanf ("%d", &op);
        } while (op != 1 && op != 2);

    } while (op != 2);

    return 0;
}


Output:
1
2
3
Line 18: error: Stack.h: No such file or directory
Line 7: error: 'TStack' was not declared in this scope
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: