| Resolución
al primer parcial IC-98 (una de tantas) |
Problema 1
/* El problema pide que compare los strings que se reciben como
argumentos, por lo tanto NO DEBO modificarlos, solo compararlos
independientemente de mayúsculas y minúsculas */
int StriCmp(char Str1[], char Str2[])
{
int i = 0;
/* Como los strings terminan en 0, navego mientras los caracteres
sean iguales o alguno llegue al final del string */
while(toupper(Str1[i]) == toupper(Str2[i]) && Str1[i] != 0)
i++;
/* la diferencia entre los caracteres que me dieron la condicion de
salida del while me da directamente el resultado buscado */
return Str1[i] - Str2[i];
}
|
Problema 2
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* Tamaño del tablero */
#define SIZE 6
/* Prototipo de las Funciones */
void ColocaPrimerPieza(void);/* Función que coloca la primer pieza */
int ColocaPieza(int Valor); /* Función que coloca el caballo en su */
/* paso Valor (1,2,3...) */
void ImprimeTablero(void); /* Función que imprime el tablero */
int Tablero[SIZE][SIZE]; /* Matriz que hara las veces del tablero */
int PosX, /* Posición actual X del caballo */
PosY; /* Posición actual Y del caballo */
void main()
{
int Valor = 2;
randomize(); /* Inicializa el generador de números aleatorios */
ColocaPrimerPieza(); /* Manda a colocar la primer pieza -> pone un 1 */
/* en alguna posición del tablero, para utilizarla */
/* como pocisión inicial del caballo */
while(ColocaPieza(Valor))/* Mientras se pueda seguir colocando el caballo */
{ /* En piezas diferentes, lo hago, incrementando */
Valor++; /* el paso que sigue */
}
ImprimeTablero(); /* imprimo el tablero y el numero de pasos dados */
printf("Se colocaron %d piezas\n", Valor - 1);
}
/* Esta función coloca el primer caballo al azar dentro del tablero */
void ColocaPrimerPieza()
{
PosX = random(SIZE),
PosY = random(SIZE);
Tablero[PosX][PosY] = 1;
}
/* Esta función intenta que el caballo que esta en la posición PoxX, */
/* PosY de un salto mas, si puede hacerlo retorna 1, sino retorna 0 */
int ColocaPieza( int Valor)
{
int Candidatos[8][2];/* En esta matriz guardo las posiciones candidatas */
/* a ser elegida, tengo que llenarla, viendo cuales de */
/* las 8 estan dentro del tablero y desocupadas */
int Indice = 0;
int Eleccion; /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX + 1 < SIZE && PosY - 2 >=0 && Tablero[PosX+1][PosY-2] == 0)
{
Candidatos[Indice][0] = PosX + 1;
Candidatos[Indice][1] = PosY - 2;
Indice++;
} /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX + 2 < SIZE && PosY - 1 >=0 && Tablero[PosX+2][PosY-1] == 0)
{
Candidatos[Indice][0] = PosX + 2;
Candidatos[Indice][1] = PosY - 1;
Indice++;
} /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX + 2 < SIZE && PosY + 1 < SIZE && Tablero[PosX+2][PosY+1] == 0)
{
Candidatos[Indice][0] = PosX + 2;
Candidatos[Indice][1] = PosY + 1;
Indice++;
} /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX + 1 < SIZE && PosY + 2 < SIZE && Tablero[PosX+1][PosY+2] == 0)
{
Candidatos[Indice][0] = PosX + 1;
Candidatos[Indice][1] = PosY + 2;
Indice++;
} /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX - 1 >= 0 && PosY + 2 < SIZE && Tablero[PosX-1][PosY+2] == 0)
{
Candidatos[Indice][0] = PosX - 1;
Candidatos[Indice][1] = PosY + 2;
Indice++;
} /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX - 2 >= 0 && PosY + 1 < SIZE && Tablero[PosX-2][PosY+1] == 0)
{
Candidatos[Indice][0] = PosX - 2;
Candidatos[Indice][1] = PosY + 1;
Indice++;
} /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX - 2 >= 0 && PosY - 1 >=0 && Tablero[PosX-2][PosY-1] == 0)
{
Candidatos[Indice][0] = PosX - 2;
Candidatos[Indice][1] = PosY - 1;
Indice++;
} /* Si cae dentro del tablero y no eta ocupada, la agrego */
if(PosX - 1 >= 0 && PosY - 2 >=0 && Tablero[PosX-1][PosY-2] == 0)
{
Candidatos[Indice][0] = PosX - 1;
Candidatos[Indice][1] = PosY - 2;
Indice++;
}
if(Indice == 0) /* Si Indice es 0, no hay posiciones posibles */
return 0; /* por lo tanto retorno 0 */
Eleccion = random(Indice); /* Ahora eligo alguna entre las posibles */
PosX = Candidatos[Eleccion][0]; /* Actualiza la posicion X del caballo */
PosY = Candidatos[Eleccion][1]; /* Actualiza la posición Y del caballo */
Tablero[PosX][PosY] = Valor; /* Actualizo el tablero */
return 1; /* Retorno 1 indicando que se hizo un mov.*/
}
/* Función que imprime el tablero */
void ImprimeTablero(void)
{
int i, j;
for(i = 0; i < SIZE; i++)
{
for(j = 0; j < SIZE; j++)
{
printf("%2d ", Tablero[i][j]);
}
printf("\n");
}
printf("\n");
}
|