Fandom

Scratchpad

PLE:Unidad6Ejercicio06

215,933pages on
this wiki
Add New Page
Discuss this page0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

/*
Programa:         Fichero06.cpp
Descripción:      Contar la frecuencia de aparición de palabras en un fichero de texto.
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctype>
 
using namespace std;
 
struct elemento
{
        char cad[25];
        unsigned int cont;
};
 
void inserta(elemento f[], unsigned int &n, char *c);
void muestra(ostream &fs, elemento f[], unsigned int n);
void incrementa(elemento f[], unsigned int n);
int busca(elemento f[], unsigned int n, char *c);
void ordena(elemento f[], unsigned int n);
void minuscula(char *c);
 
const int NUM=80;
const int NUMPAL=2000;
 
void main ()
{
        char nomFicTex[NUM], nomFicRes[NUM],pal[25];
        elemento frec[NUMPAL];
        unsigned int numElem = 0;
        int p;
 
        ifstream fe;
        ofstream fs;
 
        cout << "Introduzca el nombre del fichero a leer ";
        cin >> nomFicTex;
 
        cout << "Introduzca el nombre del fichero con los resultados ";
        cin >> nomFicRes;
 
 
        fe.open(nomFicTex);
        if(!fe.good())
        {
                cout << "ERROR. Problema al acceder al fichero " << nomFicTex;
                exit(0);
        }
 
        fs.open(nomFicRes);
        if(!fs.good())
        {
                cout << "ERROR. Problema al acceder al fichero " << nomFicRes;
                exit(0);
        }
 
 
        fe >> pal;
        while(!fe.eof())
        {
                // Buscamos el caracter
                minuscula(pal);
                p = busca(frec, numElem, pal);
                if(p == -1)
                        inserta(frec, numElem, pal);
                else
                        incrementa(frec, p);
                fe >> pal;
        }
        ordena(frec, numElem);
        muestra(fs, frec, numElem);
 
        fe.close();
        fs.close();
 
        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(ostream &fs, elemento f[], unsigned int n)
{
        for(unsigned int i = 0; i < n; i++)
                fs << f[i].cad << '\t' << f[i].cont << endl;
        fs << "TOTAL palabras = " << n;
}
 
int busca(elemento f[], unsigned int n, char *c)
{
        int pos = -1;
        unsigned int 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 minuscula(char *c)
{
        for(unsigned int i = 0; i < strlen(c); i++)
        {
                if(c[i] == 'Ñ')
                        c[i] = 'ñ';
                else
                        c[i] = tolower(c[i]);
        }
}
 
void ordena(elemento f[], unsigned int n)
{
        unsigned int i, j, k;
        elemento aux;
        for (i = 0; i < n-1; i++)
        {
                k = i;
                strcpy(aux.cad, f[i].cad);
                aux.cont = f[i].cont;
                for (j = i+1; j <= n-1; j++)
                {
                        if(f[j].cont > aux.cont)
                        {
                                k = j;
                                strcpy(aux.cad,f[j].cad);
                                aux.cont = f[j].cont;
                        }
                }
                strcpy(f[k].cad, f[i].cad);
                f[k].cont = f[i].cont;
 
                strcpy(f[i].cad, aux.cad);
                f[i].cont = aux.cont;
        }
}

Also on Fandom

Random wikia