-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
205 lines (179 loc) · 4.4 KB
/
main.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <mem.h>
#define LINE_TO_SORT "sample text"
#include "tpifpcii.h"
#include "bubble_sort.h"
#include "cocktail_shaker_sort.h"
#include "insertion_sort.h"
#include "selection_sort.h"
#include "comb_sort.h"
typedef void (*tpf_sort)(char*, int, tpifpcii, tpifpcii);
#define ALG_LIST\
ALG(bubble_sort)\
ALG(cocktail_shaker_sort)\
ALG(insertion_sort)\
ALG(selection_sort)\
ALG(comb_sort)\
#define ALG(alg) alg_id_##alg,
enum ALG_IDS
{
ALG_LIST
};
#undef ALG
struct ALG_INFO
{
const char* name;
const tpf_sort pf;
};
#define ALG(alg) {#alg, alg},
struct ALG_INFO alg_infos[] =
{
ALG_LIST
};
#undef ALG
const unsigned int alg_num = sizeof(alg_infos)/sizeof(struct ALG_INFO);
int cmp = 0;
int swp = 0;
int alg = 0;
int reverse_sort = 0;
int show_cmp = 1;
int show_swp = 1;
#define SWAP(a,b) a^=b; b^=a; a^=b;
int order_is_broken(char* s, int i, int j)
{
printf("%s\n", s);
if (i==j)
{
printf ("hm... i==j ?\n");
return 0;
}
cmp++;
if (reverse_sort? s[i] < s[j]: s[i] > s[j])
{
printf("%*c%*c\n", i+1, '#', j-i, '#');
return 1;
}
printf("%*c%*c\n", i+1, '|', j-i, '|');
return 0;
}
int swap_items(char* s, int i, int j)
{
printf("%s\n", s);
printf("%*c%*c\n", i+1, '>', j-i, '<');
SWAP(s[i], s[j]);
swp++;
return 0;
}
int order_is_broken_silent(char* s, int i, int j)
{
cmp++;
if (reverse_sort? s[i] < s[j]: s[i] > s[j])
{
return 1;
}
return 0;
}
int swap_items_silent(char* s, int i, int j)
{
SWAP(s[i], s[j]);
swp++;
return 0;
}
void legend()
{
printf("-------------------\n");
printf(" || - in order \n");
printf(" ## - NOT in order \n");
printf(" >< - swap \n");
printf("-------------------\n");
}
void print_buf(char* s)
{
int i;
printf("%s\n", s);
for (i=0; s[i]; i++) {printf("-");} printf("\n");
}
void results(char* s)
{
printf("cmp = %*d, ", 4, cmp);
printf("swp = %*d, ", 4, swp);
printf("alg = %s\n", alg_infos[alg].name);
;
}
int main()
{
char s[81], ss[81];
tpf_sort pf_srt;
tpifpcii pf_brk = order_is_broken;
tpifpcii pf_swp = swap_items;
int sz, i;
for (sz=0; 0!=(ss[sz]=LINE_TO_SORT[sz]); sz++);
while (1)
{
for (sz=0; (s[sz]=ss[sz])>=' '; sz++);
s[sz]=0;
for (i=0; i<alg_num; i++)
printf("%d - %s\n", i+1, alg_infos[i].name);
printf(
"\n\
0 - exit program\n\
-1 - change line in buffer\n\
-2 - toggle order (now: %s)\n\
-3 - toggle comparisons (now: %s)\n\
-4 - toggle swaps (now: %s)\n\
-5 - test all algorithms (silent)\n\
Which algorithm to run? ",
reverse_sort?"Descending":"Ascending",
show_cmp?"Visible":"Hidden",
show_swp?"Visible":"Hidden"
);
scanf("%d", &alg);
switch (alg)
{
case -1:
printf("New line:\n");
fgets(ss, sizeof(ss), stdin);
fgets(ss, sizeof(ss), stdin);
break;
case -2:
reverse_sort ^= 1;
break;
case -3:
show_cmp ^= 1;
pf_brk = show_cmp?order_is_broken:order_is_broken_silent;
break;
case -4:
show_swp ^= 1;
pf_swp = show_swp?swap_items:swap_items_silent;
break;
case -5:
pf_brk = order_is_broken_silent;
pf_swp = swap_items_silent;
for (alg=0; alg<alg_num; alg++)
{
for (sz=0; (s[sz]=ss[sz])>=' '; sz++);
s[sz]=0;
pf_srt = alg_infos[alg].pf;
pf_srt(s, sz, pf_brk, pf_swp);
results(s);
cmp = swp = 0;
}
pf_brk = show_cmp?order_is_broken:order_is_broken_silent;
pf_swp = show_swp?swap_items:swap_items_silent;
break;
default:
printf("\n");
if (alg<=0 || alg>alg_num) return 0;
alg--;
pf_srt = alg_infos[alg].pf;
legend();
print_buf(s);
pf_srt(s, sz, pf_brk, pf_swp);
print_buf(s);
results(s);
cmp = swp = 0;
}
printf ("\n");
}
return 0;
}