Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main.cs created with MonoDevelop
- // Jeffrey
- //
- // To change standard headers go to Edit->Preferences->Coding->Standard Headers
- //
- using System;
- namespace MatrizCaracol
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- int rows, cuadrant;
- Cartesian cartesian;
- Console.Write("Please enter the number of rows: ");
- rows = Convert.ToInt32(Console.ReadLine());
- Console.Write("Please enter the number of the quadrant you want to look: ");
- cuadrant = Convert.ToInt32(Console.ReadLine());
- cartesian = new Cartesian(rows);
- cartesian.MakeSnailMatrix();
- cartesian.PrintMatrix((Quadrant)cuadrant);
- Console.Read();
- }
- }
- public enum Quadrant
- {
- I=1,
- II,
- III,
- IV
- }
- public class Cartesian
- {
- int[,] matrix;
- Quadrant[,] cuadrant;
- int content, rows;
- public Cartesian(int newRows)
- {
- matrix = new int[newRows,newRows];
- cuadrant = new Quadrant[newRows, newRows];
- this.rows = newRows;
- Resize(newRows);
- this.content = 0;
- }
- public void Resize(int newRows)
- {
- for(int i=0; i < newRows; i++)
- {
- for(int j=0; j < newRows; j++)
- {
- this.matrix[i,j] = 0;
- if(i < newRows/2 && j < newRows/2)
- this.cuadrant[i,j] = Quadrant.II;
- else if(i < newRows/2 && j >= newRows/2)
- this.cuadrant[i,j] = Quadrant.I;
- else if(i >= newRows/2 && j < newRows/2)
- this.cuadrant[i,j] = Quadrant.III;
- else
- this.cuadrant[i,j] = Quadrant.IV;
- }
- }
- }
- public int Content
- {
- get {return content;}
- set {this.content = value;}
- }
- public void MakeSnailMatrix()
- {
- int result = rows * rows;
- for(int i = 0; i < rows; i++)
- {
- // Left
- for(int j = rows-1; j >=0; j--)
- {
- if(matrix[rows-(i+1),j] == 0)
- matrix[rows-(i+1),j] = result--;
- }
- // Up
- for(int j= rows - 1; j >= 0; j--)
- {
- if(matrix[j,i] == 0)
- matrix[j,i] = result--;
- }
- // Right
- for(int j=0; j < rows; j++)
- {
- if(matrix[i,j] == 0)
- matrix[i,j] = result--;
- }
- // Down
- for(int j=0; j < rows; j++)
- {
- if(matrix[j,rows-(i+1)] == 0)
- matrix[j,rows-(i+1)] = result--;
- }
- }
- }
- public void PrintMatrix(Quadrant cdt)
- {
- for(int i=0; i< rows; i++)
- {
- for(int j=0; j < rows; j++)
- {
- if(this.cuadrant[i,j] == cdt)
- Console.Write(String.Format("{0:d2} ", matrix[i,j]));
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement