Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class spirala
  5. {
  6.     private:
  7.         char** tab = NULL;
  8.         int wymiary = 0;
  9.        
  10.         void rozmiescLitery(string wyraz)
  11.         {
  12.             this->tab = new char*[this->wymiary];
  13.             for(int i=0; i<this->wymiary; i++)
  14.             {
  15.                 this->tab[i] = new char[this->wymiary];
  16.                 for(int j=0; j<this->wymiary; j++)
  17.                 {
  18.                     if(wymiary*i+j < wyraz.size())
  19.                     {
  20.                         this->tab[i][j] = wyraz[wymiary*i+j];
  21.                     }
  22.                     else
  23.                     {
  24.                         this->tab[i][j] = ' ';
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.        
  30.         int zmienZwrot(int aktualny)
  31.         {
  32.             if(aktualny == 3)
  33.                 return 0;
  34.             else
  35.                 return aktualny+1;
  36.         }
  37.        
  38.         string pobierajLitery(int* x, int* y, int* skok, int* zwrot)
  39.         {
  40.             string pobrane = "";
  41.             for(int i=1; i<*skok; i++)
  42.             {
  43.                 if(*zwrot == 0)
  44.                     *y -= 1;
  45.                 else if(*zwrot == 1)
  46.                     *x -= 1;
  47.                 else if(*zwrot == 2)
  48.                     *y += 1;
  49.                 else if(*zwrot == 3)
  50.                     *x += 1;
  51.                
  52.                 pobrane += this->tab[*x][*y];
  53.             }
  54.             return pobrane;
  55.         }
  56.    
  57.     public:
  58.         spirala(string wyraz)
  59.         {
  60.             for(int i=1; i<=wyraz.size(); i+=2)
  61.             {
  62.                 if(i*i >= wyraz.size())
  63.                 {
  64.                     this->wymiary = i;
  65.                     rozmiescLitery(wyraz);
  66.                     break;
  67.                 }
  68.             }
  69.         }
  70.        
  71.         void wypisz()
  72.         {
  73.            
  74.             for(int i=0; i<this->wymiary; i++)
  75.             {
  76.                 for(int j=0; j<this->wymiary; j++)
  77.                 {
  78.                     cout << this->tab[i][j];
  79.                 }
  80.                 cout << endl;
  81.             }
  82.         }
  83.        
  84.         string generuj()
  85.         {
  86.             string result = "";
  87.             int x = this->wymiary/2;
  88.             int y = this->wymiary/2;
  89.             int skok = 2;
  90.             int zwrot = 0; //0-lewy, 1-gorny, 2-prawy, 3-dolny
  91.             result += this->tab[x][y];
  92.            
  93.             for(skok; skok<=this->wymiary; skok++)
  94.             {
  95.                 result += pobierajLitery(&x, &y, &skok, &zwrot);
  96.                 zwrot = zmienZwrot(zwrot);
  97.                 result += pobierajLitery(&x, &y, &skok, &zwrot);
  98.                 zwrot = zmienZwrot(zwrot);
  99.             }
  100.             return result;
  101.         }
  102.        
  103.         ~spirala()
  104.         {
  105.             for(int i=0; i<wymiary; i++)
  106.             {
  107.                 delete [] tab[i];
  108.             }
  109.             delete [] tab;
  110.             tab = NULL;
  111.         }
  112. };
  113.  
  114. int main()
  115. {
  116.     spirala* szyfr = new spirala("WYRAZDOZASZYFROWANIA");
  117.     szyfr->wypisz();
  118.     cout << endl << endl << "Zaszyfrowane: " << endl;
  119.     cout << szyfr->generuj();
  120.     delete szyfr;
  121.    
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement