PLE:Unidad4Ejercicio25

/* Programa:        Unidad4Ejercicio25.cpp Fecha:           02/02/2007 Descripción:     Lee varias cadenas y cuenta frecuancia de palabras. using namespace std;
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 1) include

struct elemento {       char cad[25]; unsigned int cont; };

const int NUM = 80;

void inserta(elemento f[], unsigned int &n, char *c); void muestra(elemento f[], unsigned int n); void incrementa(elemento f[], unsigned int n); int busca(elemento f[], unsigned int n, char *c); void strEP(char *c, char *p, unsigned int &n);

void main {       char cadena[80], pal[25]; elemento frec[NUM]; unsigned int numElem = 0, i, p;

cout << "Introduzca una cadena "; cin.getline(cadena,80);

do       { i = 0; while(i < strlen(cadena)) {                       // Extraemos una palabra strEP(cadena, pal, i); // Procesamos la palabra p = busca(frec, numElem, pal); if(p == -1) inserta(frec, numElem, pal); else incrementa(frec, p); }               cout << "Introduzca una cadena "; cin.getline(cadena,80); }while(strlen(cadena) > 0);

muestra(frec, numElem);

cin.ignore; cin.get; }

void inserta(elemento f[], unsigned int &n, char *c) {       strcpy(f[n].cad,c); f[n].cont = 1; n++; }

void muestra(elemento f[], unsigned int n) { cout << "Cad.\tNum\n"; for(unsigned int i = 0; i < n; i++) cout << f[i].cad << '\t' << f[i].cont << endl; }

int busca(elemento f[], unsigned int n, char *c) {       int pos = -1, i = 0; while(i < n && pos == -1) {               if(strcmp(f[i].cad,c) == 0) pos = i;               i++; }       return pos; }

void incrementa(elemento f[], unsigned int n) { f[n].cont++; }

void strEP(char *c, char *p, unsigned int &n) {       unsigned int i = 0; while(c[n] != ' ' && c[n] != '\t' && c[n] != '\0') {               p[i] = c[n]; i++; n++; }       p[i] = '\0'; while(c[n] == ' ' || c[n] == '\t') n++; }