Перегрузка оператора умножения для матрицы - C#
Формулировка задачи:
Помогите пожалуйста! Нужно на этой неделе сдать работу, никак не могу разобраться. Вот имею даный код, но он чего-то не работает, посмотрите пожалуста!
Листинг программы
- using System;
- namespace Lab8
- {
- class Matrix
- {
- public int lines;
- public int columns;
- public int[,] matrix;
- public int this[int i]
- {
- get
- {
- if (i == 1) return lines;
- else if (i == 2) return columns;
- else return 0;
- }
- set
- {
- if (i == 1) lines = value;
- else if (i == 2) columns = value;
- }
- }
- public Matrix(int lines, int columns)
- {
- Random R = new Random();
- matrix = new int[lines, columns];
- for (int i = 0; i < lines; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- matrix[i, j] = R.Next(9);
- }
- }
- }
- public static Matrix operator *(Matrix Matrix1, Matrix Matrix2)
- {
- Matrix Matrix3 = new Matrix(Matrix1.lines, Matrix1.columns);
- Matrix3.lines = Matrix1.lines;
- Matrix3.columns = Matrix1.columns;
- for (int i = 0; i < Matrix3.lines; i++)
- {
- for (int j = 0; j < Matrix3.columns; j++)
- {
- Matrix3.matrix[i, j] = 0;
- }
- }
- for (int i = 0; i < Matrix1.lines; i++)
- {
- for (int j = 0; j < Matrix2.columns; j++)
- {
- for (int k = 0; k < Matrix2.lines; k++)
- {
- Matrix3.matrix[i, j] += Matrix1.matrix[i, k] * Matrix2.matrix[k, j];
- }
- }
- }
- return Matrix3;
- }
- public override string ToString()
- {
- string st ="";
- for (int i = 0; i < lines; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- st+=(this.matrix[i, j])+" ";
- }
- Console.WriteLine(" ");
- }
- return st;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter the number of lines in matrix: ");
- int lines = Convert.ToInt16(Console.ReadLine());
- Console.WriteLine("Enter the number of columns in matrix: ");
- int columns = Convert.ToInt16(Console.ReadLine());
- Matrix NewMatrix1 = new Matrix(lines, columns);
- Matrix NewMatrix2 = new Matrix(lines, columns);
- Console.WriteLine(NewMatrix1);
- Console.WriteLine(NewMatrix2);
- Console.WriteLine(NewMatrix1 * NewMatrix2);
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Перегрузка оператора умножения для матрицы»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = @"qwajkctyfgqazkc";
- string pattern = @"a\Skc";//
- Console.WriteLine("Совпадений: " + Regex.Matches(input,pattern,RegexOptions.IgnoreCase).Count);
- Console.ReadKey();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д