Zdrojové kódy pro vývojáře.
Skip Navigation Links. Top 10 přispěvatelů
UživatelČlánky
codeshare49
sochor1
stoupa1
tomas.oplt11
Článek: Algoritmus - statistiky textového souboru
Špatný
Super
Autor:
Vytvořeno:
Popularita:

Statistiky počítají následující 
Počet řádků, slov, písmen
Seřazené podle počtu výskytu
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include int main(int argc, char* argv[])
{
 
 FILE * file = NULL;
 int charCnt[127];
 for ( int i = 0;i<=126;i++)
  charCnt[i]=0;
 if ( argc < 2 )
 {
  printf("File path is not specified");
 }
 else
 {
  if ( (file=fopen(argv[1], "r")) == NULL )
  { 
   fprintf(stderr, "Cannot open file %s\n", argv[1]);
   return 0;
  }
 }  int nchars, nwords, nlines;
 int ncharsSum, nwordsSum, nlinesSum;
 int lastnblank;  
 char c;  nchars=nwords=nlines=lastnblank=0;
 ncharsSum=nwordsSum=nlines=nlinesSum=0;
 while((c=getc(file))!=EOF)
 {
  // increase quantity for this character
  charCnt[c] += 1;   nchars++;
  if (c=='\n')
  {
   if (lastnblank)
    nwords++;
   
   printf("words=%d, characters=%d\n", nwords, nchars);
   ncharsSum+=nchars;
   nwordsSum+=nwords;    nchars=nwords=lastnblank=0;
   
   nlines++;
  }
  else
  {
  if (((c==' ') || (c=='\t')) & (lastnblank))
   nwords++;
   lastnblank=((c!=' ')&&(c!='\t'));
  }
 }
 printf("characters=%d\n", ncharsSum);
 printf("words=%d\n", nwordsSum);
 printf("lines=%d\n", nlines);  char sz[20];  for ( int i2 = 0;i2<=126;i++)
 {
  sz[0]=i2;
  sz[1]=0;
  printf("Character %s Cnt:%i2\n", sz, charCnt[i2]);
 }  
 char txt[32] ;
  printf("\n\nEnter text which you want to look for:\n ");
 gets(txt);
 // goto begin of the file
 int res = fseek(file,0,SEEK_SET);
 if ( res != 0 ) return 0;  int CharsIdentical=0;
 int iPos = 0;  while((c=getc(file))!=EOF)
 {
  iPos+=1;
  if ( CharsIdentical == 0 )
  {
   if ( c == txt[0] )
   {
    CharsIdentical = 1;
   }
  }
  else
  {
   if ( txt[CharsIdentical] == 0 )
   {
    bool bFound =true;
    printf("\nText found on position %i", iPos);
    getch();
    return 0;
   }
   else
   {
    if ( txt[CharsIdentical] == c )
     CharsIdentical++;
   }
  }
 }  printf("\nText not found", iPos);
 getch();
 return 0; }
  Na stránku 
screen  Nový příspěvek
Název  Uživatel  Datum 
Poslední návštěva: 19:16:40, 24. dubna 2024 První  Předchozí  0 Záznamů  Další  Poslední  

Autor článku
Jméno
Pracovní pozice
Informace
Foto

   

Počet návštěvníků:1
 
  Kontakt