Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. int change(int a, int shift);
  6. int code(int a, int shift,int from, int to);
  7. int findchar(string word);
  8. string pliktostring();
  9.  
  10. int main()
  11. {
  12. cout << "o ile przesunac(k aby zakonczyc)"<< endl;
  13. int shift;
  14. while (cin >> shift)
  15. {
  16.  
  17. string wyraz;
  18. wyraz=pliktostring();
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. cout <<"najczestsza litera="<<(char)findchar(wyraz);
  26.  
  27.  
  28.  
  29.  
  30. cout << "zatem przesuniecie pierwotne wynosilo= " ;
  31. int shiftpierwotny=-1*(findchar(wyraz)-(int)'E');
  32. cout << (findchar(wyraz)-(int)'E')<<endl;
  33. for (int i=0; i<(int)wyraz.length(); i++)
  34. {
  35. wyraz[i]=change((int)wyraz[i], shift);
  36. }
  37. cout <<"po zakodowanieu "<< wyraz << endl;
  38. cout << "przesuniecie= "<<shiftpierwotny ;
  39. cout << " o ile przesunac(k aby zakonczyc)"<< endl;
  40. }
  41. cout << "koniec!\n";
  42. return 0;
  43. }
  44. int change(int a, int shift)
  45. {
  46. if (a>=48 && a<=57) // numery
  47. return code(a, shift,48,57);
  48. else if(a>=65 && a<=90) // wielkie litery
  49. return code(a,shift, 65,90);
  50. else if (a>=97 && a<=122) //ma³e litery
  51. return code(a, shift,97,122);
  52. else
  53. return a;
  54. }
  55. int code(int a, int shift, int from, int to)
  56. {
  57. if (shift>0)
  58. {
  59. for(int i=0; i< shift; i++)
  60. {
  61. if ((int)a==to)
  62. a=from;
  63. else
  64. a++;
  65. }
  66.  
  67. }
  68. else if (shift<0)
  69. {
  70. for(int i=0; i> shift; i--)
  71. {
  72. if ((int)a==from)
  73. a=to;
  74. else
  75. a--;
  76. }
  77. }
  78. return a;
  79. }
  80.  
  81. string pliktostring()
  82. {
  83. string wyraz;
  84. fstream plik;
  85. plik.open( "dane.txt", ios::in );
  86. if( plik.good() )
  87. {
  88.  
  89. cout << "Zawartosc pliku:" << endl;
  90. while( !plik.eof() )
  91. {
  92. string nextline;
  93. getline( plik, nextline );
  94. wyraz=wyraz+char(10)+ nextline;
  95.  
  96.  
  97. }
  98. plik.close();
  99. } else cout << "Error! Nie udalo otworzyc sie pliku!" << endl;
  100.  
  101.  
  102. return wyraz;
  103. }
  104. int findchar(string word)
  105. {
  106. int counter[128]; //każdy indeks w tablicy chowa ilość znaków w tekście o kodzie ascii = index +1
  107. int i;
  108. for(i=0;i<128;i++)
  109. counter[i]=0;
  110.  
  111. for(i=0;i<(int)word.length();i++)
  112. {
  113. counter[(int)word[i]]++;
  114. }
  115.  
  116. counter[32]=0;
  117. int full=0;
  118. for(i = 0; i < 127; i++ )
  119. {
  120. if(counter[ i ] > counter[full] )
  121. full = i;
  122. }
  123. int inajczestsza=full;
  124. //char cnajczestsza= (char)inajczestsza;
  125. // cout<<cnajczestsza<<endl;
  126. return inajczestsza;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement