M1102 – TP6 – Exercice 15

Modifier le fichier nsUtil.h en ajoutant la fonction EditVStringV4 () de signature :

void EditVStringV4 (const std::vector <std::string> & VString);

Cette fonction doit afficher à l’écran tout le code ASCII de tous les caractères alphabétiques (majuscules / ou minuscules) de chaque ligne du vecteur VString.

Nb : pour savoir si un caractère est alphabétique, vous devez utilisez la fonction isalpha ().

M1102 – TP6 – Exercice 16

Dans le fichier nsUtil.h, ajouter les deux lignes suivantes :

    const std::string KLeet ("/-\\;|3;(;|);3;|=;9;|-|;1;_|;|<;|_;|\\/|;|\\|;0;|2;Q;|?;$;'][';|_|;\\/;\\/\\/;><;`/;2;");
    //                        a     b c d  e  f g  h  i  j  k  l  m    n    o p  q  r s  t    u  v   w     x   y  z

La première ligne est la correspondance entre notre alphabet et l’alphabet leet, la seconde correspond à notre alphapbet (elle n’est présente que pour vous aidez).

Nb : a l’instar des fichiers CSV, chaque lettre de l’alphabet Lett est séparée par le caractère ';' dans la chaine KLeet.

Ecrire le corps de la fonction ToLett () de signature:

string ToLeet (char c);

Cette fonction prend en paramètre un caractère est renvoie la chaine de caractères correspondante dans l’alphabet Lett.

Ecrire le corps des deux fonctions suivantes :

vector <string> & ToLeet (vector <string> & VString) ;
vector <string> & ToLeet (vector <string> & VString, const unsigned & LineNumber) ;

La première chaque caractère de toutes les lignes du vecteur en son alphabet Leet correspondant, la seconde ne s’applique qu’à la ligne LineNumber.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « lee» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 – Exercice 17

Ecrire les fonctions duales de celles de l‘exercice 16 de façon a transformer une ligne / l’intégralité du vecteur encodé en transformant chaque (chaine de) caractères de l’alphabet Leet en alphabet “standard“.

Modifier le main () pour prendre en compte ses deux fonctions. L’appel de ces fonctions se fait en saisissant la chaine « unl» suivie de « tout » ou d’un numéro de ligne.

M1102 – TP6 Corrigés (exo1 – 5)

#include <iostream>
#include <vector>
#include <cctype>
#include <utility>

#include "nsUtil.h"

using namespace std;
using namespace nsUtil;
string & ToUpper (string & Str)
{
    for (char & car : Str)
        car = toupper (car);
    return  Str;
}

vector <string> & ToUpper (vector <string> & VString)
{
    for (string & str : VString)
        str = ToUpper(str);
    return VString;
}

vector <string> & ToUpper (vector <string> & VString, const unsigned & LineNumber)
{
    ToUpper(VString [LineNumber]);
    return VString;
}

string & ToLower (string & Str)
{
    for (char & car : Str)
        car = tolower (car);
    return  Str;
}

vector <string> & ToLower (vector <string> & VString)
{
    for (string & str : VString)
        str = ToLower (str);
    return VString;
}

vector <string> & ToLower (vector <string> & VString, const unsigned & LineNumber)
{
    ToLower (VString [LineNumber]);
    return VString;
}



vector <string> & Swap (vector <string> & VString, const unsigned & PosBeg, const unsigned & PosEnd)
{
    swap (VString[PosBeg], VString[PosEnd]);
    return VString;
}


int main(int argc, char *argv[])
{
    if (2 != argc)
    {
        cerr << "gros boulet" << endl;
        exit (0);
    }

    string InputFile (argv[1]);
    //unsigned long Width (stoul(argv[2]));
    vector <string> VFile (FileToVectString (InputFile));
    EditVStringV3 (VFile);
    string Instruction, Complement;
    while (true)
    {
        cin >> Instruction;
        Instruction = ToLower (Instruction);
        if ("fin" == Instruction)
        {
            break;
        }
        //sauvergarde
        else if ("sav" == Instruction)
        {
            string FileOut;
            cin >> FileOut;
            VectStringToFile (VFile, FileOut);

        }
        //majuscule
        else if ( "maj" ==  Instruction)
        {
            cin >> Complement;
            if (ToLower(Complement) == "tout")
                ToUpper (VFile);
            else
                ToUpper (VFile, stoul(Complement));
        }
        //minuscule
        else if ( "min" ==  Instruction)
        {
            cin >> Complement;
            if (ToLower (Complement) == "tout")
                ToLower (VFile);
            else
                ToLower (VFile, stoul(Complement));
        }

        EditVStringV2 (VFile);
    }//while

    return 0;
}


M1102 – TP4 Corrigés

#include <iostream>
#include <cctype> //isspace ()
#include "EasyAssert.h"
using namespace std;

unsigned CompterDoublonsV1 (const string & Chaine)
{
unsigned Cpt (0);
for (unsigned i (0); i < Chaine.size()-1; i = i + 1)
{
if (isspace(Chaine [i])) continue;
if (Chaine[i] == Chaine [i+1])
{
Cpt = Cpt + 1;
i = i + 1;
}
}
return Cpt;
}//CompterDoublonsV1

void TestCompterDoublonsV1()
{
EASY_ASSERT(CompterDoublonsV1 ("aaaa")==2);
EASY_ASSERT(CompterDoublonsV1 ("aaaaa")==2);
EASY_ASSERT(CompterDoublonsV1 ("aaaaaa")==3);
}//TestCompterDoublonsV1

unsigned CompterDoublonsV2 (const string & Chaine)
{
unsigned Cpt (0);
for (unsigned i (0); i < Chaine.size()-1; i = i + 1)
{
if ((isspace(Chaine [i]) && isspace (Chaine[i + 1])) || (Chaine[i] == Chaine [i+1]))
{
Cpt = Cpt + 1;
}
}
return Cpt;
}//CompterDoublonsV2

void TestCompterDoublonsV2()
{
EASY_ASSERT(CompterDoublonsV2 ("aa  aa")==3);
EASY_ASSERT(CompterDoublonsV2 ("aa  aaa")==4);
EASY_ASSERT(CompterDoublonsV2 ("aa \taa")==3);
}//TestCompterDoublonsV2

unsigned CompterDoublonsV3 (const string & Chaine)
{
unsigned Cpt (0);
char CarPrec (Chaine [0]);
for (unsigned i (0); i < Chaine.size()-1; i = i + 1)
{
if (isspace(Chaine [i])) continue;
if (Chaine[i] == CarPrec)
{
Cpt = Cpt + 1;
}
}
return Cpt;
}//CompterDoublonsV3

void TestCompterDoublonsV3()
{
EASY_ASSERT(CompterDoublonsV2 ("aa  aa")==3);
EASY_ASSERT(CompterDoublonsV2 ("aa  aaa")==4);
EASY_ASSERT(CompterDoublonsV2 ("aa \taa")==3);
}//TestCompterDoublonsV3

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

unsigned long long FindSubstrInStr(const string & Chaine, const string & SousChaine,
unsigned long long PosDep)
{
if (0 == Chaine.size() || 0 == SousChaine.size() || Chaine.size() + PosDep < SousChaine.size())
return string::npos;
unsigned long long i (TrouveCarDansStr (SousChaine [0], Chaine, PosDep));
while (i < Chaine.size() - SousChaine.size())
{
unsigned long long j (1);
while (j < SousChaine.size() && (Chaine [i + j] == SousChaine [j]))
{
j = j + 1;
}

if (j == SousChaine.size()) return i;
i = TrouveCarDansStr (SousChaine [0], Chaine, i + 1);
}
return string::npos;
}// FindSubstrInStr ()

unsigned long long FindSubstrInStrV2 (const string & Chaine, const string & SousChaine,
unsigned long long PosDep)
{
if (0 == Chaine.size() || 0 == SousChaine.size() || Chaine.size() + PosDep < SousChaine.size())
return string::npos;

for (unsigned long long i (PosDep); i < Chaine.size() - SousChaine.size(); i = i +1)
{
if (Chaine [i] != SousChaine[0]) continue;
unsigned long long j (1);
while (j < SousChaine.size() && (Chaine [i + j] == SousChaine [j]))
{
j = j + 1;
}

if (j == SousChaine.size()) return i;
}
return string::npos;
}//FindSubstrInStrV2 ()

unsigned long long FindSubstrInStrV3 (const string & Chaine, const string & SousChaine,
unsigned long long PosDep)
{
if (0 == Chaine.size() || 0 == SousChaine.size() || Chaine.size() + PosDep < SousChaine.size())
return string::npos;

for (unsigned long long i (PosDep); i < Chaine.size() - SousChaine.size(); i = i +1)
{
if (Chaine.substr(i, SousChaine.size()) == SousChaine) return i;
}
return string::npos;
}//FindSubstrInStrV3 ()

void TestFindSubstrInStr ()
{
string Chaine ("Ma jolie chaine");
string SousChaine ("jolie");
EASY_ASSERT(FindSubstrInStr (Chaine, SousChaine, 0) == 3);
EASY_ASSERT(FindSubstrInStrV2 (Chaine, SousChaine, 0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStrV3 (Chaine, SousChaine, 0));

SousChaine = "pas";
EASY_ASSERT(FindSubstrInStr (Chaine, SousChaine, 0) == string::npos);
EASY_ASSERT(FindSubstrInStrV2 (Chaine, SousChaine, 0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStr (Chaine, SousChaine, 0));
EASY_ASSERT(Chaine.find(SousChaine,0) == FindSubstrInStrV3 (Chaine, SousChaine, 0));
}// TestFindSubstrInStr ()

int main()
{
//TestCompterDoublonsV1 ();
//TestCompterDoublonsV2 ();
//TestCompterDoublonsV3();
TestFindSubstrInStr ();
return 0;
}

M1102 – TP5 – Exercice 3 (Supplémentaire)

Reprenez le premier exercice (peu importe l’approche : mot / mot ou caractère : caractère), mais cette fois-ci, vous devez compter les mots en cherchant les fin de mots.
En conséquence vous devez effectuer un parcours de la fin de la chaine vers le début.

Ecrivez une fonction de test à l’aide de easyassert.

N’oublier de faire des tests avec des chaines vides.