using System;
using System.Collections.Generic;
using System.Text;
namespace MagicSquare
{
internal class MagicSquare
{
/// <summary>
/// Вътрешни характеристики
/// </summary>
// масив за елементи на квадрата
public int[,] squareMatrix;
// размер на квадрата
public byte size;
/// <summary>
/// Конструктор за нов празен квадрат
/// </summary>
/// <param name="lng"></param>
public MagicSquare(byte lng)
{
if (lng > 25)
{
lng = 25;
}
else
{
this.size = lng;
this.squareMatrix = new int[lng,lng];
}
}
/// <summary>
/// метод за въвеждане на всички елементи на масива от потребител
/// </summary>
public void SetElements()
{
int newElement;
Random random = new Random();
for (int i = 0; i < this.size; i++)
{
for (int j = 0; j < this.size; j++)
{
Console.Write("Enter element for row {0}, column {1} of the magic square: ",i+1,j+1);
try
{
newElement = int.Parse(Console.ReadLine());
}
catch (Exception e)
{
// ако стойността въведена от потребителя не е съвместима с програмата- замести с произволно число от 1 до 99
newElement = random.Next(1, 99);
}
this.squareMatrix[i,j]= newElement;
}
}
}
/// <summary>
/// отпечатай елементите на квадрата в матричен вид
/// </summary>
public void PrintSquare()
{
Console.WriteLine("\n Square matrix:\n");
for (int i = 0; i < this.size; i++)
{
for (int j = 0; j < this.size; j++)
{
Console.Write("{0, 6} \t",this.squareMatrix[i,j]);
}
Console.Write("\n");
}
}
/// <summary>
/// провери дали дадения квадрат е магически
/// </summary>
public void CheckMagic()
{
int constSum = 0;
int tempSum=0;
//проверка по редове
for (int i = 0; i < this.size; i++)
{
tempSum = 0;
for (int j = 0; j < this.size; j++)
{
tempSum = tempSum + this.squareMatrix[i, j];
}
if (constSum == 0)
{
constSum = tempSum;
}
if (constSum != tempSum)
{
Console.WriteLine("\n This is not a magic square. \n");
return;
}
}
//проверка по колони
for (int i = 0; i < this.size; i++)
{
tempSum = 0;
for (int j = 0; j < this.size; j++)
{
tempSum = tempSum + this.squareMatrix[j, i];
}
if (constSum == 0)
{
constSum = tempSum;
}
if (constSum != tempSum)
{
Console.WriteLine("\n This is not a magic square. \n");
return;
}
}
//проверка на главен диагонал
tempSum = 0;
for (int i = 0; i < this.size; i++)
{
tempSum += this.squareMatrix[i, i];
}
if (constSum == 0)
{
constSum = tempSum;
}
if (constSum != tempSum)
{
Console.WriteLine("\n This is not a magic square. \n");
return;
}
//провекра на обратен диагонал
tempSum = 0;
for (int i = 0; i < this.size; i++)
{
tempSum += this.squareMatrix[i, this.size - i-1];
}
if (constSum == 0)
{
constSum = tempSum;
}
if (constSum != tempSum)
{
Console.WriteLine("\n This is not a magic square. \n");
return;
}
// отпечатай вълшебната сума
Console.WriteLine("\n This is a magic square with magic sum of {0, 12}. \n",constSum);
return;
}
}
}
using System;
namespace MagicSquare
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Welcome to Magic Square checker. Please state the size of the squere (3-25): ");
byte mySize;
try
{
mySize = byte.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("\n\n Your input has one or more invalid characters. Size set to 3 units.");
mySize = 3;
}
if (mySize > 25)
{
mySize = 25;
Console.WriteLine("\n\n Your input exceeds the specified parameters and has been reduced to 25 units.");
}
if (mySize < 3)
{
mySize = 3;
Console.WriteLine("\n\n Your input is below the specified parameters. Size set to 3 units.");
}
MagicSquare msq1 = new MagicSquare(mySize);
msq1.SetElements();
msq1.PrintSquare();
msq1.CheckMagic();
Console.ReadKey(true);
}
}
}
Регистрирани потребители: Google [Bot]