-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
279 lines (237 loc) · 14.3 KB
/
Copy pathmain.java
File metadata and controls
279 lines (237 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Добро пожаловать! Что вы хотите сделать? Выберете число:");
System.out.println("1. Создать комплексное число");
System.out.println("2. Сложить два комплексных числа");
System.out.println("3. Создать матрицу");
System.out.println("4. Сложить две матрицы");
System.out.println("5. Умножить две матрицы");
System.out.println("6. Транспонировать матрицу");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Введите действительную часть комплексного числа:");
double real = scanner.nextDouble();
System.out.println("Введите мнимую часть комплексного числа:");
double imaginary = scanner.nextDouble();
ComplexNumber num = new ComplexNumber(real, imaginary);
System.out.println("Создано комплексное число: " + num);
break;
case 2:
System.out.println("Введите действительную часть первого комплексного числа:");
double real1 = scanner.nextDouble();
System.out.println("Введите мнимую часть первого комплексного числа:");
double imaginary1 = scanner.nextDouble();
ComplexNumber num1 = new ComplexNumber(real1, imaginary1);
System.out.println("Введите действительную часть второго комплексного числа:");
double real2 = scanner.nextDouble();
System.out.println("Введите мнимую часть второго комплексного числа:");
double imaginary2 = scanner.nextDouble();
ComplexNumber num2 = new ComplexNumber(real2, imaginary2);
System.out.println("Сумма ваших чисел: " + num1.add(num2));
break;
case 3:
System.out.println("Введите количество строк в матрице:");
int rows = scanner.nextInt();
System.out.println("Введите количество столбцов в матрице:");
int cols = scanner.nextInt();
ComplexNumber[][] elements = new ComplexNumber[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.println("Введите действительную часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elReal = scanner.nextDouble();
System.out.println("Введите мнимую часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elImaginary = scanner.nextDouble();
elements[i][j] = new ComplexNumber(elReal, elImaginary);
}
}
Matrix matrix = new Matrix(elements);
System.out.println("Итоговая матрица:");
System.out.println(matrix);
break;
case 4:
System.out.println("Введите размерность первой матрицы.");
System.out.print("Количество строк: ");
int rows1 = scanner.nextInt();
System.out.print("Количество столбцов: ");
int cols1 = scanner.nextInt();
System.out.println("Введите элементы первой матрицы:");
ComplexNumber[][] elements1 = new ComplexNumber[rows1][cols1];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
System.out.println("Введите действительную часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elReal = scanner.nextDouble();
System.out.println("Введите мнимую часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elImaginary = scanner.nextDouble();
elements1[i][j] = new ComplexNumber(elReal, elImaginary);
}
}
Matrix matrix1 = new Matrix(elements1);
System.out.println("Введите размерность второй матрицы.");
System.out.print("Количество строк: ");
int rows2 = scanner.nextInt();
System.out.print("Количество столбцов: ");
int cols2 = scanner.nextInt();
System.out.println("Введите элементы второй матрицы:");
ComplexNumber[][] elements2 = new ComplexNumber[rows2][cols2];
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
System.out.println("Введите действительную часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elReal = scanner.nextDouble();
System.out.println("Введите мнимую часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elImaginary = scanner.nextDouble();
elements2[i][j] = new ComplexNumber(elReal, elImaginary);
}
}
Matrix matrix2 = new Matrix(elements2);
if (rows1 == rows2 && cols1 == cols2) {
Matrix sumMatrix = matrix1.add(matrix2);
System.out.println("Сумма двух матриц:");
System.out.println(sumMatrix);
} else {
System.out.println("Упс, ошибка! Невозможно сложить матрицы разной размерности.");
}
break;
case 5:
System.out.println("Введите количество строк первой матрицы:");
int row1 = scanner.nextInt();
System.out.println("Введите количество столбцов первой матрицы:");
int col1 = scanner.nextInt();
System.out.println("Введите количество строк второй матрицы:");
int row2 = scanner.nextInt();
System.out.println("Введите количество столбцов второй матрицы:");
int col2 = scanner.nextInt();
if (row2 != col1) {
System.out.println("Упс, ошибка! Количество столбцов первой матрицы должно совпадать с количеством строк второй матрицы.");
break;
}
System.out.println("Введите элементы первой матрицы:");
ComplexNumber[][] elementsMatrix1 = new ComplexNumber[row1][col1];
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
System.out.println("Введите действительную часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elReal = scanner.nextDouble();
System.out.println("Введите мнимую часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elImaginary = scanner.nextDouble();
elementsMatrix1[i][j] = new ComplexNumber(elReal, elImaginary);
}
}
Matrix matr1 = new Matrix(elementsMatrix1);
System.out.println("Введите элементы второй матрицы:");
ComplexNumber[][] elementsMatrix2 = new ComplexNumber[row2][col2];
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
System.out.println("Введите действительную часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elReal = scanner.nextDouble();
System.out.println("Введите мнимую часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elImaginary = scanner.nextDouble();
elementsMatrix2[i][j] = new ComplexNumber(elReal, elImaginary);
}
}
Matrix matr2 = new Matrix(elementsMatrix2);
matr1 = new Matrix(elementsMatrix1);
matr2 = new Matrix(elementsMatrix2);
Matrix productMatrix = matr1.multiply(matr2);
System.out.println("Произведение двух матриц:");
System.out.println(productMatrix);
break;
case 6:
System.out.println("Введите количество строк матрицы:");
int row = scanner.nextInt();
System.out.println("Введите количество столбцов матрицы:");
int col = scanner.nextInt();
System.out.println("Введите элементы матрицы:");
ComplexNumber[][] elementsMatrix = new ComplexNumber[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.println("Введите действительную часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elReal = scanner.nextDouble();
System.out.println("Введите мнимую часть числа на позиции [" + (i + 1) + "][" + (j + 1) + "]:");
double elImaginary = scanner.nextDouble();
elementsMatrix[i][j] = new ComplexNumber(elReal, elImaginary);
}
}
Matrix matr = new Matrix(elementsMatrix);
Matrix transposedMatrix = matr.transpose();
System.out.println("Транспонированная матрица:");
System.out.println(transposedMatrix);
break;
default:
System.out.println("Упс, ошибка! Некорректный выбор.");
}
}
}
class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber other) {
return new ComplexNumber(this.real + other.real, this.imaginary + other.imaginary);
}
public ComplexNumber multiply(ComplexNumber other) {
double newReal = this.real * other.real - this.imaginary * other.imaginary;
double newImaginary = this.real * other.imaginary + this.imaginary * other.real;
return new ComplexNumber(newReal, newImaginary);
}
public String toString() {
return this.real + " + " + this.imaginary + "i";
}
}
class Matrix {
private ComplexNumber[][] elements;
public Matrix(ComplexNumber[][] elements) {
this.elements = elements;
}
public Matrix add(Matrix other) {
if (this.elements.length != other.elements.length || this.elements[0].length != other.elements[0].length) {
throw new IllegalArgumentException("Упс, ошибка! Матрицы должны иметь одинаковые размеры для сложения");
}
ComplexNumber[][] result = new ComplexNumber[this.elements.length][this.elements[0].length];
for (int i = 0; i < this.elements.length; i++) {
for (int j = 0; j < this.elements[0].length; j++) {
result[i][j] = this.elements[i][j].add(other.elements[i][j]);
}
}
return new Matrix(result);
}
public Matrix multiply(Matrix other) {
if (this.elements[0].length != other.elements.length) {
throw new IllegalArgumentException("Упс, ошибка! Число столбцов первой матрицы должно совпадать с числом строк второй для умножения");
}
ComplexNumber[][] result = new ComplexNumber[this.elements.length][other.elements[0].length];
for (int i = 0; i < this.elements.length; i++) {
for (int j = 0; j < other.elements[0].length; j++) {
ComplexNumber sum = new ComplexNumber(0, 0);
for (int k = 0; k < this.elements[0].length; k++) {
sum = sum.add(this.elements[i][k].multiply(other.elements[k][j]));
}
result[i][j] = sum;
}
}
return new Matrix(result);
}
public Matrix transpose() {
int row = elements.length;
int col = elements[0].length;
ComplexNumber[][] transposedElements = new ComplexNumber[col][row];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
transposedElements[j][i] = elements[i][j];
}
}
return new Matrix(transposedElements);
}
public String toString() {
StringBuilder res = new StringBuilder();
for (ComplexNumber[] row : elements) {
res.append(Arrays.toString(row)).append("\n");
}
return res.toString();
}
}