Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- int change(int a, int shift);
- int code(int a, int shift,int from, int to);
- int findchar(string word);
- string pliktostring();
- int main()
- {
- cout << "o ile przesunac(k aby zakonczyc)"<< endl;
- int shift;
- while (cin >> shift)
- {
- string wyraz;
- wyraz=pliktostring();
- cout <<"najczestsza litera="<<(char)findchar(wyraz);
- cout << "zatem przesuniecie pierwotne wynosilo= " ;
- int shiftpierwotny=-1*(findchar(wyraz)-(int)'E');
- cout << (findchar(wyraz)-(int)'E')<<endl;
- for (int i=0; i<(int)wyraz.length(); i++)
- {
- wyraz[i]=change((int)wyraz[i], shift);
- }
- cout <<"po zakodowanieu "<< wyraz << endl;
- cout << "przesuniecie= "<<shiftpierwotny ;
- cout << " o ile przesunac(k aby zakonczyc)"<< endl;
- }
- cout << "koniec!\n";
- return 0;
- }
- int change(int a, int shift)
- {
- if (a>=48 && a<=57) // numery
- return code(a, shift,48,57);
- else if(a>=65 && a<=90) // wielkie litery
- return code(a,shift, 65,90);
- else if (a>=97 && a<=122) //ma³e litery
- return code(a, shift,97,122);
- else
- return a;
- }
- int code(int a, int shift, int from, int to)
- {
- if (shift>0)
- {
- for(int i=0; i< shift; i++)
- {
- if ((int)a==to)
- a=from;
- else
- a++;
- }
- }
- else if (shift<0)
- {
- for(int i=0; i> shift; i--)
- {
- if ((int)a==from)
- a=to;
- else
- a--;
- }
- }
- return a;
- }
- string pliktostring()
- {
- string wyraz;
- fstream plik;
- plik.open( "dane.txt", ios::in );
- if( plik.good() )
- {
- cout << "Zawartosc pliku:" << endl;
- while( !plik.eof() )
- {
- string nextline;
- getline( plik, nextline );
- wyraz=wyraz+char(10)+ nextline;
- }
- plik.close();
- } else cout << "Error! Nie udalo otworzyc sie pliku!" << endl;
- return wyraz;
- }
- int findchar(string word)
- {
- int counter[128]; //każdy indeks w tablicy chowa ilość znaków w tekście o kodzie ascii = index +1
- int i;
- for(i=0;i<128;i++)
- counter[i]=0;
- for(i=0;i<(int)word.length();i++)
- {
- counter[(int)word[i]]++;
- }
- counter[32]=0;
- int full=0;
- for(i = 0; i < 127; i++ )
- {
- if(counter[ i ] > counter[full] )
- full = i;
- }
- int inajczestsza=full;
- //char cnajczestsza= (char)inajczestsza;
- // cout<<cnajczestsza<<endl;
- return inajczestsza;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement