Filip_Markoski

[NP] 4.1 Комплексни броеви (Sovled)

Oct 25th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Collections;
  3. import java.util.LinkedList;
  4.  
  5. class ComplexNumber<T extends Number, U extends Number> implements Comparable<ComplexNumber<?, ?>> {
  6.     private T real;
  7.     private U imaginary;
  8.  
  9.     ComplexNumber(T real, U imaginary) {
  10.         this.real = real;
  11.         this.imaginary = imaginary;
  12.     }
  13.  
  14.     public T getReal() {
  15.         return real;
  16.     }
  17.  
  18.     public U getImaginary() {
  19.         return imaginary;
  20.     }
  21.  
  22.     public double modul() {
  23.         /* sqrt(x^2 + y^2)  */
  24.         return Math.sqrt(Math.pow(real.doubleValue(), 2) + Math.pow(imaginary.doubleValue(), 2));
  25.  
  26.     }
  27.  
  28.     @Override
  29.     public String toString() {
  30.         StringBuffer sb = new StringBuffer();
  31.         if (imaginary.doubleValue() >= 0) {
  32.             sb.append(String.format("%.2f+%.2fi", real.doubleValue(), imaginary.doubleValue()));
  33.         } else {
  34.             sb.append(String.format("%.2f-%.2fi", real.doubleValue(), imaginary.doubleValue() * (-1)));
  35.         }
  36.         return sb.toString();
  37.     }
  38.  
  39.     @Override
  40.     public int compareTo(ComplexNumber<?, ?> that) {
  41.         return Double.compare(this.modul(), that.modul());
  42.     }
  43. }
  44.  
  45. public class ComplexNumberTest {
  46.  
  47.     public static void main(String[] args) {
  48.         Scanner jin = new Scanner(System.in);
  49.         int k = jin.nextInt();
  50.         if (k == 0) { //test simple functions int
  51.             int r = jin.nextInt();
  52.             int i = jin.nextInt();
  53.             ComplexNumber<Integer, Integer> c = new ComplexNumber<Integer, Integer>(r, i);
  54.             System.out.println(c);
  55.             System.out.println(c.getReal());
  56.             System.out.println(c.getImaginary());
  57.             System.out.println(c.modul());
  58.         }
  59.         if (k == 1) { //test simple functions float
  60.             float r = jin.nextFloat();
  61.             float i = jin.nextFloat();
  62.             ComplexNumber<Float, Float> c = new ComplexNumber<Float, Float>(r, i);
  63.             System.out.println(c);
  64.             System.out.println(c.getReal());
  65.             System.out.println(c.getImaginary());
  66.             System.out.println(c.modul());
  67.         }
  68.         if (k == 2) { //compareTo int
  69.             LinkedList<ComplexNumber<Integer, Integer>> complex = new LinkedList<ComplexNumber<Integer, Integer>>();
  70.             while (jin.hasNextInt()) {
  71.                 int r = jin.nextInt();
  72.                 int i = jin.nextInt();
  73.                 complex.add(new ComplexNumber<Integer, Integer>(r, i));
  74.             }
  75.             System.out.println(complex);
  76.             Collections.sort(complex);
  77.             System.out.println(complex);
  78.         }
  79.         if (k == 3) { //compareTo double
  80.             LinkedList<ComplexNumber<Double, Double>> complex = new LinkedList<ComplexNumber<Double, Double>>();
  81.             while (jin.hasNextDouble()) {
  82.                 double r = jin.nextDouble();
  83.                 double i = jin.nextDouble();
  84.                 complex.add(new ComplexNumber<Double, Double>(r, i));
  85.             }
  86.             System.out.println(complex);
  87.             Collections.sort(complex);
  88.             System.out.println(complex);
  89.         }
  90.         if (k == 4) { //compareTo mixed
  91.             LinkedList<ComplexNumber<Double, Integer>> complex = new LinkedList<ComplexNumber<Double, Integer>>();
  92.             while (jin.hasNextDouble()) {
  93.                 double r = jin.nextDouble();
  94.                 int i = jin.nextInt();
  95.                 complex.add(new ComplexNumber<Double, Integer>(r, i));
  96.             }
  97.             System.out.println(complex);
  98.             Collections.sort(complex);
  99.             System.out.println(complex);
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment