Hi! I Downloaded and modified this opensource.
It basically, read a txt file and tell how much characters it has and it also tell how much of each letter (from A to Z) it has.
But... I Cant understand most of the code, from the "///////////OUTPUT////////" line to the end...
Someone can help me? Maybe commenting the code would help a lot!
Thanks!!
Code:#include <iostream> #include <fstream> using namespace std; using std::ifstream; char String[1024]; double Length = 0; int letterCount[36] = {0}; void sortCharacter(char cLetter); int main() { char nome[1024]; cout << "Coloque o nome do arquivo a ser examinado (deve ser um .txt)." << endl; cout << "O arquivo tem que estar na mesma pasta do programa." << endl; cin >> nome; ifstream inFile(nome, ios::in); if (!inFile) { cout << "O Arquivo " << nome << " nao foi encontrado." << endl; cout << "Por favor execulte o programa novamente." << endl; cin.get(); cin.get(); return 0; } while (!inFile.eof()) { inFile >> String; for (int x = 0; x < strlen(String); x++) { sortCharacter(String[x]); Length++; } } ///////////////////OUTPUT////////////////////////// cout << "Caracteres: " << Length << endl; for (int w = 0; w < 36; w++) { if (w <= 9) { if (letterCount[w] != 0) { cout << w << " aparece " << letterCount[w] << " vezes. \t"; cout << ((double)letterCount[w] / Length) * 100 << "%" << endl; } } else { if (letterCount[w] != 0) { cout << char(w + 55) << " aparece " << letterCount[w] << " vezes. \t"; cout << ((double)letterCount[w] / Length) * 100 << "%" << endl; } } } cout << endl << "(Caracteres com acento, assim como os de pontuacao, nao sao listados, porem sao contados)" << endl << endl; cout << "Processo completo!" << endl; cin.get(); cin.get(); return(0); } void sortCharacter(char cLetter) { if (cLetter <= '9' && cLetter >= '0') { letterCount[cLetter - 48]++; } else if ((cLetter <= 'z' && cLetter >= 'a') || (cLetter <= 'Z' && cLetter >= 'A')) { if (cLetter <= 'Z' && cLetter >= 'A') { letterCount[cLetter - 55]++; } else { letterCount[cLetter - 87]++; } } }



LinkBack URL
About LinkBacks


