Randomsurpriseguy

Komplexe Zahlen

Jan 20th, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. package Übung11;
  2. import java.util.Scanner;
  3. class C{
  4.    
  5.     public static Scanner sc=new Scanner(System.in);
  6.     private double Re;//Ein Objekt der  Klasse C(Für komplexe Zahlen) besteht aus einem Real und einem Imaginärteil
  7.     private double Im;
  8.    
  9.    
  10.     //konstrukter
  11.     C(){
  12.         Re=0;//Konstrukter, der nicht null auf die Werte gibt, da das doof ist
  13.         Im=0;
  14.     }
  15.    
  16.     C(double R, double I){//Noch besser: Instanz direkt mit richtigen werten erstellen
  17.         Re=R;
  18.         Im=I;
  19.     }
  20.     C(String Name){
  21.         System.out.print("Re "+Name+" :");
  22.         Re=sc.nextDouble();
  23.         System.out.print("Im "+Name+" :");
  24.         Im=sc.nextDouble();
  25.     }
  26.    
  27.     //Ausgabe
  28.     void Re() {
  29.         System.out.print(Re);
  30.     }
  31.     void Im() {
  32.         System.out.print(Im);
  33.     }
  34.    
  35.     void print() {
  36.         System.out.print(Re+" + "+Im+"i");
  37.     }
  38.    
  39.     //Rechenmethoden
  40.    
  41.     void neg(){
  42.         Re=-Re;
  43.         Im=-Im;
  44.     }
  45.    
  46.     void konj() {
  47.         Im=-Im;
  48.     }
  49.    
  50.     void ska(double s) {
  51.         Re=Re*s;
  52.         Im=Im*s;
  53.     }
  54.    
  55.    
  56.     void add(C x, C y) {
  57.         Re=x.Re+y.Re;
  58.         Im=x.Im+y.Im;
  59.        
  60.     }
  61.    
  62.    
  63.    
  64.     void sub(C x, C y) {
  65.         y.neg();
  66.         Re=x.Re+y.Re;
  67.         Im=x.Im+y.Im;
  68.        
  69.     }
  70.    
  71.     void mult(C x, C y) {
  72.         Re=x.Re*y.Re-x.Im*y.Im;
  73.         Im=x.Re*y.Im+x.Im*y.Re;
  74.        
  75.     }
  76.    
  77.     void div(C x, C y) {
  78.         Re=(x.Re*y.Re+x.Im*y.Im)/(y.Re*y.Re+y.Im*y.Im);
  79.         Im=(x.Im*y.Re+x.Re*y.Im)/(y.Re*y.Re+y.Im*y.Im);
  80.        
  81.     }
  82.    
  83.     double Abs() {
  84.         double Out= Math.pow(Re*Re+Im*Im, 0.5);
  85.         return Out;
  86.     }
  87.    
  88.     boolean equals(C x, C y) {
  89.         return (x.Re==y.Re&&x.Im==y.Im);
  90.     }
  91.    
  92.    
  93. }
  94.  
  95. abstract class Tools{
  96.    
  97.     public static Scanner sc=new Scanner(System.in);
  98.    
  99.     public final boolean Aus=true;
  100.    
  101.     public static String[] SArrayErw(String[] In, String Add) {
  102.         String Out[]=new String[In.length+1];
  103.         int c=0;
  104.         while (c<In.length) {
  105.             Out[c]=In[c];
  106.             c++;
  107.         }
  108.         Out[c]=Add;
  109.         return Out;
  110.     }
  111.    
  112.     public static C[] CArrayErw(C[] In, C Add) {
  113.         C Out[]=new C[In.length+1];
  114.         int c=0;
  115.         while (c<In.length) {
  116.             Out[c]=In[c];
  117.             c++;
  118.         }
  119.         Out[c]=Add;
  120.         return Out;
  121.     }
  122.    
  123.     public static String[] StringEinlesen() {
  124.         String[] Out=new String[0];
  125.         String Eingabe="hallo";
  126.         while (!Eingabe.equals("Stop")) {
  127.             Eingabe=sc.next();
  128.             Out=SArrayErw(Out, Eingabe);
  129.            
  130.         }
  131.         return Out;
  132.        
  133.     }
  134.     //Drucker
  135.     public static void P(String In) {
  136.         System.out.print(In);
  137.     }
  138.     public static void L(String In) {
  139.         System.out.println(In);
  140.     }
  141.    
  142.    
  143. }
  144.  
  145.  
  146. public class KomplexeZahlen {
  147.    
  148.     public static Scanner sc=new Scanner(System.in);
  149.     public static Tools t;
  150.  
  151.    
  152.    
  153.     public static void main(String[] args) {
  154.         C z[]=new C[] {new C()};
  155.         int c=1;
  156.        
  157.         boolean B=true;
  158.         while (B) {
  159.             t.L("Z"+c+":");
  160.             z=t.CArrayErw(z, new C("Z"+c++));
  161.             t.P("Für Eingabe-Abbruch schreibe Stop");
  162.             if (sc.next().equalsIgnoreCase("Stop"))B=false;
  163.         }
  164.         int a=0,b=0;
  165.         char r='q';
  166.         B=true;
  167.        
  168.         t.L("Rechenoperationen: \n-Addition '+'\n-Subtraktion '-'\n-Multiplikation '*'\n-Division '/'\n-Absoluter Wert 'A'\n-Konjugierte Zahl 'K'\n-Testaufgleichheit '='");
  169.         t.L("-Skalarmultiplikation '#'\n-Auf nächster Stelle speichern (Nur im bereits erstellten!) '~'");
  170.         t.L("Tipp: Das Ergebnis der letzten Rechnung wird an der Stelle Z0 abgespeichert.");
  171.         t.L("Gebe als Rechenoperation '!' ein, um das Programm zu beenden");
  172.         while(B) {
  173.             a=z.length;
  174.             b=z.length;
  175.             while(a>=z.length) {
  176.                 t.P("Z");a=sc.nextInt();
  177.             }
  178.             t.P("Rechenoperation: ");r=sc.next().charAt(0);
  179.             switch (r) {
  180.             case('+'):
  181.                 t.P("Z");b=sc.nextInt();
  182.                 z[0].add(z[a],z[b]);
  183.                 z[0].print();t.P(" = ");z[a].print();t.P(" + ");z[b].print();t.L("");
  184.                 break;
  185.             case('-'):
  186.                 t.P("Z");b=sc.nextInt();
  187.                 z[0].sub(z[a],z[b]);
  188.                 z[0].print();t.P(" = ");z[a].print();t.P(" - (");z[b].print();t.L(")");
  189.                 break;
  190.             case('*'):
  191.                 t.P("Z");b=sc.nextInt();
  192.                 z[0].mult(z[a],z[b]);
  193.                 z[0].print();t.P(" = ");t.P("(");z[a].print();t.P(") * (");z[b].print();t.L(")");
  194.                 break;
  195.             case('/'):
  196.                 t.P("Z");b=sc.nextInt();
  197.                 z[0].div(z[a],z[b]);
  198.                 z[0].print();t.P(" = ");t.P("(");z[a].print();t.P(") / (");z[b].print();t.L(")");
  199.                 break;
  200.             case('k'):
  201.                 z[0]=z[a];
  202.                 z[0].konj();
  203.                 z[a].print();t.P(" konjugiert ist gleich ");z[0].print();t.L("");
  204.                 break;
  205.             case('A'):
  206.                 t.L("|Z"+a+"| = "+z[a].Abs());
  207.                 break;
  208.             case('='):
  209.                 t.P("Z");b=sc.nextInt();
  210.                 if (z[a].equals(z[b])) t.L("Z"+a+" = Z"+b);
  211.                 else t.L("Z"+a+" ≠ Z"+b);
  212.                 break;
  213.             case('~'):
  214.                
  215.                 t.P("Z"+a);t.P("↦");t.P("Z");b=sc.nextInt();
  216.                 z[b]=z[a];
  217.                 t.P("Z"+b+" = ");z[b].print();t.L("");
  218.                 break;
  219.             case('#'):
  220.                 t.P("Skalar = ");double S=sc.nextDouble();
  221.                 z[0]=z[a];
  222.                 z[0].ska(S);
  223.                 t.P("Z"+a+ " * " +S+" = ");z[0].print();t.L("");
  224.                 break;
  225.             case('!'):
  226.                 B=false;
  227.                 t.L("");
  228.                 break;
  229.             default: break;
  230.                
  231.            
  232.             }
  233.             t.L("");
  234.            
  235.         }
  236.         c=0;
  237.         while (c<z.length) {
  238.             t.P("Z"+c+" = ");z[c].print();t.L("");
  239.             c++;
  240.         }
  241.        
  242.         t.sc.close();
  243.         sc.close();
  244.        
  245.  
  246.     }
  247.  
  248. }
Add Comment
Please, Sign In to add comment