Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class spirala
- {
- private:
- char** tab = NULL;
- int wymiary = 0;
- void rozmiescLitery(string wyraz)
- {
- this->tab = new char*[this->wymiary];
- for(int i=0; i<this->wymiary; i++)
- {
- this->tab[i] = new char[this->wymiary];
- for(int j=0; j<this->wymiary; j++)
- {
- if(wymiary*i+j < wyraz.size())
- {
- this->tab[i][j] = wyraz[wymiary*i+j];
- }
- else
- {
- this->tab[i][j] = ' ';
- }
- }
- }
- }
- int zmienZwrot(int aktualny)
- {
- if(aktualny == 3)
- return 0;
- else
- return aktualny+1;
- }
- string pobierajLitery(int* x, int* y, int* skok, int* zwrot)
- {
- string pobrane = "";
- for(int i=1; i<*skok; i++)
- {
- if(*zwrot == 0)
- *y -= 1;
- else if(*zwrot == 1)
- *x -= 1;
- else if(*zwrot == 2)
- *y += 1;
- else if(*zwrot == 3)
- *x += 1;
- pobrane += this->tab[*x][*y];
- }
- return pobrane;
- }
- public:
- spirala(string wyraz)
- {
- for(int i=1; i<=wyraz.size(); i+=2)
- {
- if(i*i >= wyraz.size())
- {
- this->wymiary = i;
- rozmiescLitery(wyraz);
- break;
- }
- }
- }
- void wypisz()
- {
- for(int i=0; i<this->wymiary; i++)
- {
- for(int j=0; j<this->wymiary; j++)
- {
- cout << this->tab[i][j];
- }
- cout << endl;
- }
- }
- string generuj()
- {
- string result = "";
- int x = this->wymiary/2;
- int y = this->wymiary/2;
- int skok = 2;
- int zwrot = 0; //0-lewy, 1-gorny, 2-prawy, 3-dolny
- result += this->tab[x][y];
- for(skok; skok<=this->wymiary; skok++)
- {
- result += pobierajLitery(&x, &y, &skok, &zwrot);
- zwrot = zmienZwrot(zwrot);
- result += pobierajLitery(&x, &y, &skok, &zwrot);
- zwrot = zmienZwrot(zwrot);
- }
- return result;
- }
- ~spirala()
- {
- for(int i=0; i<wymiary; i++)
- {
- delete [] tab[i];
- }
- delete [] tab;
- tab = NULL;
- }
- };
- int main()
- {
- spirala* szyfr = new spirala("WYRAZDOZASZYFROWANIA");
- szyfr->wypisz();
- cout << endl << endl << "Zaszyfrowane: " << endl;
- cout << szyfr->generuj();
- delete szyfr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement