Título: Programa de gráficos de tortuga
Publicado por: abra2004 en 30 Octubre 2012, 06:00 am
Hola, gracias por admitirme en el foro, tengo el siguente problema: Estoy haciendo un ejercicio del libro de Deitel "Como programar en C/C++" El ejercicio trata de hacer gráficos de tortuga, el problema lo tengo al hacer una función para introducir los comandos que se van a realizar, los comandos van en forma de número y el problema que tengo es que al introducir el comando '5' debo poner 5 una coma y el numero de movimientos, es decir que este comando sirve para mover la tortuga x movimientos por ejemplo si pongo "5,9" la tortuga se moverá 9 casillas, mi problema es que no entiendo como declarar el arreglo o como hacer que simplemente ignore la coma, ¿alguien sabe como podría solucionarlo? Gracias. void guarda_comandos(int comands[COMANDOS]){ int i, x; for (x = 0; i != 9 && x < COMANDOS; x++) { printf("Introduzca el próximo comando: "); comands[x] = i; } for (x = 0; x < COMANDOS && comands[x] != 0; x++) { } }
Título: Re: Programa de gráficos de tortuga
Publicado por: abra2004 en 31 Octubre 2012, 04:33 am
Bueno gracias por la ayuda :¬¬ , de cualquier manera encontré el código como viene en el libro de soluciones aunque realmente no lo comprendo y estoy un poco frustrado porque mi compilador me da errores al ejecutarlo, utilizo "xcode". #include <stdio.h> #define TRUE 1 #define FALSE 0 #define MAX 100 /* the maximum number of commands */ /* function prototypes */ void getCommands( int commands[][ 2 ] ); int turnRight( int d ); int turnLeft( int d ); void movePen( int down, int a[][ 50 ], int dir, int dist ); void printArray( int a[][ 50 ] ); int main(int argc, const char * argv[]) { //Declaraciones int floor[ 50 ][ 50 ] = { 0 }; int penDown = FALSE; int command; int direction = 0; int commandArray[MAX][ 2 ] = {0}; int distance; int count = 0; //Fin Declaraciones getCommands( commandArray ); command = commandArray[ count ][ 0 ]; while(command!=9){ switch (command) { case 2: penDown = TRUE; break; /* exit switch */ case 3: direction = turnRight( direction ); break; /* exit switch */ case 4: direction = turnLeft( direction ); break; /* exit switch */ case 5: distance = commandArray[count][1]; movePen ( penDown , floor, direction , distance ); break; /* exit switch */ case 6: printf( "\nThe drawing is:\n\n" ); printArray ( floor ); break; /* exit switch */ } /* end switch */ command = commandArray[ ++count ][ 0 ]; } /* end while */ return 0; }/**************-FIN-DE-MAIN-***************/ void getCommands( int commands[][2]) { int i; /* counter */ int tempCommand; /* temporary command holder */ printf( "Enter command ( 9 to end input ): "); scanf( "%d", &tempCommand ); /* recieve commands until -9 or 100 commands perform action */ for(i=0;tempCommand!=9&&i<MAX;i++){ commands[ i ][ 0 ] = tempCommand; /* ignore comma after 5 is entered */ if ( tempCommand == 5 ) { scanf( ",%d", &commands [ i ][ 1 ] ); } /* end if */ printf( "Enter command ( 9 to end input ): " ); scanf( "%d", &tempCommand ); } commands[ i ][ 0 ] = 9; }/* FIN DE GET COMMANDS */ int turnRight( int d ) { return ++d > 3 ? 0 : d; } int turnLeft( int d ) { return --d < 0 ? 3 : d; } void movePen( int down, int a[][ 50 ], int dir, int dist ) { int i; int j; static int xPos = 0; static int yPos = 0; /* determine which way to move pen */ switch(dir){ case 0: for (j=1;j<=dist&&yPos+j<50;j++){ if (down) { a[xPos][yPos+j]=1; } /* end if */ } yPos += j - 1; break; /* exit switch */ case 1: /* move down */ /* move dist spaces or until edge of floor */ for (i=1;i<=dist&&xPos+i<50;i++){ if (down) { a[xPos+i][yPos] =1; } } xPos += i - 1; break; case 2: /* move to the left */ for (j=1;j<=dist&&yPos-j>=0;j++){ if (down){ a[xPos][yPos-j]=1; } /* end if */ } yPos -= j - 1; break; /* exit switch */ case 3: /* move up */ /* move dist spaces or until edge of floor */ for (i=1;i<=dist&&xPos-i>=0;i++){ if (down){ a[xPos-i][yPos]=1; } /* end if */ } xPos -= i - 1; break; /* exit switch */ } } void printArray( int a[][ 50 ] ) { int i; /* counter */ int j; /* counter */ /* loop through array */ for ( i = 0; i < 50; i++ ) { /* loop through array */ for ( j = 0; j < 50; j++ ) { putchar( a [ i ][ j ] ? '*' : ' ' ); } /* end for */ } /* end for */ }
El código está simplemente copiado y como dije anteriormente me gustaría saber como es que esto.. if ( tempCommand == 5 ) { scanf( ",%d", &commands [ i ][ 1 ] ); } /* end if */
ignora la coma pues lee la variable tempCommand pero ésta fue previamente escaneada y ahí luego luego viene mi problema al meter el 5 y la coma, si alguien me pudiera ayudar se lo agradecería para avanzar en mi aprendizaje.
Título: Re: Programa de gráficos de tortuga
Publicado por: flony en 31 Octubre 2012, 05:10 am
se tan poco de c pero cuando vi tu code original me di cuenta que un for no dejaba que funcione el otro....te iba a poner un ejemplo...pero bue esta bueno que lo leas y trates de entenderlo...me pongo a leer esta solucion y te digo
Título: Re: Programa de gráficos de tortuga
Publicado por: flony en 31 Octubre 2012, 14:43 pm
lo que pasa es que no se entiende si lo sacas de contexto for(i=0;tempCommand!=9&&i<MAX;i++){ commands[ i ][ 0 ] = tempCommand; /* ignore comma after 5 is entered */ if ( tempCommand == 5 ) { scanf( ",%d", &commands[ i ][ 1 ] ); } /* end if */ printf( "Enter command ( 9 to end input ): " ); scanf( "%d", &tempCommand ); } se lee asi, mas o menos, (haga un bucle iniciando en i=o ; con limite en la variable tempCommand distinta de 9 y con i menor a MAX ; si no entra en el limite sume uno a i ) mientras no cumpla esos limites hara lo siguiente la variable tempCommand se guarda en commands[ i ][ 0 ] (fijate que cada bucle hara que i suba en una. el bucle no termina alli dice si tempCommand es igual a 5 ingrese manualmente un decimal entero para que ocupe el lugar de la variable commands[ i ][ 0 ] o sea si es 5 meta el dato a mano que el bucle no lo hace, en cada bucle va a aparecer en pantalla si mete 9 sale del bucle, El tema de la coma la soluciona porque la variable commands[ i ][ 1 ] es una matriz bidimensional
Título: Re: Programa de gráficos de tortuga
Publicado por: xiruko en 31 Octubre 2012, 14:59 pm
has probado a introducir el movimiento asi? scanf("%d %c %d", &a , &aux , &b );
en este caso, en aux tendras la ','. luego podrias comprobarlo por si el usuario ha cometido un error, algo asi: if (aux != ',') printf("ERROR: Introduce un formato valido -> a,b\n");
un saludo!
|