Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // statystyczny
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <fstream>
- using namespace std;
- char* szyfr_cezara(int klucz, char* oryginal)
- {
- char* alfabet_malych = "abcdefghijklmnopqrstuvwxyz";
- char* alfabet_duzych = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char* alfabet_cyfr = "0123456789";
- int n = strlen(oryginal);
- char* result = new char[n+1];
- strcpy(result, oryginal);
- for(int i = 0; i < n; i++)
- {
- char* alfabet;
- //cout << static_cast<int>(oryginal[i]) << endl;
- if(oryginal[i] >= 65 && oryginal[i] <= 90)
- {
- alfabet = alfabet_duzych;
- }
- else if(oryginal[i] >= 97 && oryginal[i] <= 122)
- {
- alfabet = alfabet_malych;
- }
- else if (oryginal[i] >= 48 && oryginal[i] <= 57)
- {
- alfabet = alfabet_cyfr;
- }
- else
- {
- //cout << "znaki musza byc alfanumeryczne";
- //exit(1);
- result[i] = oryginal[i];
- continue;
- }
- int length = strlen(alfabet);
- int indeks_szukanej;
- for(int j = 0; j < length; j++)
- {
- if(alfabet[j] == oryginal[i])
- {
- indeks_szukanej = j;
- }
- }
- int spot = (indeks_szukanej + klucz) % length;
- if(spot < 0)
- {
- spot += length;
- }
- result[i] = alfabet[spot];
- }
- return result;
- }
- int roznica_do_e(char litera)
- {
- if(litera == 'E') return 0;
- char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int key = 0;
- if(litera > 'E')
- {
- key = 'E' - litera;
- key += 26;
- }
- else
- {
- key = 'E' - litera;
- }
- return key;
- }
- int count_occurrences(string s, char literka)
- {
- int count = 0;
- for (int i = 0; i < s.size(); i++)
- if (s[i] == literka) count++;
- return count;
- }
- int main()
- {
- char* alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- char litera = 'E';
- int tab[26];
- for(int i = 0; i < 26; i++)
- {
- tab[i] = 0;
- }
- fstream plik;
- string line;
- plik.open( "szyfrogram.txt", ios::in );
- if( plik.good() == true )
- {
- while(getline(plik, line))
- {
- for(int i = 0; i < 26; i++)
- {
- char aktualna_literka = alfabet[i];
- int ilosc = count_occurrences(line,aktualna_literka);
- tab[i] += ilosc;
- }
- //cout << line;
- }
- }
- plik.close();
- // wypisz po zliczeniu
- for(int i = 0; i < 26; i++)
- {
- cout << alfabet[i] << " ";
- cout << tab[i] << endl;
- }
- // znajdz maximum
- int indeks_max,max = 0;
- for(int i = 0; i < 26; i++)
- {
- if(tab[i] > max)
- {
- max = tab[i];
- indeks_max = i;
- }
- }
- cout << "najwiecej jest literek: " << alfabet[indeks_max] << endl;
- char najczestsza = alfabet[indeks_max];
- int key = roznica_do_e(najczestsza);
- cout << "klucz to: " << key << endl;
- cout << endl;
- cout << endl;
- std::fstream plik2;
- //char oryginal[1000];
- string line2;
- plik2.open( "szyfrogram.txt", ios::in );
- if( plik2.good() == true )
- {
- int klucz = key;
- while(getline(plik2, line2))
- {
- char* line_cstr = const_cast<char*>(line2.c_str());
- char* szyfrogram = szyfr_cezara( klucz , line_cstr );
- cout << szyfrogram;
- }
- }
- plik2.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement