M1103 – TP1 – Exercice 4

Insérer dans le code suivant vos algorithmes de tri.
Modifier le .pro en ajoutant la ligne LIBS += -pthread

Puis faite divers tests en faisant varier les paramètres d’entrée :

  1. la taille des vecteurs;
  2. le nombre de vecteurs différents;
  3. nombre d’itérations par vecteur;
#include <iostream>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <thread>

using namespace std;

typedef vector <unsigned> CVUint;
typedef vector < vector <double>> CMatrix;

CMatrix Mat;

void SelectSort (CVUint & VUint)
{
//todo
}

void InsertSort (CVUint & VUint)
{
//todo
}

void BubbleSort (CVUint & VUint)
{
//todo
}

void LanguageSort (CVUint & VUint)
{
    sort (VUint.begin(), VUint.end());
}

void InitMat (unsigned NbColumns)
{
    Mat.resize(4, vector <double> (NbColumns));
}

//http://stackoverflow.com/questions/2962785/c-using-clock-to-measure-time-in-multi-threaded-programs
void protoGenericSort(void (*Sort) (CVUint & VUint), const CVUint & VUint, unsigned NbFois, unsigned PosMat, unsigned VectNum)
{
    for (unsigned i (0); i < NbFois; ++i)
    {
        CVUint CopyVUint (VUint);
        struct timespec start, finish;
        double elapsed;
        clock_gettime(CLOCK_MONOTONIC, &start);
        Sort (CopyVUint);
        clock_gettime(CLOCK_MONOTONIC, &finish);
        elapsed = (finish.tv_sec - start.tv_sec);
        elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
        Mat [PosMat][i + NbFois * VectNum] = elapsed;
    }
}

void TraiterResultats (const unsigned & NbElemAEnlever)
{
    for (vector <double> & UneLigne : Mat)
    {
        //tri
        sort (UneLigne.begin(), UneLigne.end());
        //suppresion des éléments non significatifs
        UneLigne = vector <double> (UneLigne.begin() + NbElemAEnlever / 2, UneLigne.end() - NbElemAEnlever / 2 );
        //plus petit temps
        double min (UneLigne[0]);
        //plus grand temps
        double max (UneLigne[UneLigne.size()-1]);
        //temps median
        double med (UneLigne[UneLigne.size()/2]);

        //On assigne les valeurs memorisees aux 3 premières cases
        UneLigne[0] = min;
        UneLigne[1] = med;
        UneLigne [2] = max;
    }
    //Affichage
    cout << setw (20) << "Tri" << setw (10) << "Min" << setw (10) << "Med" << setw (10) << "Max" << endl;
    vector<string> VMetode {"Selection", "Insertion", "Bulles", "Langage"};
    for (unsigned i (0); i < VMetode.size(); ++i)
        cout << setw (20) << VMetode[i] << setw (10) << setprecision(6) << Mat[i][0] << setw (10) << setprecision(6) << Mat[i][1] << setw (10) << setprecision(6) << Mat[i][2] << endl;

}

int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        cerr << "boulette !\n utilisation : " << argv [0] << " (1) NbElem par vecteur (2) Nb de vecteurs differents (3) Nb itérations par vecteur" << endl;
        return 1;
    }

    unsigned NbElem (stoul(argv[1]));
    unsigned NbVecteurs (stoul(argv[2]));
    unsigned NbFois (stoul(argv[3]));

    srand (time(NULL));
    CVUint VUInt (NbElem);
    InitMat(NbFois * NbVecteurs);

    for (unsigned i (0); i < NbVecteurs; ++i)
    {
        for (auto & Val : VUInt)
            Val = rand () % (VUInt.size() * 10);

        thread th1 (protoGenericSort,SelectSort, VUInt, NbFois, 0, i);
        thread th2 (protoGenericSort, InsertSort, VUInt, NbFois, 1, i);
        thread th3 (protoGenericSort, BubbleSort, VUInt, NbFois, 2, i);
        thread th4 (protoGenericSort, LanguageSort, VUInt, NbFois, 3, i);
        th1.join();
        th2.join();
        th3.join();
        th4.join();
        cout << i << "fini" << endl;
    }

    cout << "Taille des vecteurs : " << NbElem << "\nNb de vecteurs : " << NbVecteurs << "\nNb iterations par vecteur : " << NbFois << endl;
    TraiterResultats (NbFois * NbVecteurs / 10);
    return 0;
}

M1103 – TP2 Correction – PacMan

/*!
* \file   main.cpp
* \authors Alain Casali, Marc Laporte
* \date december 8 2016
* \brief 	Terminal's color management
* 			beginning of the project titled "catch me if you can"
*/

#include <iostream>
#include <vector>

using namespace std;

//! 1st player's token
const char KTokenPlayer1 = 'X';
//! 2nd player's token
const char KTokenPlayer2 = 'O';
//! empty position token
const char KEmpty        = ' ';

//!alias to the reset color display
const string 	KReset    = "0";
//!alias to the black color
const string  	KBlack    = "30";
//!alias to the red color
const string  	KRed      = "31";
//!alias to the green color
const string  	KGreen    = "32";
//!alias to the yellow color
const string  	KYellow   = "33";
//!alias to the blue color
const string 	KBlue     = "34";
//!alias to the mangenta color
const string  	KMagenta  = "35";
//!alias to the cyan color
const string 	KCyan     = "36";

/*!
* \brief Clear the terminal
*/

void ClearScreen ()
{
cout << "\033[H\033[2J";
} //ClearScreen ()

/*!
* \brief Display color in the terminal
* \param[in] Col the color in which the next text will be displayed in
*/
void Color (const string & Col)
{
cout << "\033[" << Col <<"m";
} // Color()

/*!
* \brief alias to vector of char,
* represents a line of the matrix
*/
typedef vector <char> CVLine;

/*!
* \brief alias to the Matrix
*/
typedef vector <CVLine> CMatrix;

/*!
* \brief represent a pair (unsigned, unsigned) : a position in the matrix
* \brief 1st element is coordinate on the x-axis, 2nd element is the coordinate on the y-axis
*/
typedef pair <unsigned, unsigned> CPosition;

/*!
* \brief Initialization of the Matrix from scratch
* \param[out] Mat the matrix to be initialized
* \param[in] NbLine number of lines of the matrix Mat
* \param[in] NbColumn number of columns of the matrix Mat
* \param[out] PosPlayer1 position of the first player's token in Mat
* \param[out] PosPlayer2 position of the second player's token in Mat
*/
void InitMat (CMatrix & Mat, unsigned NbLine, unsigned NbColumn, CPosition & PosPlayer1, CPosition & PosPlayer2)
{
Mat.resize (NbLine);
const CVLine KLine (NbColumn, KEmpty);
for (CVLine &ALine : Mat)
ALine = KLine;

PosPlayer1.first = 0;
PosPlayer1.second = NbColumn - 1;
Mat [PosPlayer1.first][PosPlayer1.second] = KTokenPlayer1;
PosPlayer2.first = NbLine - 1;
PosPlayer2.second =0;
Mat [PosPlayer2.first][PosPlayer2.second]   = KTokenPlayer2;
}//InitMat ()

/*!
* \brief Display the matrix
* \param[in] Mat the matrix to be displayed
*/
void ShowMatrix (const CMatrix & Mat)
{
const unsigned KNbLine = Mat.size ();
const unsigned KNbCol  = Mat[0].size ();
cout << string (KNbCol + 2 , '-') << endl;
for (unsigned i (0); i < KNbLine; ++i)
{
cout << '|';
for (char c : Mat[i])
{

switch (c)
{
case KEmpty:
cout << c;
break;
case KTokenPlayer1:
Color (KBlue);
cout << c;
Color (KReset);
break;
case KTokenPlayer2:
Color (KRed);
cout << c;
Color (KReset);
break;

}
}
cout << '|' << endl;
}
cout << string (KNbCol + 2 , '-') << endl;
}// ShowMatrix ()

/*!
* \brief Move the current token according to the character in the 2nd parameter
* \param[in, out] Mat the matrix before and after the move
* \param[in] Move the key pressed by the user
* \param[in, out] Pos the player's position before and after the move
*/

void MoveToken (CMatrix & Mat, char Move, CPosition & Pos)
{
char car = Mat [Pos.first][Pos.second];
Mat [Pos.first][Pos.second] = KEmpty;
switch (Move)
{
case 'A':
-- Pos.first;
-- Pos.second;
break;
case 'Z':
--Pos.first;
break;
case 'E':
--Pos.first;
++Pos.second;
break;
case 'Q':
--Pos.second;
break;
case 'D':
++Pos.second;
break;
case 'W':
++Pos.first;
--Pos.second;
break;
case 'X':
++Pos.first;
break;
case 'C':
++Pos.first;
++Pos.second;
break;
}
Mat [Pos.first][Pos.second] = car;
} //MoveToken ()

/*!
* \brief New main programm
* \return 1 if draw 0 otheriwse
*/
int ppal (void)
{

const unsigned KSize (10);
unsigned PartyNum (1);
const unsigned KMaxPartyNum (KSize * KSize);
CMatrix Mat;

bool Player1Turn (true);
bool Victory (false);

CPosition PosPlayer1, PosPlayer2;

InitMat (Mat, KSize, KSize, PosPlayer1, PosPlayer2) ;

ShowMatrix (Mat);

while (PartyNum <= KMaxPartyNum && ! Victory)
{

cout << "tour numero : " << PartyNum << ", Joueur"
<< (Player1Turn ? '1' : '2') << ", entrez un déplacement : ";

char Move;
cin >> Move;

Move = toupper (Move);
MoveToken (Mat, Move, (Player1Turn ? PosPlayer1: PosPlayer2));
ClearScreen();
ShowMatrix (Mat);

//Victiry test
if (PosPlayer1 == PosPlayer2) Victory = true;

//Increase party's number
++PartyNum;

//Player changing
Player1Turn = !Player1Turn;
}//while (no victory)

if (!Victory)
{
Color (KMagenta);
cout << "Aucun vainqueur" << endl;
return 1;
}

Color (KGreen);
cout << "Félicitations Joueur" << (Player1Turn ? '2' : '1')
<< " vous avez gagné :)" << endl;
Color (KReset);
return 0;
} //ppal ()

/*!
* \brief main
* \return ppal () value
*/
int main ()
{
return ppal ();

}// main ()