- #include <iostream>
- #include <time.h>
- #include <windows.h>
- #include <stdio.h>
- #include <cstdlib>
- #include <conio.h>
- using namespace std;
- int dane[6];
- int rozmiar;
- void wyswietl_stos()
- {
- system("CLS");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),14);
- cout<<endl;
- cout<<"----------------"<<endl;
- cout<<"ZAWARTOSC STOSU:"<<endl;
- cout<<"----------------"<<endl;
- for (int i=rozmiar; i>=1; i--)
- {
- cout<<dane[i]<<endl;
- }
- if (rozmiar==0)
- cout<<"pusty"<<endl;
- cout << "----------------"<<endl<<endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);
- }
- //------------------------------------------------------------------------
- void push()
- {
- if (rozmiar>=5)
- {
- cout << "Stos pelny!";
- Sleep(1000);
- }
- else
- {
- cout<<endl<< "PUSH (jaka liczbe polozyc na stosie): ";
- rozmiar=rozmiar+1;
- cin>>dane[rozmiar];
- }
- }
- //------------------------------------------------------------------------
- void pop()
- {
- if (rozmiar>=1)
- {
- cout<<endl<<"POP - nastapi usuniecie ze stosu liczby: "<<dane[rozmiar];
- Sleep(2000);
- rozmiar=rozmiar-1;
- }
- else
- {
- cout << "Stos pusty!";
- Sleep(1000);
- }
- }
- //------------------------------------------------------------------------
- void size()
- {
- cout<<endl<<"Liczba elementow na stosie: "<<rozmiar;
- Sleep(2000);
- }
- //------------------------------------------------------------------------
- void empty()
- {
- if (rozmiar==0)
- cout<<endl<<"EMPTY (stos pusty?) -> TRUE";
- else
- cout<<endl<<"EMPTY (stos pusty?) -> FALSE";
- Sleep(2000);
- }
- //------------------------------------------------------------------------
- void quicksort(int *tablica, int lewy, int prawy)
- {
- int piwot=tablica[(lewy+prawy)/2];
- cout<<"wartosc piwota "<<piwot<<endl;
- int i,j,x;
- i=lewy;// 0
- j=prawy;// 6
- do // 3 65 1 43 45 4
- {
- while (tablica[i]<piwot)
- i++; //0
- while (tablica[j]>piwot)
- j--; //0
- if (i<=j)
- {
- x=tablica[i];
- tablica[i]=tablica[j];
- tablica[j]=x;
- i++; // 1
- j--;// -1
- cout<<"to Jest i "<<i-1<<"to Jest J "<<j-1<<endl;
- cout<<"to jest wartość i "<<tablica[i]<<"to jest wartość j "<<tablica[j]<<endl;
- }
- }
- while(i<=j);
- if(j>lewy)
- quicksort(tablica,lewy, j);
- if(i<prawy)
- quicksort(tablica, i, prawy); //rekurencja funcki z intencj¹ zmiany pivota
- }
- void losowanko()
- {
- int ile;
- system("CLS");
- srand(time(NULL)); // "inicjowanie losowania"
- cout<<"Podaj ile liczb ma byc posortowanych: ";
- cin>>ile;
- int *tablica;
- tablica= new int[ile];
- for(int i=0; i<ile; i++)
- {
- tablica[i]=rand()%100+1;//losowanie liczby od 1 do 100 i wpisywanie jej do tablicy
- cout<<tablica[i]<<" ";
- }
- cout<<endl<<"";
- cout<<"Liczby po sortowaniu: "<<endl;
- quicksort(tablica, 0, ile-1); //wywo³anie funcki
- for(int i=0; i<ile; i++)
- {
- cout<<tablica[i]<<" ";
- }
- cout<<endl<<" ";
- delete [] tablica;
- cout<<"ENTER POWROT DO MENU"<<endl;
- getchar();
- getchar();
- }
- void menu_stosu()
- {
- int wybor;
- rozmiar=0;
- do
- {
- wyswietl_stos();
- cout << "MENU GLOWNE STOSU:"<<endl;
- cout << "------------------------------------------"<<endl;
- cout << "1. PUSH (dodaje element na szczyt stosu) "<<endl;
- cout << "2. POP (usuwa element ze szczytu stosu) "<<endl;
- cout << "3. SIZE (ile elementow na stosie) "<<endl;
- cout << "4. EMPTY (czy stos jest pusty?) "<<endl;
- cout << "5. Powrucenie do menu "<<endl;
- cout << "------------------------------------------"<<endl;
- cout << "Wybor: ";
- cin >> wybor;
- switch (wybor)
- {
- case 1:
- push();
- break;
- case 2:
- pop();
- break;
- case 3:
- size();
- break;
- case 4:
- empty();
- break;
- }
- }
- while (wybor != 5);
- }
- int main()
- {
- char wybur;
- do
- {
- system("CLS");
- cout << "MENU GLOWNE PROGRAMU:"<<endl;
- cout << "------------------------------------------"<<endl;
- cout << "1. Sortowanie liczb "<<endl;
- cout << "2. Stos "<<endl;
- cout << "3. Koniec programu "<<endl;
- cout << "------------------------------------------"<<endl;
- wybur=getch();
- switch (wybur)
- {
- case '1':
- {
- losowanko();
- }
- break;
- case '2':
- {
- menu_stosu();
- }
- break;
- }
- }
- while(wybur !='3');
- return 0;
- }