RiccardoMontagnin

Esercitazione 4 - Esercizio A

Apr 24th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1.  
  2. /*
  3.  * C++ Program to Count lines in a file
  4.  */
  5.  
  6. #include<iostream>
  7. #include<fstream>
  8. using namespace std;
  9.  
  10. /**
  11.  * Legge il file avente nome filename carattere per carattere.
  12.  * Al ritorno
  13.  * - chars contiene il numero di caratteri letto
  14.  * - words contiene il numero delle parole lette (separate da spaci o ritoni a capo)
  15.  * - lines contiene il numero delle righe (non vuote) lette
  16.  *
  17.  * Ritorna 0 se c'e' stato qualche errore nell'apertura del file, 1 altrimenti
  18.  */
  19. int we(const char *filename, int *chars, int *words, int *lines) {
  20.  
  21.     // Creiamo l'input stream
  22.     ifstream inFile;
  23.  
  24.     // Cerca di aprire il file
  25.     inFile.open(filename);
  26.  
  27.     // Se c'e' qualche errore, ritorna 0
  28.     if (!inFile) {
  29.         return 0;
  30.     }
  31.  
  32.     // Inizializzazione dei valori, per essere sicuri di partire da 0
  33.     // Essendo chars, words e lines passati per riferimento, non possiamo
  34.     // sapere che valori iniziali avranno
  35.     int charCount = 0;
  36.     int wordsCount = 0;
  37.     int lineCount = 0;
  38.  
  39.     string line;
  40.  
  41.     // Scorri ogni riga
  42.     while (getline(inFile, line)) {
  43.  
  44.         int lineCharCount = 0;
  45.  
  46.         bool previousIsChar = false;
  47.         for (int i = 0; i < line.length(); i++) {
  48.  
  49.             // Sto leggendo un carattere in ogni caso
  50.             lineCharCount += 1;
  51.  
  52.             // Controllo se ho trovato uno spazio o un terminatore di riga
  53.             if (line[i] == ' ' || line[i] == '\n' || line[i] == '\r') {
  54.  
  55.                 // Se il carattere precedente allo spazio o al terminatore era diverso
  56.                 // da un terminatore, allora vuol dire che ho appena finito una parola
  57.                 if (previousIsChar) {
  58.                     wordsCount += 1;
  59.                     previousIsChar = false;
  60.                 }
  61.             } else {
  62.  
  63.                 if (!previousIsChar && i == line.length() - 1) {
  64.                     wordsCount += 1;
  65.                 }
  66.  
  67.                 previousIsChar = true;
  68.             }
  69.         }
  70.    
  71.         if (lineCharCount > 0) {
  72.             lineCount += 1;
  73.             charCount += lineCharCount;
  74.         }
  75.     }
  76.  
  77.     // Chiudiamo l'input
  78.     inFile.close();
  79.  
  80.     // Copiamo i valori
  81.     *chars = charCount;
  82.     *words = wordsCount;
  83.     *lines = lineCount;
  84.  
  85.     return 1;
  86. }
  87.  
  88. int main(int argc, char *argv[]){
  89.  
  90.    
  91.     int charCount = 0;
  92.     int wordsCount = 0;
  93.     int lineCount = 0;
  94.  
  95.     we(argv[1], &charCount, &wordsCount, &lineCount);
  96.    
  97.  
  98.     cout << "Number of lines in the file: " << lineCount << endl;
  99.     cout << "Number of words in fhe file: " << wordsCount << endl;
  100.     cout << "Number of chars in fhe file: " << charCount << endl;
  101.     return 0;
  102. }
Add Comment
Please, Sign In to add comment