Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class NumarComplex
- {
- protected double re, im;
- public NumarComplex(double re, double im)
- {
- this.re = re;
- this.im = im;
- }
- public double modul()
- {
- return Math.sqrt(re * re + im * im);
- }
- public NumarComplex multiply(NumarComplex x)
- {
- return new NumarComplex(re * x.re - im * x.im, re * x.im + im * x.re);
- }
- public NumarComplex adunare(NumarComplex x)
- {
- return new NumarComplex(re + x.re, im + x.im);
- }
- public static NumarComplex[][] matrix_multiply(NumarComplex[][] a, NumarComplex[][] b)
- {
- NumarComplex s = new NumarComplex(0, 0);
- NumarComplex[][] c = new NumarComplex[a.length][b[0].length];
- for (int i = 0; i < a.length; i++)
- {
- for (int j = 0; j < b[0].length; j++)
- {
- s.re = 0.0;
- s.im = 0.0;
- for (int k = 0; k < b.length; k++)
- s = s.adunare(a[i][k].multiply(b[k][j]));
- if (s.im != 0.0)
- c[i][j] = new NumarComplex(s.re, s.im);
- else
- c[i][j] = new NumarReal(s.re);
- }
- }
- return c;
- }
- public String toString()
- {
- return re + " + " + im + " * i";
- }
- }
- class NumarReal extends NumarComplex
- {
- public NumarReal(double re)
- {
- super(re, 0);
- }
- public boolean maiMare(NumarReal a)
- {
- return re > a.re;
- }
- public String toString()
- {
- return re + "";
- }
- }
- class Client
- {
- public static void main(String argv[])
- {
- int n, m, p;
- NumarComplex s;
- NumarComplex[][] a, b, c;
- try
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("n="); n = Integer.parseInt(br.readLine());
- System.out.print("m="); m = Integer.parseInt(br.readLine());
- System.out.print("p="); p = Integer.parseInt(br.readLine());
- a = new NumarComplex[n][m];
- b = new NumarComplex[m][p];
- c = new NumarComplex[n][p];
- System.out.println("\nMatricea A: ");
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- System.out.print("[" + i + ", " + j + "] Complex sau real (c / r): ");
- switch ((br.readLine()).charAt(0))
- {
- case 'c':
- {
- a[i][j] = new NumarComplex(Double.parseDouble(br.readLine()), Double.parseDouble(br.readLine()));
- break;
- }
- case 'r':
- {
- a[i][j] = new NumarReal(Double.parseDouble(br.readLine()));
- break;
- }
- default:
- {
- System.out.print("[" + (i + 1) + ", " + (j + 1) + "] Complex sau real (c / r): ");
- break;
- }
- }
- }
- }
- System.out.println("\nMatricea B: ");
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < p; j++)
- {
- System.out.print("[" + i + ", " + j + "] Complex sau real (c / r): ");
- switch ((br.readLine()).charAt(0))
- {
- case 'c':
- {
- b[i][j] = new NumarComplex(Double.parseDouble(br.readLine()), Double.parseDouble(br.readLine()));
- break;
- }
- case 'r':
- {
- b[i][j] = new NumarReal(Double.parseDouble(br.readLine()));
- break;
- }
- default:
- {
- System.out.print("[" + (i + 1) + ", " + (j + 1) + "] Complex sau real (c / r): ");
- break;
- }
- }
- }
- }
- c = NumarComplex.matrix_multiply(a, b);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < p; j++)
- System.out.print(c[i][j] + " ");
- System.out.println();
- }
- }
- catch (IOException e)
- {
- System.out.println("Eroare la citire");
- System.exit(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment