M1102-TP2 Corrigés

#include <iostream>
#include <vector>
#include "EasyAssert.h"
using namespace std;

unsigned TrouveCarDansStr (const char & Caract,
const string & Chaine,
const unsigned & Debut)
{
unsigned Pos (Debut);
for (; Pos < Chaine.size() && Chaine[Pos] != Caract; Pos = Pos + 1);
return Pos;
}//TrouveCarDansStr ()

void TestTrouveCarDansStr ()
{
string Chaine  ("Ma jolie chaine");
EASY_ASSERT (Chaine.size() == TrouveCarDansStr ('a', Chaine, 0));
//EASY_ASSERT (Chaine.size() != TrouveCarDansStr ('a', Chaine, 0));
EASY_ASSERT (Chaine.size() == TrouveCarDansStr ('z', Chaine, 0));
//EASY_ASSERT (Chaine.size() != TrouveCarDansStr ('z', Chaine, 0));
EASY_ASSERT (Chaine.size() == TrouveCarDansStr ('e', Chaine, 10));
EASY_ASSERT (Chaine.size() != TrouveCarDansStr ('e', Chaine, 0));
EASY_ASSERT (8 == TrouveCarDansStr ('e', Chaine, 0));
EASY_ASSERT (7 == TrouveCarDansStr ('e', Chaine, 0));
}//TestTrouveCarDansStr ()

unsigned ComptCaract (const char & Caract,
const string & Chaine,
const unsigned & Debut)
{
unsigned Cpt (0);
unsigned Pos (Debut);
for (; Pos < Chaine.size(); Pos = Pos + 1)
{
if (Chaine [Pos] == Caract)
{
Cpt = Cpt + 1;
}
}
return Cpt;
}

void TestComptCaract ()
{
string Chaine  ("Ma jolie chaine");
EASY_ASSERT (0 == ComptCaract ('a', Chaine, 0));
EASY_ASSERT (2 == ComptCaract ('a', Chaine, 0));
EASY_ASSERT (0 == ComptCaract ('z', Chaine, 0));
}//TestComptCaract ()

unsigned ComptCaractV2 (const char & Caract,
const string & Chaine,
const unsigned & Debut)
{
unsigned Cpt (0);

for (unsigned Pos (TrouveCarDansStr (Caract, Chaine, Debut));
Pos < Chaine.size(); Pos = TrouveCarDansStr (Caract, Chaine, Pos + 1))
{
Cpt = Cpt + 1;
}
return Cpt;
}//ComptCaractV2 ()

void TestComptCaractV2 ()
{
string Chaine  ("Ma jolie chaine");
EASY_ASSERT (0 == ComptCaractV2 ('a', Chaine, 0));
EASY_ASSERT (2 == ComptCaractV2 ('a', Chaine, 0));
EASY_ASSERT (0 == ComptCaractV2 ('z', Chaine, 0));
}//TestComptCaractV2 ()

vector<unsigned> ComptEnsembleDansChaine (const string & ChaineACompter, const string & ChaineDeComptage)
{
vector <unsigned> VecCpt (ChaineACompter.size());
for (unsigned i (0); i < VecCpt.size(); i = i + 1)
{
VecCpt [i] = ComptCaractV2 (ChaineACompter[i], ChaineDeComptage, 0);
}
return VecCpt;
}// ComptEnsembleDansChaine()

void TestComptEnsembleDansChaine ()
{
string Chaine  ("Ma jolie chaine");
string Voyelle ("aeiouy");
vector <unsigned> VCpt (ComptEnsembleDansChaine (Voyelle, Chaine));
EASY_ASSERT (VCpt [0] == 2); //a
EASY_ASSERT (VCpt [1] == 2); //e
EASY_ASSERT (VCpt [2] == 2); //i
EASY_ASSERT (VCpt [3] == 1); //o
EASY_ASSERT (VCpt [4] == 0); //u
EASY_ASSERT (VCpt [5] == 0); //y
}//TestComptEnsembleDansChaine ()

int main ()
{
TestTrouveCarDansStr ();
//TestComptCaract ();
//TestComptCaractV2 ();
//TestComptEnsembleDansChaine ();
return 0;
}