| Ver tema anterior :: Ver siguiente tema | | Autor | Mensaje |
|---|
Archivel
Registrado: 07 Ago 2007 Mensajes: 4
| Publicado: 14/11/2007 1:05 pm | | | Título: Comparador |
| Hola amigos, estoy escribiendo un programa que compara dos arrays de caracteres y muestra lo siguiente:
! si el elemento está en ambos y en la misma posicion # si esta en ambos pero no en la misma posicion ? si no esta en ambos
Por ejemplo si jugadaprov= c1, c3, c5, c7 y combinicial= c1, c3, c6, c5 el programa devolveria !!?#
Lo he hecho con el siguiente código y mi pregunta es si habria alguna manera de optimizarlo para ahorrar lineas de código.
Un saludo!
| Código: | for (i=0; i<4; i++) { if ((strcmp(jugadaprov[i], combinicial[0])==0) && (i==0)) { printf("!"); } else if ((strcmp(jugadaprov[i], combinicial[0])==0) && (i!=0)) { printf("#"); } else if ((strcmp(jugadaprov[i], combinicial[1])==0) && (i==1)) { printf("!"); } else if ((strcmp(jugadaprov[i], combinicial[1])==0) && (i!=1)) { printf("#"); } else if ((strcmp(jugadaprov[i], combinicial[2])==0) && (i==2)) { printf("!"); } else if ((strcmp(jugadaprov[i], combinicial[2])==0) && (i!=2)) { printf("#"); } else if ((strcmp(jugadaprov[i], combinicial[3])==0) && (i==3)) { printf("!"); } else if ((strcmp(jugadaprov[i], combinicial[3])==0) && (i!=3)) { printf("#"); } else printf("?"); } |
|
| | Volver arriba | |  | digies
Registrado: 18 Nov 2005 Mensajes: 325 Ubicación: Cono Sur
| Publicado: 14/11/2007 2:28 pm | | | Título: |
| Para ahorrar líneas de código, elimina todas las llaves que utilizas en los if-else if-else puesto que no es necesario usarlas porque en ellas solo se ejecuta una sola instrucción.
| Código: | for(i=0; i<4; i++) { if ((strcmp(jugadaprov[i], combinicial[0])==0) && (i==0)) printf("!"); else if ((strcmp(jugadaprov[i], combinicial[0])==0) && (i!=0)) printf("#"); else if ((strcmp(jugadaprov[i], combinicial[1])==0) && (i==1)) printf("!"); else if ((strcmp(jugadaprov[i], combinicial[1])==0) && (i!=1)) printf("#"); else if ((strcmp(jugadaprov[i], combinicial[2])==0) && (i==2)) printf("!"); else if ((strcmp(jugadaprov[i], combinicial[2])==0) && (i!=2)) printf("#"); else if ((strcmp(jugadaprov[i], combinicial[3])==0) && (i==3)) printf("!"); else if ((strcmp(jugadaprov[i], combinicial[3])==0) && (i!=3)) printf("#"); else printf("?"); }
|
Un saludo |
| | Volver arriba | |  | digies
Registrado: 18 Nov 2005 Mensajes: 325 Ubicación: Cono Sur
| Publicado: 14/11/2007 2:41 pm | | | Título: |
| | Código: | for(i=0; i<4; i++) { if (((strcmp(jugadaprov[i], combinicial[0])==0) && (i==0)) || ((strcmp(jugadaprov[i], combinicial[1])==0) && (i==1)) || ((strcmp(jugadaprov[i], combinicial[2])==0) && (i==2)) || ((strcmp(jugadaprov[i], combinicial[3])==0) && (i==3))) printf("!"); else if (((strcmp(jugadaprov[i], combinicial[0])==0) && (i!=0)) || ((strcmp(jugadaprov[i], combinicial[1])==0) && (i!=1)) || ((strcmp(jugadaprov[i], combinicial[2])==0) && (i!=2)) || ((strcmp(jugadaprov[i], combinicial[3])==0) && (i!=3))) printf("#"); else printf("?"); }
|
Un saludo |
| | Volver arriba | |  | | rir3760

Registrado: 01 Oct 2004 Mensajes: 3777 Ubicación: Mexico
| Publicado: 14/11/2007 3:55 pm | | | Título: |
| La forma mas sencilla es utilizando dos bucles for:
| Código: | for (i = 0; i < 4; i++){ for (j = 0; j < 4; j++) if (strcmp(jugadaprov[i], combinicial[j]) == 0){ putchar((i == j) ? '!' : '#'); break; } if (j == 4) putchar('?'); } |
Un saludo _________________ The capacity to learn is a gift; The ability to learn is a skill; The willingness to learn is a choice. -- Rebec of Ginaz |
| | Volver arriba | |  | digies
Registrado: 18 Nov 2005 Mensajes: 325 Ubicación: Cono Sur
| Publicado: 14/11/2007 5:07 pm | | | Título: |
| | rir3760 escribió: | | La forma mas sencilla es utilizando dos bucles for: | La verdad que esa forma se me pasó por alto 
Un saludo |
| | Volver arriba | |  | | |
| No puede crear mensajes No puede responder temas No puede editar sus mensajes No puede borrar sus mensajes No puede votar en encuestas
|
|
| |