Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. float log2(float n) {
  8. return log(n)/log(2.0);
  9. }
  10.  
  11. int frequenze[256];
  12.  
  13. int main() {
  14.  
  15. string s;
  16. float entropia = 0.0;
  17. int dim;
  18.  
  19. cout << "Inserisci una stringa: ";
  20. cin >> s;
  21. dim = s.size();
  22. cout << endl;
  23.  
  24. for(int i=0; i<dim; i++)
  25. frequenze[s[i]]++; // Esegue una conversione dal carattere (es 'a') al valore numerico ASCII (es 65)
  26.  
  27. for(int i=0; i<dim; i++) {
  28. if(frequenze[i] > 0.0)
  29. entropia = entropia + (float)(frequenze[i] / dim * log2((float) dim/frequenze[i]));
  30. }
  31.  
  32. cout << endl << "> Entropia della stringa: " << entropia;
  33.  
  34. cout << endl;
  35. system("pause");
  36.  
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement