Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- float log2(float n) {
- return log(n)/log(2.0);
- }
- int frequenze[256];
- int main() {
- string s;
- float entropia = 0.0;
- int dim;
- cout << "Inserisci una stringa: ";
- cin >> s;
- dim = s.size();
- cout << endl;
- for(int i=0; i<dim; i++)
- frequenze[s[i]]++; // Esegue una conversione dal carattere (es 'a') al valore numerico ASCII (es 65)
- for(int i=0; i<dim; i++) {
- if(frequenze[i] > 0.0)
- entropia = entropia + (float)(frequenze[i] / dim * log2((float) dim/frequenze[i]));
- }
- cout << endl << "> Entropia della stringa: " << entropia;
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement