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
|
/* vc++ debugger message
First-chance exception at 0x1026f8bc (msvcr90d.dll) in as.exe: 0xC0000005: Access violation reading location 0x00000001.
Unhandled exception at 0x1026f8bc (msvcr90d.dll) in as.exe: 0xC0000005: Access violation reading location 0x00000001.
*/
#include <iostream>
#include <iomanip>//setw ,setprecision;
#include <ctime>
#include <cstdlib>//rand
using namespace std;
#ifndef print
#define print cout <<
#endif
#ifndef L
#define L <<endl
#endif
template <class T>
T getMAX( T m[],int s){
T _max = m[1];
for(int i = 0;i<s;i++){
if(m[i] > _max){
_max = m[i];
}
}
return _max;
}
template <class T>
T getMIN(T m[],int s){
T _min = m[1];
for( int i = 0; i < s; i++ ){
if(m[i] < _min){
_min = m[i];
}
}
return _min;
}
template <class T>
void sortArray(T m[],const int _size){
T Array_min_element = getMIN(m,_size);
T Array_max_element = getMAX(m,_size);
int *tmparray = new int[_size];
int tmparray_count = 0;
tmparray[tmparray_count] = Array_min_element;
for(T i = Array_min_element;i < Array_max_element;i++){
for(int n = 0;n<_size;n++){
if(i == Array_min_element){
continue;
}
if(m[n] == i){
++tmparray_count;
tmparray[tmparray_count] = m[n];
}
}
}
tmparray[tmparray_count+1] = Array_max_element;
for(int i = 0;i<_size;i++){
m[i] = tmparray[i];
}
delete [] tmparray;
}
void cArray_sort(char* m[],const int size){
int *tmp = new int[size];
char *ctmp[sizeof(tmp)/sizeof(int)] = {0};
int tmp_counter = 0;
int ctmp_counter = 0;
for(int i =0;i<size;i++){
tmp[tmp_counter]= strlen(m[i]);
++tmp_counter;
}
sortArray(tmp,size);
for(int n = 0;n < size;n++){
for(int r = 0; r < size; r++){
if(strlen(m[r]) == tmp[n]){
ctmp[ctmp_counter] = m[r];
++ctmp_counter;
}
}
}
for(int b = 0;b < size;b++){
m[b] = ctmp[b];
}
delete [] tmp;
}
///////////////////////////////////////////////////////////////////////////////////////////
int main(){
//test function
char *n[7] = {"google","cplusplus","cprogramming","barnameh","program","cpp","c"};
cArray_sort(n,7);
for(int i = 0;i < 7;i++){
print n[i] L; //sorted array;
}
return 0;
}
|