This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.c
482 lines (420 loc) · 12 KB
/
functions.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/**
* @file functions.c
*
* @brief Contains all the functions used.
*
* @author Teddy TOUSSAINT, 3TC, group 2
*
* @version 3.2
*
* @date Tuesday, January 19th 2016
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include "lib.h"
#include "functions.h"
#define PI 3.14159265
/**
* @fn void display(INST* root, int indent)
*
* @brief Displays a program.
*
* @param root The pointer towards the root instruction of the
* program to display.
* @param indent Number of spaces to put before the instruction,
* according to the number of nested 'REPEAT'.
*
* @warning 'indent' parameter must be initialized at 0. The
* program will increment it itself.
*/
void display(INST* root, int indent){
int i;
while (root != NULL){
if (1){ //TODO ... or not.
switch (root->key) {
case 0:
for(i = 0; i < indent; i++){
printf(" ");
}
printf("FORWARD %d\n", root->val);
break;
case 1:
for(i = 0; i < indent; i++){
printf(" ");
}
printf("LEFT %d\n", root->val);
break;
case 2:
for(i = 0; i < indent; i++){
printf(" ");
}
printf("RIGHT %d\n", root->val);
break;
case 3:
if(root->prog == NULL){
printf("ERROR: 'REPEAT' without parameter 'prog'.");
} else {
for(i = 0; i < indent; i++){
printf(" ");
}
printf("REPEAT %d\n", root->val);
indent++;
display(root->prog, indent);
indent--;
}
break;
}
root = root->next;
}
}
}
/**
* @fn void test_display()
*
* @brief Test the 'display' function.
*
* @note This function prints two results: what the 'display'
* function does, and what it should do. It is up to the user
* to see if the 'display' function passes the test or not,
* by comparing the two results.
*/
void test_display(){
PROG my_prog;
INST *inst1, *inst2, *inst3, *inst4, *inst5, *inst6;
INST *inst7, *inst8;
inst2 = createInst(RIGHT, 90, NULL);
inst3 = createInst(FORWARD, 10, NULL);
inst4 = createInst(LEFT, 90, NULL);
inst5 = createInst(RIGHT, 45, NULL);
inst6 = createInst(FORWARD, 50, NULL);
inst7 = createInst(FORWARD, 1, NULL);
inst8 = createInst(LEFT, 30, NULL);
addInst(inst3, inst2);
addInst(inst4, inst2);
inst1 = createInst(REPEAT, 5, inst2);
addInst(inst7, inst1);
addInst(inst1, inst8);
my_prog = createInst(REPEAT, 2, inst8);
addInst(inst5, my_prog);
addInst(inst6, my_prog);
printf("What the 'display' function does:\n");
display(my_prog, 0);
printf("\nWhat it should do:\n");
printf("REPEAT 2\n");
printf(" LEFT 30\n");
printf(" REPEAT 5\n");
printf(" RIGHT 90\n");
printf(" FORWARD 10\n");
printf(" LEFT 90\n");
printf(" FORWARD 1\n");
printf("RIGHT 45\n");
printf("FORWARD 50\n");
freeProg(my_prog);
}
/**
* @fn void addInst(INST* inst, INST* prog)
*
* @brief Adds an instruction to a LOGO program.
*
* @param inst The instruction to add.
* @param prog The program to add the instruction to.
*/
void addInst(INST* inst, INST* prog){
assert((prog != NULL)&&(inst != NULL));
while (prog->next != NULL){
prog = prog->next;
}
prog->next = inst;
}
/**
* @fn int test_addInst()
*
* @brief Test the 'addInst' function.
*
* @note 'createInst' function is tested before 'addInst' function
* can pass its test.
*
* @return 1 if the function passed the test.
* @return 0 if the function failed it.
*/
int test_addInst(){
assert(test_createInst());
PROG prog = createInst(FORWARD, 50, NULL);
INST* inst = createInst(LEFT, 90, NULL);
addInst(inst, prog);
if(prog->next == inst) {
freeProg(prog);
return 1;
}
else {
printf("\nERROR: addInst failed its test.\n");
freeProg(prog);
return 0;
}
}
/**
* @fn void freeProg(INST* root)
*
* @brief Frees an allocated memory.
*
* @param root The pointer which the memory to free was
* allocated for.
*/
void freeProg(INST* root){
INST* inst;
while(root != NULL) {
if(root->prog != NULL) {
freeProg(root->prog);
}
inst = root->next;
free(root);
root = inst;
}
}
/**
* @fn INST* createInst(instType key, int val, INST* prog)
*
* @brief Creates an instruction.
*
* @param key The key number corresponding to the type of the
* instruction to create.
* @param val The value of the instruction to create.
* @param prog The pointer towards the program to repeat.
*
* @warning Parameter 'prog' is different from 'NULL' if and only
* if parameter 'key' equals '3', which refers to the 'REPEAT'
* instruction.
* @warning This function allocates memory to the returned pointer.
* Function 'freeProg(INST* root)' must be used on the returned
* variable to free the allocated memory.
*
* @return The pointer towards the instruction created.
*/
INST* createInst(instType key, int val, INST* prog){
assert( !( ((key == 3)&&(prog == NULL))
|| ((key != 3)&&(prog != NULL)) ) );
INST* my_prog = (INST*) malloc(sizeof(INST));
my_prog->key = key;
my_prog->val = val;
my_prog->prog = prog;
my_prog->next = NULL;
return my_prog;
}
/**
* @fn int test_createInst()
*
* @brief Test the 'createInst' function.
*
* @return 1 if the function passed the test.
* @return 0 if the function failed it.
*/
int test_createInst(){
int res = 0;
INST* my_FORWARD = createInst(FORWARD, 10, NULL);
PROG my_REPEAT = createInst(REPEAT, 3, my_FORWARD);
if( (my_REPEAT->key == 3)
&& (my_REPEAT->val == 3)
&& (my_REPEAT->prog == my_FORWARD)
&& (my_FORWARD->key == 0)
&& (my_FORWARD->val == 10) ) {
res = 1;
}
else printf("\nERROR: createInst failed its test.\n");
freeProg(my_REPEAT);
return res;
}
/**
* @fn int test_ALL()
*
* @brief Tests the following functions : display, addInst,
* addInstR and createInst.
*
* @note The function 'addInst' can pass its test if an only if
* the function 'createInst' has passed its own test.
*
* @return 1 if all the functions passed the test.
* @return 0 if at least one of them failed it.
*/
int test_ALL(){
test_display();
return test_addInst();
}
/**
* @fn void translateToSVG(INST* root)
*
* @brief Translates a LOGO program from a "LOGO" file to an SVG
* program in a "SVG.svg" file.
*
* @param root The first instruction of the program to translate.
*/
void translateToSVG(INST* root){
FILE* svg;
svg = fopen("SVG.svg", "w");
writeHeader(svg);
double x1, y1;
x1 = 500;
y1 = 250;
int angle = 0;
double *px1, *py1;
px1 = &x1;
py1 = &y1;
int* pangle;
pangle = ∠
char* colour = "purple";
int frir; // "f-r-i-r" = "first repeat is read".
int* pfrir = &frir;
printf("Welcome, Human.\n\n");
printf("I am a C program and my purpose ");
printf("is to translate 'LOGO' files\ninto 'SVG' files. ");
printf("I have already read what is inside the 'LOGO'\n");
printf("file, and I am going to create a displayable ");
printf("'SVG.svg' file.\n\n");
//This does not work, unfortunately.
/*
printf("First of all, choose the colour to display : ");
printf("red (r), black (bk), blue (be), yellow (y), ");
printf("green (g), orange (o), purple (p) : ");
scanf("%s", &colour);
printf("%s", colour);
if(strcmp(colour, "r") == 0) colour = "red";
if(strcmp(colour, "bk") == 0) colour = "black";
if(strcmp(colour, "be") == 0) colour = "blue";
if(strcmp(colour, "y") == 0) colour = "yellow";
if(strcmp(colour, "g") == 0) colour = "green";
if(strcmp(colour, "o") == 0) colour = "orange";
if(strcmp(colour, "p") == 0) colour = "purple";
*/
recTranslateToSVG(root, svg, px1, py1, pangle, colour, pfrir);
printf(" ...\n\n");
//Test : add a signature to the SVG file.
/**/
//T
fprintf(svg, "<line x1=\"800\" y1=\"450\" ");
fprintf(svg, "x2=\"810\" y2=\"450\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"805\" y1=\"450\" ");
fprintf(svg, "x2=\"805\" y2=\"465\" ");
fprintf(svg, "stroke=\"black\" />\n");
//E
fprintf(svg, "<line x1=\"812\" y1=\"450\" ");
fprintf(svg, "x2=\"822\" y2=\"450\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"812\" y1=\"450\" ");
fprintf(svg, "x2=\"812\" y2=\"465\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"812\" y1=\"457\" ");
fprintf(svg, "x2=\"822\" y2=\"457\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"812\" y1=\"465\" ");
fprintf(svg, "x2=\"822\" y2=\"465\" ");
fprintf(svg, "stroke=\"black\" />\n");
//D
fprintf(svg, "<line x1=\"824\" y1=\"450\" ");
fprintf(svg, "x2=\"824\" y2=\"465\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"824\" y1=\"450\" ");
fprintf(svg, "x2=\"831\" y2=\"452\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"831\" y1=\"452\" ");
fprintf(svg, "x2=\"834\" y2=\"455\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"834\" y1=\"455\" ");
fprintf(svg, "x2=\"834\" y2=\"460\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"834\" y1=\"460\" ");
fprintf(svg, "x2=\"831\" y2=\"463\" ");
fprintf(svg, "stroke=\"black\" />\n");
fprintf(svg, "<line x1=\"831\" y1=\"463\" ");
fprintf(svg, "x2=\"824\" y2=\"465\" ");
fprintf(svg, "stroke=\"black\" />\n");
/**/
fprintf(svg, "</svg>\n");
printf("Done. File 'SVG.svg' created.\n");
printf("You can now display the SVG program by typing the ");
printf("command\n'eog SVG.svg'.\n\n");
printf("Thank you for using this program.\n");
fclose(svg);
}
/**
* @fn void writeHeader(FILE* svg)
*
* @brief Writes the svg header in the svg file.
*
* @param svg The SVG file to write in.
*/
void writeHeader(FILE* svg) {
fprintf(svg, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
fprintf(svg, "<svg xmlns=\"http://www.w3.org/2000/svg\" ");
fprintf(svg, "version=\"1.1\" width=\"1000\" height=\"500\">");
fprintf(svg, "\n");
fprintf(svg, "<title> Mon exemple LOGO</title>\n");
fprintf(svg, "<desc>Affichage d'une figure LOGO.</desc>\n");
}
/**
* @fn void recTranslateToSVG(INST* root, FILE* svg, double* px1,
* double* py1, int* pangle, char* colour, int* pfrir)
*
* @brief The recursive version of 'translateToSVG'.
*
* @param root The pointer towards the first instruction of the
* program to translate.
* @param svg The pointer towards the SVG file to write in.
* @param px1 The pointer towards the horizontal coordinate.
* @param py1 The pointer towards the vertical coordinate.
* @param pangle The pointer towards the angle or the drawing arrow.
* @param colour The colour we draw with.
* @param pfrir The pointer towards the integer 'first repeat
* is read', which has value '1' if it is, or '0' if it is not.
*/
void recTranslateToSVG(INST* root, FILE* svg, double* px1, double* py1, int* pangle, char* colour, int* pfrir) {
double x2, y2;
int i, j;
INST* first_repeat;
char* c[8];
c[0] = "red";
c[1] = "black";
c[2] = "blue";
c[3] = "yellow";
c[4] = "green";
c[5] = "orange";
c[6] = "purple";
c[7] = "cyan";
while(root != NULL) {
switch((int)root->key) {
case 0: //FORWARD
x2 = *px1 + (root->val) * cos((*pangle)*(PI/180));
y2 = *py1 - (root->val) * sin((*pangle)*(PI/180));
fprintf(svg, "<line x1=\"%f\" y1=\"%f\" ", *px1, *py1);
fprintf(svg, "x2=\"%f\" y2=\"%f\" ", x2, y2);
fprintf(svg, "stroke=\"%s\" />\n", colour);
*px1 = x2;
*py1 = y2;
break;
case 1: //LEFT
*pangle = (*pangle + (root->val)) % 360;
break;
case 2: //RIGHT
*pangle = (*pangle - (root->val)) % 360;
if(*pangle < 0) *pangle = 360 + (*pangle);
break;
case 3: //REPEAT
if(*pfrir != 1) {
first_repeat = root;
j = root->val;
*pfrir = 1;
}
for(i = 0; i < root->val; i++) {
if(first_repeat == root){
colour = c[j % 8];
j--;
}
recTranslateToSVG(root->prog, svg, px1, py1, pangle, colour, pfrir);
}
break;
}
root = root->next;
}
}