#include <iostream>
#include<string.h>
#include<stdlib.h>
#include<time.h>
using namespace std;

const int CHMAX = 10 ;
const int MAXMOTS = 20 ;

struct mot
{
    int longueur;
    int nb_voy;
    char chaine[CHMAX];
};

struct phrase
{
    int nb_mots;
    struct mot tab_mot[MAXMOTS];
};

int compte_voyelles (char ch[CHMAX])
{
    int i, nb=0,lg;
    lg = strlen(ch);
    for (i=0; i<lg;i++)
    {
        if (ch[i]=='a' || ch[i]=='e'  || ch[i]=='i'  || ch[i]=='o'  || ch[i]=='u'  || ch[i]=='y')
            nb++;
    }
    return nb;
}

struct mot genere_mot()
{
        struct mot m;
        int i;
        m.longueur = rand()% 9 + 2;
        do {
        for (i=0;i<m.longueur;i++)
        {
            m.chaine[i] = rand()%26 + 'a';
        }
        m.chaine[m.longueur] = '\0';
        }
        while (compte_voyelles(m.chaine) <m.longueur/2);
        m.nb_voy = compte_voyelles(m.chaine) ;
        return m;
}

void affiche_mot(struct mot m)
{
    cout<<"Longueur du mot : "<<m.longueur<<endl;
    cout<<"Mot genere : "<<m.chaine<<endl;
}

void ajoute_un_mot (struct phrase &phr)
{
    if (phr.nb_mots<MAXMOTS)
    {
        phr.tab_mot[phr.nb_mots] = genere_mot();
        phr.nb_mots++;
    }
}

void affiche_phrase (struct phrase phr)
{
    int i;
    for (i=0 ; i<phr.nb_mots ; i++)
    {
        cout<<phr.tab_mot[i].chaine<<" ";
    }
}

void statistiques (phrase ph, int &voyelles_totales, float &moy_lg)
{
    int i;
    voyelles_totales = 0 ;
    moy_lg = 0;
    for (i=0 ; i<ph.nb_mots;i++)
    {
        voyelles_totales+=ph.tab_mot[i].nb_voy;
        moy_lg+=ph.tab_mot[i].longueur;
    }
    moy_lg/=ph.nb_mots;
}
int main (void)
{
        struct mot m;
        struct phrase p;
        int voyelles;
        char rep;
        float moy_long;
        p.nb_mots=0;
        int i;
        srand(time(NULL));
       do
        {
            ajoute_un_mot(p);
            affiche_phrase(p);
            cout<<endl<<"encore un mot ? 'O' ou 'o' pour oui "<<endl;
            cin>>rep;
        }
        while (rep == 'O' || rep == 'o');
        statistiques(p,voyelles,moy_long);
        cout<<"longueur moyenne des mots : "<<moy_long<<" et nombre total de voyelles : "<<voyelles<<endl;
        return 0;
}
