View difference between Paste ID: htuSYWRa and MygVqGQR
SHOW: | | - or go back to the newest paste.
1
using System;
2
using System.Text;
3
4
class OperatorOverloading
5
{
6
7
    public class Matrix
8
    {
9
        private int rows;
10
        private int cols;
11
        private int[,] matrix;
12
        
13
        public Matrix(int x, int y)
14
        {
15
            rows = x;
16
            cols = y;
17
            matrix = new int[rows,cols];
18
        }
19
20
        public int this[int x, int y]
21
        {
22
            get
23
            {
24
                return matrix[x, y];
25
            }
26
            set
27
            {
28
                matrix[x, y] = value;
29
            }
30
        }
31
32-
        public int numRows()
32+
        public int getRows()
33
        {
34
            return rows;
35
        }
36
37-
        public int numCols()
37+
        public int getCols()
38
        {
39
            return cols;
40
        }
41
42
        public static Matrix operator +( Matrix first, Matrix second) {
43-
            if (first.numRows() != second.numRows() || first.numCols() != second.numCols()) {
43+
            if (first.getRows() != second.getRows() || first.getCols() != second.getCols()) {
44
                throw new Exception("Matrices must have the same dimensions!");
45
            }
46
47-
            Matrix result = new Matrix(first.numRows(), first.numCols());
47+
            Matrix result = new Matrix(first.getRows(), first.getCols());
48
        
49
            for (int i = 0; i < first.matrix.GetLength(0); i++)
50
            {
51
                for (int j = 0; j < second.matrix.GetLength(1); j++)
52
                {
53
                    result[i, j] = first[i, j] + second[i, j];
54
                }
55
            }
56
            return result;
57
        }
58
59
        public static Matrix operator -(Matrix first, Matrix second)
60
        {
61-
            if (first.numRows() != second.numRows() || first.numCols() != second.numCols())
61+
            if (first.getRows() != second.getRows() || first.getCols() != second.getCols())
62
            {
63
                throw new Exception("Matrices must have the same dimensions!");
64
            }
65
66-
            Matrix result = new Matrix(first.numRows(), first.numCols());
66+
            Matrix result = new Matrix(first.getRows(), first.getCols());
67
68
            for (int i = 0; i < first.matrix.GetLength(0); i++)
69
            {
70
                for (int j = 0; j < second.matrix.GetLength(1); j++)
71
                {
72
                    result[i, j] = first[i, j] - second[i, j];
73
                }
74
            }
75
            return result;
76
        }
77
78
        public static Matrix operator *(Matrix first, Matrix second)
79
        {
80-
            if (first.numRows() != second.numRows() || first.numCols() != second.numCols())
80+
            if (first.getRows() != second.getRows() || first.getCols() != second.getCols())
81
            {
82
                throw new Exception("Matrices must have the same dimensions!");
83
            }
84
85-
            Matrix result = new Matrix(first.numRows(), first.numCols());
85+
            Matrix result = new Matrix(first.getRows(), first.getCols());
86
87
            for (int i = 0; i < first.matrix.GetLength(0); i++)
88
            {
89
                for (int j = 0; j < second.matrix.GetLength(1); j++)
90
                {
91
                    result[i, j] = first[i, j] * second[i, j];
92
                }
93
            }
94
            return result;
95
        }
96
97
        public override string ToString()
98
        {
99
            StringBuilder matrixToString = new StringBuilder();
100
101
            for (int i = 0; i < rows; i++)
102
			{
103
			    for (int j = 0; j < cols; j++)
104
			    {
105
			        matrixToString.Append(String.Format("{0} ", matrix[i,j]));
106
			    }
107
                matrixToString.Append("\n");
108
			}
109
110
            return matrixToString.ToString();
111
        }
112
113
    }
114
115
    static void Main(string[] args)
116
    {
117
        Matrix a = new Matrix(4, 4);
118
        Matrix b = new Matrix(4, 4);
119
        Matrix result;
120
121
        //tests - entering some random-ish data
122
        int counter = 1;
123-
        for (int i = 0; i < a.numCols(); i++)
123+
        for (int i = 0; i < a.getCols(); i++)
124
        {
125-
            for (int j = 0; j < a.numRows(); j++)
125+
            for (int j = 0; j < a.getRows(); j++)
126
            {
127
                a[i, j] = counter;
128
                b[i, j] = j + counter;
129
                counter++;
130
            }
131
        }
132
133
        //printing the test matrices
134
        Console.WriteLine(a);
135
        Console.WriteLine(b);
136
137
        result = a + b;
138
        Console.WriteLine("Addition:");
139
        Console.WriteLine(result);
140
141
        result = a - b;
142
        Console.WriteLine("Subtraction:");
143
        Console.WriteLine(result);
144
145
        result = a * b;
146
        Console.WriteLine("Multiplication:");
147
        Console.WriteLine(result);
148
    }
149
}