1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
// school.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
#define SORT_ASC 1
#define SORT_DESC 2
int _tmain(int argc, _TCHAR* argv[]) {
/* On choisit le nombre de valeurs dans n; */
int n = 1;
std::cin >> n;
std::cout << "Remplissage du tableau avec n=" << n << " valeurs..." << std::endl;
/* On declare et rempli le tableau cf. fonction fill(int *t, int s); */
int *table = NULL;
table = (int *)malloc(n*sizeof(int));
fill(table,n);
/* et on trie ... */
sort(table,n,SORT_DESC);
show(table,n);
_getch(); // Usage de _getch(); a la place de getch(); , meme fonction.
}
void show(int *t,int s) {
std::cout << "Show apres tri..."<<std::endl;
for(int i=0;i<s;i=i+1) {
std::cout << "valeur a n=" << i <<" est " << t[i] << std::endl; // affichage des valeurs
}
}
void fill(int *t,int s) {
/* boucle qui remplie chaque case de notre tableau avec la fonction rand(); */
for(int i=0;i<s;i=i+1) {
t[i] = rand();
std::cout << "valeur a n=" << i <<" est " << t[i] << std::endl; // affichage des valeurs
}
}
void sort(int *t, int s, int d) {
/*
* mon idée:
* On va faire une boucle qui se repete s fois (c'est a dire le nombre de valeurs dans notre tableau)
* pour chaque iteration, on recupere le maximum du tableau (cf. get_max(int *t, int s);)
* et en fonction du tri (ASC ou DESC) on va placer cette valeur a la fin ou au debut de notre tableau
*/
std::cout << "Tri ordre - " << ((d == SORT_ASC) ? "croissant" : "decroissant") << std::endl;
int *table_cpy = NULL;
table_cpy = (int *)malloc(sizeof(int)*s*sizeof(int)*s);
table_cpy = t;
int max = get_max(table_cpy,s);
int min = table_cpy[get_min(table_cpy,s, max)]-1;
//std::cout << "minimum "<<min<<std::endl;
if(d == SORT_DESC) {
for(int i = (s-1); i >= 0; i--) {
max = get_max(table_cpy,s);
t[i] = table_cpy[max];
std::cout << "------------- iteration "<<i<<std::endl;
std::cout << "table_cpy("<<max<<"):"<<table_cpy[max]<<" - t("<<i<<"):"<<t[i]<<std::endl;
//std::cout << "max ("<<cpos<<"):" <<t[cpos]<<std::endl;
table_cpy[max] = min; // on met une valeur minimale comme ca on a plus besoin de la prendre en compte lors de la prochaine iteration
//del(table_cpy,(i+1),max);
//std::cout << "table_cpy("<<max<<"):"<<table_cpy[max]<<" - t("<<i<<"):"<<t[i]<<std::endl;
}
}/*
if(d == SORT_ASC) {
//int cpos = s;
for(int i = 0; i < s; i = i + 1) {
min = table_cpy[get_min(table_cpy,s, max)];
t[i] = table_cpy[min];
//std::cout << "max ("<<cpos<<"):" <<t[cpos]<<std::endl;
table_cpy[min] = table_cpy[max]+1;
std::cout << "------------- iteration "<<i<<std::endl;
std::cout << "valeur minimale ("<<min<<") - t:"<<t[i]<<std::endl;
}
}*/
}
int get_max(int *t, int s) {
int w[] = {t[0],0};
//std::cout << "-------------------- Recherche de la valeur maximale d'un tableau ----------" << std::endl;
for(int i = 0;i<s;i=i+1) {
//std::cout << "---- CMAX : "<<w[0]<<" - curr : "<<t[i]<<std::endl;
if(t[i] > w[0]) { w[0] = t[i]; w[1] = i; }
}
//std::cout << "the max is now << "<<w[1]<<std::endl;
return w[1];
}
int get_min(int *t, int s, int m) {
int w2[] = {t[m],m};
for(int i = 0;i<s;i=i+1) {
if(t[i] < w2[0]) { w2[0] = t[i]; w2[1] = i; }
}
return w2[1];
} |