Stos na strukturze

0
 #include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct Stack
{
    int m_count;
    int max_size;
    int *tablica;
};

void Init(Stack *S,int max)
{
    S->m_count = 0;
    S->max_size = max;
    S->tablica = new int [S->max_size];
}

bool Empty(Stack *S)
{
    return S->m_count == 0;
}

bool Full(Stack *S)
{
    return S->m_count == S->max_size;
}

void Push(Stack *S, int number)
{
    if(S->m_count < S->max_size)
    {
        S->tablica[S->m_count++] = number;
    }
}

int Top(Stack *S)
{
    if(S->m_count)
    {
        return S->tablica[S->m_count - 1];
    }
}

void Pop(Stack *S)
{
    if(S->m_count)
    {
        --(S->m_count);
    }
}

void Destroy(Stack *S)
{
    delete [] S->tablica;
}


int main()
{
    typedef struct Stack Stack;
    Stack s1,s2;

    int liczba = rand() % 100 + 1;

    Stack *a = &s1;
    Stack *b = &s2;

    Init(a,liczba);
    Init(b,liczba);

    for(int i = 0; i < liczba; ++i)
    {
        Push(a,rand() % 100 + 1);
        Push(b,rand() % 100 + 1);
    }

    cout << "Stos s1:" << setw(24) << "Stos s2:" << endl;

    for(int i = 0; i < liczba; ++i)
    {
       cout << Top(a) << "\t\t\t";
       Pop(a);
       cout << Top(b) << endl;
       Pop(b);
    }

    Destroy(a);
    Destroy(b);

    return 0;
}

Czy w funkcji Top,Pop i Push dać warunek else ?

0

i co z nim chcesz zrobic?

Dac zawsze mozesz dac

0

W Top na pewno.
W Push - to zależy jak chcesz obsłużyć błąd przepełnienia (wyjątek czy kod błędu).

2

Abstrahując od logiki programu, to funkcja Top jest niepoprawna. Funkcja deklaruje, że zwraca int, więc musi w każdym przypadku zwracać int (chyba że rzuca wyjątek, ale tutaj wyjątków nie ma). W tym przypadku jeśli if nie jest spełniony to funkcja nic nie zwraca.

0

Chciałem dodać wyjątki, lecz nie wiem za bardzo jak to poprawnie napisać. Nigdy wcześniej nie pisałem wyjątków.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct Stack
{
    int m_count;
    int max_size;
    int *tablica;
};

void Init(Stack *S,int max)
{
    S->m_count = 0;
    S->max_size = max;
    S->tablica = new int [S->max_size];
}

bool Empty(Stack *S)
{
    return S->m_count == 0;
}

bool Full(Stack *S)
{
    return S->m_count == S->max_size;
}

void Push(Stack *S, int number)
{
    if(S->m_count < S->max_size)
    {
        S->tablica[S->m_count++] = number;
    }
    else
    {
        throw "Nie ma takiego elementu na stosie !";
    }
}

int Top(Stack *S)
{
    if(S->m_count)
    {
        return S->tablica[S->m_count - 1];
    }
    else
    {
        throw "Nie ma elementu na stosie !";
    }
}

void Pop(Stack *S)
{
    if(S->m_count)
    {
        --(S->m_count);
    }
    else
    {
        throw "Nie ma elementu na stosie !";
    }
}

void Destroy(Stack *S)
{
    delete [] S->tablica;
}


int main()
{
    typedef struct Stack Stack;
    Stack s1,s2;

    int liczba = rand() % 100 + 1;

    Stack *a = &s1;
    Stack *b = &s2;

    Init(a,liczba);
    Init(b,liczba);

    try
    {
        Push(Stack *S, int number);
        Top(Stack *S);
        Pop(Stack *S);
    }
    catch(string Stack wiadomosc)
    {
        cout << wiadomosc;
    }

    for(int i = 0; i < liczba; ++i)
    {
        Push(a,rand() % 100 + 1);
        Push(b,rand() % 100 + 1);
    }

    cout << "Stos s1:" << setw(24) << "Stos s2:" << endl;

    for(int i = 0; i < liczba; ++i)
    {
       cout << Top(a) << "\t\t\t";
       Pop(a);
       cout << Top(b) << endl;
       Pop(b);
    }
 cout <<   Full(a);
    Destroy(a);
    Destroy(b);

    return 0;
}
 
0

Wywal to

typedef struct Stack Stack;

Popraw to

catch(const string& wiadomosc)
Push(Stack *S, int number);

Co to za wywołanie funkcji? Prędzej:

Push(a, 1);
0

Pusha wywołuje w forze, to w forze dać try i catch ?

0

Zależy od kontekstu. Można też dać fora wewnątrz try.

0

Zmieniłem kod, ale nie wiem co w catchu mam robić i jaki argument funkcji dać:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct Stack
{
    int m_count;
    int max_size;
    int *tablica;
};

void Init(Stack *S,int max)
{
    S->m_count = 0;
    S->max_size = max;
    S->tablica = new int [S->max_size];
}

bool Empty(Stack *S)
{
    return S->m_count == 0;
}

bool Full(Stack *S)
{
    return S->m_count == S->max_size;
}

void Push(Stack *S, int number)
{
    if(S->m_count < S->max_size)
    {
        S->tablica[S->m_count++] = number;
    }
    else
    {
        throw "Nie ma takiego elementu na stosie !";
    }
}

int Top(Stack *S)
{
    if(S->m_count)
    {
        return S->tablica[S->m_count - 1];
    }
    else
    {
        throw "Nie ma elementu na stosie !";
    }
}

void Pop(Stack *S)
{
    if(S->m_count)
    {
        --(S->m_count);
    }
    else
    {
        throw "Nie ma elementu na stosie !";
    }
}

void Destroy(Stack *S)
{
    delete [] S->tablica;
}


int main()
{
    typedef struct Stack Stack;
    Stack s1,s2;

    int liczba = rand() % 100 + 1;

    Stack *a = &s1;
    Stack *b = &s2;

    Init(a,liczba);
    Init(b,liczba);

    for(int i = 0; i < liczba; ++i)
    {
        try
        {
            Push(a,rand() % 100 + 1);
            Push(b,rand() % 100 + 1);
        }
        catch(string Stack wiadomosc)
        {
            cout << wiadomosc;
        }

    }

    cout << "Stos s1:" << setw(24) << "Stos s2:" << endl;

    for(int i = 0; i < liczba; ++i)
    {
        try
        {
            cout << Top(a) << "\t\t\t";
            Pop(a);
            cout << Top(b) << endl;
            Pop(b);
        }
        catch(string Stack wiadomosc)
        {
            cout << wiadomosc;
        }
    }
    Destroy(a);
    Destroy(b);

    return 0;
}
 
0

Zmieniłem, ale przy złapaniu wyjątku nie wyświetla mi tego co dałem w throw.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct Stack
{
    int m_count;
    int max_size;
    int *tablica;
};

void Init(Stack *S,int max)
{
    S->m_count = 0;
    S->max_size = max;
    S->tablica = new int [S->max_size];
}

bool Empty(Stack *S)
{
    return S->m_count == 0;
}

bool Full(Stack *S)
{
    return S->m_count == S->max_size;
}

void Push(Stack *S, int number)
{
    if(S->m_count < S->max_size)
    {
        S->tablica[S->m_count++] = number;
    }
    else
    {
        throw  "Nie ma takiego elementu na stosie !";
    }
}

int Top(Stack *S)
{
    if(S->m_count)
    {
        return S->tablica[S->m_count - 1];
    }
    else
    {
        throw "Nie ma elementu na stosie !";
    }
}

void Pop(Stack *S)
{
    if(S->m_count)
    {
        --(S->m_count);
    }
    else
    {
        throw "Nie ma elementu na stosie !";
    }
}

void Destroy(Stack *S)
{
    delete [] S->tablica;
}


int main()
{
    Stack s1,s2;

    int liczba = rand() % 100 + 1;

    Stack *a = &s1;
    Stack *b = &s2;

    Init(a,3);
    Init(b,liczba);

    for(int i = 0; i < liczba; ++i)
    {
        try
        {
            Push(a,rand() % 100 + 1);
            Push(b,rand() % 100 + 1);
        }
        catch(const string  &wiadomosc)
        {
            cout << wiadomosc;
        }

    }

    cout << "Stos s1:" << setw(24) << "Stos s2:" << endl;

    for(int i = 0; i < liczba; ++i)
    {
        try
        {
            cout << Top(a) << "\t\t\t";
            Pop(a);
            cout << Top(b) << endl;
            Pop(b);
        }
        catch(const string  &wiadomosc)
        {
            cout << wiadomosc;
        }
    }
    Destroy(a);
    Destroy(b);

    return 0;
}
 
1
throw string("Nie ma elementu na stosie !");

1 użytkowników online, w tym zalogowanych: 0, gości: 1