Fandom

Scratchpad

PLE:Unidad4Ejercicio24

216,229pages 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:         Unidad4Ejercicio24.cpp
Fecha:            30/01/2007
Descripción:      Lee varias cadenas y cuenta frecuancia de caracteres.
*/
#include <iostream>
#include <conio>
#include <stdlib>
#include <time>
using namespace std;

struct elemento
{
        char car;
        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 ordena(elemento f[], unsigned int n);

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

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

        do
        {
                for(i = 0; i < strlen(cadena); i++)
                {
                        p = busca(frec, numElem, cadena[i]);
                        if(p == -1)
                                inserta(frec, numElem, cadena[i]);
                        else
                                incrementa(frec, p);
                }
                cout << "Introduzca una cadena ";
                cin.getline(cadena,80);
        }while(strlen(cadena) > 0);

        ordena(frec, numElem);
        muestra(frec, numElem);

        cin.ignore();
        cin.get();
}

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

void muestra(elemento f[], unsigned int n)
{
        cout << "Car.\tNum\n";
        for(unsigned int i = 0; i < n; i++)
                cout << f[i].car << '\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(f[i].car == c)
                        pos = i;
                i++;
        }
        return pos;
}

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

void ordena(elemento f[], unsigned int n)
{
        int i, j, k;
        elemento aux;
        for (i = 0; i < n-1; i++)
        {
                k = i;
                aux.car = f[i].car;
                aux.cont = f[i].cont;
                for (j = i+1; j <= n-1; j++)
                {
                        if (f[j].cont < aux.cont)
                        {
                                k = j;
                                aux.car = f[j].car;
                                aux.cont = f[j].cont;
                        }
                }
                f[k].car = f[i].car;
                f[k].cont = f[i].cont;

                f[i].car = aux.car;
                f[i].cont = aux.cont;
        }
}

Also on Fandom

Random wikia