TP1
This commit is contained in:
110
TP1/1.2.cpp
Normal file
110
TP1/1.2.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int Nmax = 10;
|
||||
|
||||
void print_tab(int tab[], int n);
|
||||
|
||||
void print_help(int tab[], int n);
|
||||
|
||||
void insert_begin(int v, int tab[], int &n);
|
||||
|
||||
void insert_end(int v, int tab[], int &n);
|
||||
|
||||
void pop(int i, int tab[], int &n);
|
||||
|
||||
int main()
|
||||
{
|
||||
int tab[Nmax] = {5, 3, 11, 9, 2};
|
||||
int n = 5;
|
||||
int v;
|
||||
|
||||
char c;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
while (true)
|
||||
{
|
||||
print_help(tab, n);
|
||||
cout << "Votre choix : ";
|
||||
cin >> c;
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case 'a':
|
||||
if (n >= Nmax)
|
||||
break;
|
||||
cout << "Entrez la valeur à ajouter : ";
|
||||
cin >> v;
|
||||
insert_begin(v, tab, n);
|
||||
|
||||
break;
|
||||
case 'b':
|
||||
if (n >= Nmax)
|
||||
break;
|
||||
cout << "Entrez la valeur à ajouter : ";
|
||||
cin >> v;
|
||||
insert_end(v, tab, n);
|
||||
|
||||
break;
|
||||
|
||||
case 'c':
|
||||
if (n < 2)
|
||||
break;
|
||||
pop(1, tab, n);
|
||||
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_tab(int tab[], int n)
|
||||
{
|
||||
cout << "[";
|
||||
for (int i = 0; i < n - 1; i++)
|
||||
{
|
||||
cout << tab[i] << ", ";
|
||||
}
|
||||
if (n > 0)
|
||||
cout << tab[n - 1];
|
||||
cout << "]";
|
||||
}
|
||||
|
||||
void print_help(int tab[], int n)
|
||||
{
|
||||
cout << "Tableau ";
|
||||
print_tab(tab, n);
|
||||
cout << endl;
|
||||
cout << "a) Ajouter au début du tableau une valeur introduite par l'utilisateur\n";
|
||||
cout << "b) Ajouter à la fin du tableau une valeur introduite par l'utilisateur\n";
|
||||
cout << "c) Supprimer la valeur de l'élément à la deuxième position du tableau\n";
|
||||
cout << "d) Quitter\n";
|
||||
}
|
||||
|
||||
void insert_begin(int v, int tab[], int &n)
|
||||
{
|
||||
for (int i = n - 1; i >= 0; i--)
|
||||
{
|
||||
tab[i + 1] = tab[i];
|
||||
}
|
||||
tab[0] = v;
|
||||
n++;
|
||||
}
|
||||
|
||||
void insert_end(int v, int tab[], int &n)
|
||||
{
|
||||
tab[n++] = v;
|
||||
}
|
||||
|
||||
void pop(int i, int tab[], int &n)
|
||||
{
|
||||
for (; i < n - 1; i++)
|
||||
{
|
||||
tab[i] = tab[i + 1];
|
||||
}
|
||||
n--;
|
||||
}
|
||||
Reference in New Issue
Block a user