commit 64b4c3074b1d60a222bb351ae043fa7ee501c918
parent 391f887a2b495c1852912897ae8a1e693117df25
Author: John Charron <rm_rf_windows@yahoo.fr>
Date: Tue, 16 Nov 2010 19:39:22 +0100
Ajout d'un fichier c, tentative de faire l'exo 4 (help Georges) (jc)
Diffstat:
1 file changed, 148 insertions(+), 0 deletions(-)
diff --git a/exo4-codage-lisp/couples.c b/exo4-codage-lisp/couples.c
@@ -0,0 +1,148 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int pair(int x){
+ return (!(x % 2));
+}
+
+int code_couples_very_slow(int _x, int _y){
+ int x, y;
+ x = y = 0;
+ int code = 0;
+ char direction = 'r'; // 'l' pour "left-down", 'r' pour "right-up"
+ printf("In the 'couples' function\n");
+ sleep(1);
+ while(((x != _x) || (y != _y))){
+ if((y == 0) && (pair(x))){
+ printf("IF1:\n");
+ sleep(1);
+ x++;
+ code++;
+ direction = 'l';
+ printf("IF N° 1, x = %d, y = %d, code = %d\n", x, y, code);
+ sleep(1);
+ }
+ else if((x == 0) && (!pair(y))){
+ printf("IF2:\n");
+ sleep(1);
+ y++;
+ code++;
+ direction = 'r';
+ printf("IF N° 2, x = %d, y = %d, code = %d\n", x, y, code);
+ sleep(1);
+ }
+ else if((y == 0) && (direction == 'l')){
+ printf("IF3:\n");
+ sleep(1);
+ while((x != 0) && ((x != _x) || (y != _y))){
+ printf("WHILE3:\n");
+ sleep(1);
+ x--;
+ y++;
+ code++;
+ printf("IF N° 3, x = %d, y = %d, code = %d\n", x, y, code);
+ sleep(1);
+ }
+ }
+ else if((x == 0) && (direction == 'r')){
+ printf("IF4:\n");
+ sleep(1);
+ while((y != 0) && ((x != _x) || (y != _y))){
+ printf("WHILE4:\n");
+ sleep(1);
+ x++;
+ y--;
+ code++;
+ printf("IF N° 4, x = %d, y = %d, code = %d\n", x, y, code);
+ sleep(1);
+ }
+ }
+ }
+ return code;
+}
+
+int code_couples_slow(int _x, int _y){
+ int x, y;
+ x = y = 0;
+ int code = 0;
+ char direction = 'r'; // 'l' pour "left-down", 'r' pour "right-up"
+ while(((x != _x) || (y != _y))){
+ if((y == 0) && (pair(x))){
+ x++;
+ code++;
+ direction = 'l';
+ }
+ else if((x == 0) && (!pair(y))){
+ y++;
+ code++;
+ direction = 'r';
+ }
+ else if((y == 0) && (direction == 'l')){
+ while((x != 0) && ((x != _x) || (y != _y))){
+ x--;
+ y++;
+ code++;
+ }
+ }
+ else if((x == 0) && (direction == 'r')){
+ while((y != 0) && ((x != _x) || (y != _y))){
+ x++;
+ y--;
+ code++;
+ }
+ }
+ }
+ return code;
+}
+
+
+int code_couples_faster(int _x, int _y){
+ int x, y;
+ x = y = 0;
+ int code = 0;
+ int incr_int = 0;
+ char direction = 'r'; // 'l' pour "left-down", 'r' pour "right-up"
+ while(((x != _x) || (y != _y))){
+ if((y == 0) && (pair(x))){
+ x++;
+ code++;
+ incr_int++;
+ direction = 'l';
+ }
+ else if((x == 0) && (!pair(y))){
+ y++;
+ code++;
+ incr_int++;
+ direction = 'r';
+ }
+ else if((y == 0) && (direction == 'l')){
+ while((x != 0) && ((x != _x) || (y != _y))){
+ x--;
+ y++;
+ code++;
+ }
+ }
+ else if((x == 0) && (direction == 'r')){
+ while((y != 0) && ((x != _x) || (y != _y))){
+ x++;
+ y--;
+ code++;
+ }
+ }
+ }
+ return code;
+}
+
+
+
+
+int main(int argc, char **argv, char **envp){
+ printf("hello\n");
+ int code = 0;
+ int x, y, z;
+ x = 11;
+ y = 5;
+ //z = -2;
+ code = code_couples_slow(x,y);
+ printf("Le code du couple (%d, %d) est %d\n", x, y, code);
+}