Ejercicio resuelto C

Iniciado por the_patox, 3 Enero 2014, 23:46 PM

0 Miembros y 1 Visitante están viendo este tema.

the_patox

Buenas tardes, llevo bastante tiempo como lector de este foro, el cual me ha enseñado desde hace años sobre informática, seguridad y programación  :P

Hace unas semanas me pidieron hacer una tarea para un ramo llamado fundamentos de programación, solo me complicó entenderlo un poco, hasta tuve que dibujar en papel :xD

Adjuntaré el pdf con los detalles, algunos casos de prueba y mi código ya que me gustaría recibir consejos en cuanto a optimización y mejoras del mismo (ya sé que está medio enredado, pero cumple con lo pedido :xD )

Y obviamente que sea resuelto por ustedes, que tienen más experiencia y así también podré apreciar otros tipos de solución ya que me pidieron usar solo ansi C, arreglos bidimensionales y funciones.

Ejercicio:
tarea.pdf

Entrada:

Citar4
3
1 2 3
4 5 6
7 8 9
2 1 2
4 1 2 3 4
4
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 6
2 1 2
2 3 4
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
2 3 4
2 1 3
4 1 3 2 4
6
1 2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9
1 2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9
2 4 1
2 2 3
1 1

Salida:

Citar
9 8 7
6 5 4
3 2 1
6 6 5 4
3 2 1 9
8 7 6 5
4 3 2 1
7 6 5 4 3
2 8 3 7 7
6 9 4 8 2
1 1 5 9 6
5 4 3 2 1
4 7 1 4 7 1
5 2 8 5 2 2
6 1 3 4 1 3
7 9 6 7 9 4
8 8 5 2 8 5
9 3 6 9 3 6

Código:

#include <stdio.h>
#define MAX 100

void LeerMatriz(int matriz[MAX][MAX],int,int,int);
void Operacion1(int matriz[MAX][MAX],int i,int N,int Anillo);
void Operacion2(int matriz[MAX][MAX],int i,int N,int Anillo);
void Operacion3(int matriz[MAX][MAX],int i,int N,int Anillo);
void Operacion4(int matriz[MAX][MAX],int i,int N,int Anillo);
void ImprimirMatriz(int matriz[MAX][MAX],int,int,int);
void Swap(int *,int *);

int main()
{
   int matriz[MAX][MAX];
   int i,j,a,b,c,casos;
   int N,T,C,anillo,Anillo;

   scanf("%d",&casos); //Cantidad de casos de prueba

   for(c=1;c<=casos;c++)
   {
       scanf("%d",&N); //Dimension de la matriz correspondiente

       if((N==1)||(N==0)); anillo=1;
       if(N%2==0) anillo=N/2; else anillo=(N+1)/2; //Calcular los anillos de la matriz

       LeerMatriz(matriz,i,j,N);

       for(a=0;a<anillo;a++) //Recorrer cada anillo de la matriz
       {
           scanf("%d",&T); //Cantidad de operaciones a realizar al anillo respectivo
           Anillo=a+1;
           for(b=1;b<=T;b++)
           {
               scanf("%d",&C); //Especificar la operacion a realizar
               switch (C)
               {
                   case 1: // VERTICAL
                       Operacion1(matriz,i,N,Anillo);
                       break;
                   case 2: // HORIZONTAL " -> "
                       Operacion2(matriz,i,N,Anillo);
                       break;
                   case 3: // DIAGONAL " \ "
                       Operacion3(matriz,i,N,Anillo);
                       break;
                   case 4: // DIAGONAL " / "
                       Operacion4(matriz,i,N,Anillo);
                       break;
               }
           }
       }
       ImprimirMatriz(matriz,i,j,N);
   }
}

void LeerMatriz(int matriz[MAX][MAX],int i,int j, int N)
{
   for(i=0;i<N;i++)
   {
       for(j=0;j<N;j++)
       {
           scanf("%d",&matriz[i][j]);
       }
   }
}

void ImprimirMatriz(int matriz[MAX][MAX],int i,int j,int N)
{
   for(i=0;i<N;i++)
   {
       printf("\n");
       for(j=0;j<N;j++)
       {
           printf(" %d",matriz[i][j]);
       }
   }
}

void Swap(int * a, int * b) //Intercambiar variables
{
   int aux;
   aux=*a;
   *a=*b;
   *b=aux;
}

/* Desde aqui jugando con los dibujitos a prueba y error, ya me entenderan xD */

void Operacion1(int matriz[MAX][MAX],int i,int N,int Anillo) //VERTICAL
{
   for(i=Anillo-1;i<(N/2);i++)
   {
       Swap(&matriz[i][Anillo-1],&matriz[N-i-1][Anillo-1]);
   }
   for(i=Anillo;i<(N-Anillo);i++)
   {
       Swap(&matriz[Anillo-1][i],&matriz[N-Anillo][i]);
   }
   for(i=Anillo-1;i<(N/2);i++)
   {
       Swap(&matriz[i][N-Anillo],&matriz[N-i-1][N-Anillo]);
   }
}

void Operacion2(int matriz[MAX][MAX],int i,int N,int Anillo) //HORIZONTAL
{
   for(i=Anillo-1;i<(N/2);i++)
   {
       Swap(&matriz[Anillo-1][i],&matriz[Anillo-1][N-i-1]);
   }
   for(i=Anillo;i<(N-Anillo);i++)
   {
       Swap(&matriz[i][Anillo-1],&matriz[i][N-Anillo]);
   }
   for(i=Anillo-1;i<(N/2);i++)
   {
       Swap(&matriz[N-Anillo][i],&matriz[N-Anillo][N-i-1]);
   }
}

void Operacion3(int matriz[MAX][MAX],int i,int N,int Anillo) //DIAGONAL " \ "
{
   for(i=Anillo;i<(N-Anillo);i++)
   {
       Swap(&matriz[Anillo-1][i],&matriz[i][Anillo-1]);
       Swap(&matriz[N-Anillo][i],&matriz[i][N-Anillo]);
   }
   Swap(&matriz[N-Anillo][Anillo-1],&matriz[Anillo-1][N-Anillo]);
}

void Operacion4(int matriz[MAX][MAX],int i,int N,int Anillo) //DIAGONAL " / "
{
   for(i=Anillo-1;i<N-Anillo;i++)
   {
       Swap(&matriz[Anillo-1][i],&matriz[((N-Anillo)-i)+Anillo-1][N-Anillo]);
       Swap(&matriz[i][Anillo-1],&matriz[N-Anillo][((N-Anillo)-i)+Anillo-1]);
   }
   Swap(&matriz[Anillo-1][Anillo-1],&matriz[N-Anillo][N-Anillo]);
}


Para ingresar los casos de prueba deben guardar la entrada en un archivo de texto, luego ubicarse en la consola e ingresar:
Citarprograma.exe<entrada.txt>salida.txt

El archivo salida debe ser idéntico al mostrado arriba.

Saludos y cualquier duda pregunten :)