Advertisement
Guest User

Jeffrey

a guest
Nov 10th, 2009
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. // Main.cs created with MonoDevelop
  2. // Jeffrey
  3. //
  4. // To change standard headers go to Edit->Preferences->Coding->Standard Headers
  5. //
  6. using System;
  7.  
  8.  
  9. namespace MatrizCaracol
  10. {
  11.     class MainClass
  12.     {
  13.         public static void Main(string[] args)
  14.         {
  15.             int rows, cuadrant;
  16.             Cartesian cartesian;
  17.            
  18.             Console.Write("Please enter the number of rows: ");
  19.             rows = Convert.ToInt32(Console.ReadLine());
  20.             Console.Write("Please enter the number of the quadrant you want to look: ");
  21.             cuadrant = Convert.ToInt32(Console.ReadLine());
  22.            
  23.            
  24.             cartesian = new Cartesian(rows);
  25.             cartesian.MakeSnailMatrix();
  26.             cartesian.PrintMatrix((Quadrant)cuadrant);
  27.             Console.Read();                        
  28.         }      
  29.     }
  30.    
  31.     public enum Quadrant
  32.     {
  33.         I=1,
  34.         II,
  35.         III,
  36.         IV
  37.     }
  38.    
  39.     public class Cartesian
  40.     {
  41.         int[,] matrix;
  42.         Quadrant[,] cuadrant;
  43.         int content, rows;
  44.        
  45.        
  46.         public Cartesian(int newRows)
  47.         {
  48.             matrix = new int[newRows,newRows];
  49.             cuadrant = new Quadrant[newRows, newRows];
  50.             this.rows = newRows;
  51.             Resize(newRows);
  52.             this.content = 0;          
  53.         }
  54.        
  55.         public void Resize(int newRows)
  56.         {
  57.             for(int i=0; i < newRows; i++)
  58.             {
  59.                 for(int j=0; j < newRows; j++)
  60.                 {          
  61.                     this.matrix[i,j] = 0;
  62.                     if(i < newRows/2 && j < newRows/2)
  63.                         this.cuadrant[i,j] =  Quadrant.II;
  64.                     else if(i < newRows/2 && j >= newRows/2)
  65.                         this.cuadrant[i,j] = Quadrant.I;
  66.                     else if(i >= newRows/2 && j < newRows/2)
  67.                         this.cuadrant[i,j] = Quadrant.III;
  68.                     else
  69.                         this.cuadrant[i,j] = Quadrant.IV;
  70.                 }
  71.             }
  72.         }
  73.        
  74.        
  75.         public int Content
  76.         {
  77.             get {return content;}
  78.             set {this.content = value;}
  79.         }
  80.        
  81.         public void MakeSnailMatrix()
  82.         {
  83.             int result = rows * rows;
  84.            
  85.                 for(int i = 0; i < rows; i++)
  86.                 {
  87.                 // Left
  88.                 for(int j = rows-1; j >=0; j--)
  89.                     {
  90.                         if(matrix[rows-(i+1),j] == 0)
  91.                             matrix[rows-(i+1),j] = result--;
  92.                     }
  93.                    
  94.                 // Up
  95.                     for(int j= rows - 1; j >= 0; j--)
  96.                     {
  97.                         if(matrix[j,i] == 0)
  98.                             matrix[j,i] = result--;
  99.                     }
  100.                    
  101.                 // Right
  102.                     for(int j=0; j < rows; j++)
  103.                     {
  104.                         if(matrix[i,j] == 0)
  105.                             matrix[i,j] = result--;
  106.                     }
  107.                    
  108.                 // Down
  109.                     for(int j=0; j < rows; j++)
  110.                     {
  111.                         if(matrix[j,rows-(i+1)] == 0)
  112.                             matrix[j,rows-(i+1)] = result--;
  113.                     }
  114.                    
  115.                 }
  116.             }
  117.        
  118.         public void PrintMatrix(Quadrant cdt)
  119.         {
  120.             for(int i=0; i< rows; i++)
  121.             {
  122.                 for(int j=0; j < rows; j++)
  123.                 {
  124.                     if(this.cuadrant[i,j] == cdt)
  125.                         Console.Write(String.Format("{0:d2} ", matrix[i,j]));
  126.                 }
  127.                 Console.WriteLine();
  128.             }
  129.                
  130.         }
  131.     }
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement