-
Notifications
You must be signed in to change notification settings - Fork 0
/
metodidaaggiungere.txt
82 lines (75 loc) · 2.03 KB
/
metodidaaggiungere.txt
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
/* Nel Main */
#include <time.h> //libreria in aggiunta
int main(){
ReadFile(fp);
sleep(1);
PrintStruct(datas);
printf("Now applying Merge-BinaryInsertionSort...\n");
srand(time(NULL));
int count = rand() % 3+1;//per non avere ripetizioni.
printf(" Tipo su cui ordino : %d", count);
Ordina(datas, count);
PrintStruct(datas);
return 0;
}
/* Prototipi Funzioni Ordinamenti (mylib.h)*/
void Ordina(struct v_ptr * datas, int count);
void Ordina_Integer(struct v_ptr * integer);
void Ordina_String(struct v_ptr * string);
void Ordina_Float(struct v_ptr * floater);
/* Funzioni complete : da inserire in mylib.c*/
void Ordina(struct v_ptr * datas, int count){
if(count == 1){
Ordina_Integer(datas);
}
else if(count == 2){
Ordina_String(datas);
}
else if(count == 3){
Ordina_Float(datas);
}
}
void Ordina_Integer(struct v_ptr * integer){
int i, key, j;
for (i = 1; i < MAX_LINES; i++){
key = *((int*)integer[i].field2);
j = i - 1;
while (j >= 0 && *((int*)integer[j].field2) > key)
{
*((int*)integer[j+1].field2) = *((int*)integer[j].field2);
j = j - 1;
}
*((int*)integer[j + 1].field2) = key;
}
}
/*Ordina characters*/
void Ordina_String(struct v_ptr * string){
int i, j;
char * key = malloc(MAX_LINES * sizeof(char*));
for (i = 1; i < MAX_LINES; i++){
strcpy(key,(char*)string[i].field1);
j = i - 1;
while (j >= 0 && strcmp((char*)string[j].field1,key)>0)
{
strcpy((char*)string[j+1].field1,(char*)string[j].field1);
//((char*)integer.arr)[j + 1] = ((char*)integer.arr)[j];
j = j - 1;
}
strcpy((char*)string[j+1].field1, key);
//((char*)integer.arr)[j + 1] = key;
}
}
void Ordina_Float(struct v_ptr * floater){
int i,j;
float key;
for (i = 0; i < MAX_LINES; i++){
key = *((double*)floater[i].field3);
j = i - 1;
while (j >= 0 && *((double*)floater[j].field3) > key)
{
*((double*)floater[j + 1].field3) = *((double*)floater[j].field3);
j = j - 1;
}
*((double*)floater[j + 1].field3) = key;
}
}