110 lines
1.6 KiB
C++
110 lines
1.6 KiB
C++
#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--;
|
|
} |