Загрузка данных


using System;

class Program
{
    static Random rnd = new Random();

    static void Main()
    {
        Console.WriteLine("ЗАДАЧА 1. Вариант 22");

        double a = D("a = ");
        double q = D("q = ");
        int N = I("N = ", 1, 100);

        double S = Sg(a, q, N);

        Console.WriteLine("Сумма = " + S);

        Console.WriteLine();
        Console.WriteLine("ЗАДАЧА 2. Матрица B = A * F");

        int n = I("n = ", 1, 100);
        int m = I("m = ", 1, 100);

        int[,] f =
        {
            { 1, 0, 1 },
            { 0, 1, 0 },
            { 1, 0, 1 }
        };

        int k = f.GetLength(0);

        if (n < k || m < k)
        {
            Console.WriteLine("Ошибка: n и m должны быть >= " + k);
            Console.ReadKey();
            return;
        }

        int mn = I("a = ");
        int mx;

        do
        {
            mx = I("b = ");
            if (mx < mn) Console.WriteLine("b должно быть >= a");
        }
        while (mx < mn);

        int[,] A = new int[n, m];

        int v = I("Ввод A: 1 - вручную, 2 - случайно: ", 1, 2);

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                A[i, j] = v == 1 ? I("A[" + i + "," + j + "] = ", mn, mx)
                                  : rnd.Next(mn, mx + 1);

        int[,] B = Cb(A, f);

        Console.WriteLine();
        Console.WriteLine("Матрица A:");
        Pr(A);

        Console.WriteLine();
        Console.WriteLine("Матрица F:");
        Pr(f);

        Console.WriteLine();
        Console.WriteLine("Матрица B:");
        Pr(B);

        Console.ReadKey();
    }

    static double Sg(double a, double q, int N)
    {
        if (N == 1) return a;
        return a + q * Sg(a, q, N - 1);
    }

    static int[,] Cb(int[,] A, int[,] F)
    {
        int n = A.GetLength(0);
        int m = A.GetLength(1);
        int k = F.GetLength(0);

        int[,] B = new int[n - k + 1, m - k + 1];

        for (int i = 0; i <= n - k; i++)
            for (int j = 0; j <= m - k; j++)
            {
                int s = 0;

                for (int p = 0; p < k; p++)
                    for (int q = 0; q < k; q++)
                        s += A[i + p, j + q] * F[p, q];

                B[i, j] = s;
            }

        return B;
    }

    static int I(string s)
    {
        int x;

        while (true)
        {
            Console.Write(s);

            if (int.TryParse(Console.ReadLine(), out x))
                return x;

            Console.WriteLine("Ошибка ввода.");
        }
    }

    static int I(string s, int mn, int mx)
    {
        int x;

        while (true)
        {
            x = I(s);

            if (x >= mn && x <= mx)
                return x;

            Console.WriteLine("Введите число от " + mn + " до " + mx);
        }
    }

    static double D(string s)
    {
        double x;

        while (true)
        {
            Console.Write(s);

            if (double.TryParse(Console.ReadLine(), out x))
                return x;

            Console.WriteLine("Ошибка ввода.");
        }
    }

    static void Pr(int[,] x)
    {
        for (int i = 0; i < x.GetLength(0); i++)
        {
            for (int j = 0; j < x.GetLength(1); j++)
                Console.Write("{0,5}", x[i, j]);

            Console.WriteLine();
        }
    }
}