TP2
This commit is contained in:
82
TP2/2.1.cpp
Normal file
82
TP2/2.1.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void Fct1();
|
||||
void Fct2(int &A);
|
||||
void Fct3(int A);
|
||||
void Fct4(float &B, char &C, string &D);
|
||||
void Fct5(float B, char C, string D);
|
||||
string Fct6(float B, char C, string D);
|
||||
|
||||
// ostream &(*pfs)(ostream &o)Fct6Bis(float B, char C, string D);
|
||||
|
||||
function<ostream &(ostream &)> Fct6Bis(float B, char C, string D);
|
||||
|
||||
void main()
|
||||
{
|
||||
int A;
|
||||
float B;
|
||||
char C;
|
||||
string D;
|
||||
|
||||
Fct1();
|
||||
Fct2(A);
|
||||
Fct3(A);
|
||||
Fct4(B, C, D);
|
||||
Fct5(B, C, D);
|
||||
cout << "Voici la solution de cette fonction" << Fct6(B, C, D);
|
||||
cout << "\n";
|
||||
// cout << "Voici la solution de cette fonction (bis)" << Fct6Bis(B, C, D);
|
||||
}
|
||||
|
||||
void Fct1()
|
||||
{
|
||||
cout << "Bonjour" << endl;
|
||||
}
|
||||
|
||||
void Fct2(int &A)
|
||||
{
|
||||
cout << "Entrez la valeur de l'entier A : ";
|
||||
cin >> A;
|
||||
}
|
||||
|
||||
void Fct3(int A)
|
||||
{
|
||||
cout << "Le double de A vaut : " << (A * 2) << endl;
|
||||
}
|
||||
|
||||
void Fct4(float &B, char &C, string &D)
|
||||
{
|
||||
cout << "Entrez la valeur du float B : ";
|
||||
cin >> B;
|
||||
cout << "Entrez la valeur du char C : ";
|
||||
cin >> C;
|
||||
cout << "Entrez la valeur du string D : ";
|
||||
// cin.getline(D);
|
||||
cin >> D;
|
||||
}
|
||||
|
||||
void Fct5(float B, char C, string D)
|
||||
{
|
||||
cout << "La valeur de B vaut : " << B << endl;
|
||||
cout << "La valeur de C vaut : " << C << endl;
|
||||
cout << "La valeur de D vaut : " << D << endl;
|
||||
}
|
||||
|
||||
string Fct6(float B, char C, string D)
|
||||
{
|
||||
return " La valeur de B vaut : " + to_string(B) + ", " + "La valeur de C vaut : " + to_string(C) + ", " + "La valeur de D vaut : " + D + ".";
|
||||
}
|
||||
|
||||
function<ostream &(ostream &)> Fct6Bis(float B, char C, string D)
|
||||
{
|
||||
auto a = [](ostream &o) -> ostream &
|
||||
{
|
||||
return o << "HelloWorld";
|
||||
};
|
||||
|
||||
return a;
|
||||
}
|
||||
28
TP2/2.2.cpp
Normal file
28
TP2/2.2.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int pow(int base, int exp);
|
||||
|
||||
void main()
|
||||
{
|
||||
int P1, P2, P3;
|
||||
|
||||
P1 = pow(5, 3);
|
||||
P2 = pow(7, 9);
|
||||
P3 = pow(3, 21);
|
||||
|
||||
cout << "\nLa puissance P1 est = " << P1;
|
||||
cout << "\nLa puissance P2 est = " << P2;
|
||||
cout << "\nLa puissance P2 est = " << P2;
|
||||
}
|
||||
|
||||
int pow(int base, int exp)
|
||||
{
|
||||
int r = 1;
|
||||
while (exp-- > 0)
|
||||
{
|
||||
r *= base;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
52
TP2/2.3.cpp
Normal file
52
TP2/2.3.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int Nmax = 100;
|
||||
|
||||
void Saisie(int tab[], int &n, int &S, float &Moy);
|
||||
void Affichage(int tab[], int n, int S, float Moy);
|
||||
|
||||
void main()
|
||||
{
|
||||
int N, S;
|
||||
float Moy;
|
||||
int Tab[Nmax];
|
||||
|
||||
Saisie(Tab, N, S, Moy);
|
||||
Affichage(Tab, N, S, Moy);
|
||||
}
|
||||
|
||||
void Saisie(int tab[], int &n, int &S, float &Moy)
|
||||
{
|
||||
cout << "Entrez la taille du tableau : ";
|
||||
cin >> n;
|
||||
|
||||
if (n > Nmax)
|
||||
{
|
||||
n = Nmax;
|
||||
cout << "La taille maximale du tableau est de " << Nmax;
|
||||
}
|
||||
|
||||
S = 0;
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "tab[" << i << "]" << " = ";
|
||||
cin >> tab[i];
|
||||
S += tab[i];
|
||||
}
|
||||
|
||||
Moy = S / (float)n;
|
||||
}
|
||||
|
||||
void Affichage(int tab[], int n, int S, float Moy)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "tab[" << i << "]" << " = " << tab[i] << endl;
|
||||
}
|
||||
|
||||
cout << "Somme = " << S << endl;
|
||||
cout << "Moyenne = " << Moy << endl;
|
||||
}
|
||||
65
TP2/2.4.cpp
Normal file
65
TP2/2.4.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int Nmax = 10;
|
||||
|
||||
void Saisie(int tab[][Nmax], int &n1, int &n2, int &S, float &Moy);
|
||||
void Affichage(int tab[][Nmax], int n1, int n2, int S, float Moy);
|
||||
|
||||
void main()
|
||||
{
|
||||
int N1, N2, S;
|
||||
float Moy;
|
||||
int Tab[Nmax][Nmax];
|
||||
// i*n1 + j
|
||||
Saisie(Tab, N1, N2, S, Moy);
|
||||
Affichage(Tab, N1, N2, S, Moy);
|
||||
}
|
||||
|
||||
void Saisie(int tab[][Nmax], int &n1, int &n2, int &S, float &Moy)
|
||||
{
|
||||
cout << "Entrez le nombre de lignes : ";
|
||||
cin >> n1;
|
||||
cout << "Entrez le nombre de colonnes : ";
|
||||
cin >> n2;
|
||||
|
||||
if (n1 > Nmax)
|
||||
{
|
||||
n1 = Nmax;
|
||||
cout << "La taille maximale du tableau est de " << Nmax;
|
||||
}
|
||||
if (n2 > Nmax)
|
||||
{
|
||||
n2 = Nmax;
|
||||
cout << "La taille maximale du tableau est de " << Nmax;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n1; i++)
|
||||
{
|
||||
for (int j = 0; j < n2; j++)
|
||||
{
|
||||
cout << "tab[" << i << "]" << "[" << j << "]" << " = ";
|
||||
cin >> tab[i][j];
|
||||
S += tab[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
Moy = (float)S / (n1 + n2);
|
||||
}
|
||||
|
||||
void Affichage(int tab[][Nmax], int n1, int n2, int S, float Moy)
|
||||
{
|
||||
for (int i = 0; i < n1; i++)
|
||||
{
|
||||
for (int j = 0; j < n2; j++)
|
||||
{
|
||||
cout << tab[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
// cout << "tab[" << i << "]" << " = " << tab[i] << endl;
|
||||
}
|
||||
|
||||
cout << "Somme = " << S << endl;
|
||||
cout << "Moyenne = " << Moy << endl;
|
||||
}
|
||||
Reference in New Issue
Block a user