Advertisement
tegusta

Statistiche di un file

May 16th, 2012
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.92 KB | None | 0 0
  1. /*
  2. Studente : Scia Massimiliano
  3. Classe : 3IC
  4. Data : 14/05/12 15:44
  5. Nome del file : statistiche file, conta:
  6.                                 -quanti numeri
  7.                                 -quante cifre
  8.                                 -quante frasi
  9.                                 -quante lettere
  10.                                 -quanti caratteri totali
  11.                                 -il numero di parole
  12.                                 -il numero di caratteri che ci sono di media per ogni parola
  13.                                 -il numero di parole che ci sono di media per ogni frase
  14.                                 -la frequenza di una parola
  15. */
  16.  
  17. #include <iostream>
  18. #include <cmath>
  19. #include <ctime>
  20. #include <cstdlib>
  21. #include <cctype>
  22. #include <windows.h>
  23. #include <fstream>
  24. #include <time.h>
  25. #include <stdio.h>
  26. #define N 200
  27.  
  28. using namespace std;
  29.  
  30.  
  31. void end(void){
  32.     fflush(stdin);
  33.     cout<<"\n\nPremere Invio per continuare.";
  34.     getchar();
  35. }//end
  36.  
  37. bool lettere(int &numlet){
  38.      FILE* f;
  39.      FILE* f1;
  40.      f=fopen("prova.txt","r");
  41.      f1=fopen("analisi.txt","w");
  42.      if(f==NULL || f1==NULL)
  43.          return 0;
  44.      char c;
  45.      char d[20];
  46.      numlet=0;
  47.      fputs("Il numero di lettere e`: ",f1);
  48.      fseek(f,0,SEEK_SET);
  49.      while(!feof(f)){
  50.          c=fgetc(f);
  51.          if(isalpha(c))
  52.              numlet++;
  53.      }//while
  54.      itoa(numlet,d,10);
  55.      fputs(d,f1);
  56.      fputc('\n',f1);
  57.      fputc('\n',f1);
  58.      fclose(f);
  59.      fclose(f1);
  60.      return 1;
  61. }//lettere
  62.  
  63. bool frasi(int &numfrasi){
  64.      FILE* f;
  65.      FILE* f1;
  66.      f=fopen("prova.txt","r");
  67.      f1=fopen("analisi.txt","a");
  68.      if(f==NULL || f1==NULL)
  69.          return 0;
  70.      char c;
  71.      char d[20];
  72.      numfrasi=0;
  73.      fputs("Il numero di frasi e`: ",f1);
  74.      int i=0;
  75.      while(!feof(f)){
  76.          c=fgetc(f);
  77.          if(c=='.')
  78.              numfrasi++;
  79.      }//while
  80.      itoa(numfrasi,d,10);
  81.      fputs(d,f1);
  82.      fputc('\n',f1);
  83.      fputc('\n',f1);
  84.      fclose(f);
  85.      fclose(f1);
  86.      return 1;
  87. }//frasi
  88.  
  89. bool cifre(){
  90.      FILE* f;
  91.      FILE* f1;
  92.      f=fopen("prova.txt","r");
  93.      f1=fopen("analisi.txt","a");
  94.      if(f==NULL || f1==NULL)
  95.          return 0;
  96.      char c;
  97.      char d[20];
  98.      int cont=0;
  99.      fputs("Il numero di cifre e`: ",f1);
  100.      while(!feof(f)){
  101.          c=fgetc(f);
  102.          if(isdigit(c))
  103.              cont++;
  104.      }//while
  105.      itoa(cont,d,10);
  106.      fputs(d,f1);
  107.      fputc('\n',f1);
  108.      fputc('\n',f1);
  109.      fclose(f);
  110.      fclose(f1);
  111.      return 1;
  112. }//cifre
  113.  
  114. bool parole(int &numpar){
  115.      FILE* f;
  116.      FILE* f1;
  117.      f=fopen("prova.txt","r");
  118.      f1=fopen("analisi.txt","a");
  119.      if(f==NULL || f1==NULL)
  120.          return 0;
  121.      char s;
  122.      int j=0,i=0,k=0;
  123.      while(!feof(f)){
  124.          s=fgetc(f);
  125.          if(isascii(s))
  126.              j++;
  127.      }//while
  128.      char c;
  129.      char d[20];
  130.      char fff[20];
  131.      numpar=0;
  132.      fputs("Il numero di parole e`: ",f1);
  133.      fseek(f,0,SEEK_SET);
  134.      while(k<j){
  135.          c=fgetc(f);
  136.          if(isalpha(c)){
  137.              if(i==0){
  138.                  numpar++;
  139.                  i++;
  140.              }//if
  141.          }//if
  142.          else
  143.              i=0;
  144.          k++;
  145.      }//while
  146.      itoa(numpar,d,10);
  147.      fputs(d,f1);
  148.      fputc('\n',f1);
  149.      fputc('\n',f1);
  150.      fputs("Il numero di caratteri e`: ",f1);
  151.      itoa(j,fff,10);
  152.      fputs(fff,f1);
  153.      fputc('\n',f1);
  154.      fputc('\n',f1);
  155.      fclose(f);
  156.      fclose(f1);
  157.      return 1;
  158. }//parole
  159.  
  160. bool numeri(){
  161.      FILE* f;
  162.      FILE* f1;
  163.      f=fopen("prova.txt","r");
  164.      f1=fopen("analisi.txt","a");
  165.      if(f==NULL || f1==NULL)
  166.          return 0;
  167.      char s;
  168.      int j=0,i=0,k=0,cifre=0;
  169.      while(!feof(f)){
  170.          s=fgetc(f);
  171.          if(isascii(s))
  172.              j++;
  173.      }//while
  174.      char c;
  175.      char qnt[20];
  176.      fseek(f,0,SEEK_SET);
  177.      while(k<j){
  178.          c=fgetc(f);
  179.          if(isdigit(c)){
  180.              if(i==0){
  181.                  i++;
  182.                  cifre++;
  183.              }//if
  184.          }//if
  185.          else
  186.              i=0;
  187.          k++;
  188.      }//while
  189.      fputs("Il totale di numeri e`: ",f1);
  190.      itoa(cifre,qnt,10);
  191.      fputs(qnt,f1);
  192.      fputc('\n',f1);
  193.      fputc('\n',f1);
  194.      fclose(f);
  195.      fclose(f1);
  196.      return 1;
  197. }//numeri
  198.  
  199.  
  200. bool mediapar(int numpar, int numlet){
  201.      FILE* f;
  202.      FILE* f1;
  203.      f=fopen("prova.txt","r");
  204.      f1=fopen("analisi.txt","a");
  205.      if(f==NULL || f1==NULL)
  206.          return 0;
  207.      int d[N];
  208.      char m[20];
  209.      fputs("La lunghezza media delle parole e`: ",f1);
  210.      int med;//itoa accetta solo interi! maledizione..ed il cast non approssima :)
  211.      med=numlet/numpar;
  212.      itoa(med,m,10);
  213.      fputs(m,f1);
  214.      fputc('\n',f1);
  215.      fputc('\n',f1);
  216.      fclose(f);
  217.      fclose(f1);
  218.      return 1;
  219. }//mediapar
  220.      
  221. bool mediaparfrase(int numpar, int numfrasi){
  222.      FILE* f;
  223.      FILE* f1;
  224.      f=fopen("prova.txt","r");
  225.      f1=fopen("analisi.txt","a");
  226.      if(f==NULL || f1==NULL)
  227.          return 0;
  228.      int d[N];
  229.      char m[20];
  230.      fputs("La media delle parole per frase e`: ",f1);
  231.      int med;
  232.      if(numfrasi==0)
  233.          numfrasi=1;
  234.      med=numpar/numfrasi;
  235.      itoa(med,m,10);
  236.      fputs(m,f1);
  237.      fputc('\n',f1);
  238.      fputc('\n',f1);
  239.      fclose(f);
  240.      fclose(f1);
  241.      return 1;
  242. }//mediapar    
  243.  
  244. bool trovapar(char* parola){
  245.      FILE* f;
  246.      FILE* f1;
  247.      f=fopen("prova.txt","r");
  248.      f1=fopen("analisi.txt","a");
  249.      if(f==NULL || f1==NULL)
  250.          return 0;
  251.      char car;
  252.      char m[N];
  253.      int i=0,j=0,n=0;
  254.      while(!feof(f)){
  255.          car=fgetc(f);
  256.          if(tolower(car)==tolower(parola[i])){
  257.              j++;
  258.              if(j==strlen(parola)-1)
  259.                  n++;
  260.          }//if
  261.          else
  262.              j=0;
  263.          i++;
  264.      }//while
  265.      fputs("La parola e` presente: ",f1);
  266.      itoa(n,m,10);
  267.      fputs(m,f1);
  268.      fputs(" volte",f1);
  269.      fputc('\n',f1);
  270.      fputc('\n',f1);
  271.      fclose(f);
  272.      fclose(f1);
  273.      return 1;      
  274. }//trovapar              
  275.          
  276.  
  277.  
  278. int main(){
  279.     int numpar,numlet,numfrasi;
  280.     bool l=lettere(numlet);
  281.     bool p=parole(numpar);
  282.     bool f=frasi(numfrasi);
  283.     bool c=cifre();
  284.     bool n=numeri();
  285.     bool m=mediapar(numpar,numlet);
  286.     bool mf=mediaparfrase(numpar,numfrasi);
  287.     cout<<"\nControlli eseguiti con successo!\n";
  288.     cout<<"\nVuoi controlare quante volte compare una parola? (s/n)\n- ";
  289.     char scelta;
  290.     do{
  291.         cin>>scelta;
  292.         if(scelta!='s' && scelta!='n')
  293.             cout<<"\nErrore!\n";
  294.     }//do
  295.     while(scelta!='s' && scelta!='n');
  296.     if(scelta=='s'){
  297.         char parola[26];//vedi: precipitevolissimevolmente
  298.         cout<<"\nLa parola e`: ";
  299.         fflush(stdin);
  300.         cin.get(parola,26,'\n');
  301.         fflush(stdin);
  302.         bool t=trovapar(parola);
  303.         end();
  304.     }//if
  305.     else
  306.         end();
  307.     return 0;
  308. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement