Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Übung11;
- import java.util.Scanner;
- class C{
- public static Scanner sc=new Scanner(System.in);
- private double Re;//Ein Objekt der Klasse C(Für komplexe Zahlen) besteht aus einem Real und einem Imaginärteil
- private double Im;
- //konstrukter
- C(){
- Re=0;//Konstrukter, der nicht null auf die Werte gibt, da das doof ist
- Im=0;
- }
- C(double R, double I){//Noch besser: Instanz direkt mit richtigen werten erstellen
- Re=R;
- Im=I;
- }
- C(String Name){
- System.out.print("Re "+Name+" :");
- Re=sc.nextDouble();
- System.out.print("Im "+Name+" :");
- Im=sc.nextDouble();
- }
- //Ausgabe
- void Re() {
- System.out.print(Re);
- }
- void Im() {
- System.out.print(Im);
- }
- void print() {
- System.out.print(Re+" + "+Im+"i");
- }
- //Rechenmethoden
- void neg(){
- Re=-Re;
- Im=-Im;
- }
- void konj() {
- Im=-Im;
- }
- void ska(double s) {
- Re=Re*s;
- Im=Im*s;
- }
- void add(C x, C y) {
- Re=x.Re+y.Re;
- Im=x.Im+y.Im;
- }
- void sub(C x, C y) {
- y.neg();
- Re=x.Re+y.Re;
- Im=x.Im+y.Im;
- }
- void mult(C x, C y) {
- Re=x.Re*y.Re-x.Im*y.Im;
- Im=x.Re*y.Im+x.Im*y.Re;
- }
- void div(C x, C y) {
- Re=(x.Re*y.Re+x.Im*y.Im)/(y.Re*y.Re+y.Im*y.Im);
- Im=(x.Im*y.Re+x.Re*y.Im)/(y.Re*y.Re+y.Im*y.Im);
- }
- double Abs() {
- double Out= Math.pow(Re*Re+Im*Im, 0.5);
- return Out;
- }
- boolean equals(C x, C y) {
- return (x.Re==y.Re&&x.Im==y.Im);
- }
- }
- abstract class Tools{
- public static Scanner sc=new Scanner(System.in);
- public final boolean Aus=true;
- public static String[] SArrayErw(String[] In, String Add) {
- String Out[]=new String[In.length+1];
- int c=0;
- while (c<In.length) {
- Out[c]=In[c];
- c++;
- }
- Out[c]=Add;
- return Out;
- }
- public static C[] CArrayErw(C[] In, C Add) {
- C Out[]=new C[In.length+1];
- int c=0;
- while (c<In.length) {
- Out[c]=In[c];
- c++;
- }
- Out[c]=Add;
- return Out;
- }
- public static String[] StringEinlesen() {
- String[] Out=new String[0];
- String Eingabe="hallo";
- while (!Eingabe.equals("Stop")) {
- Eingabe=sc.next();
- Out=SArrayErw(Out, Eingabe);
- }
- return Out;
- }
- //Drucker
- public static void P(String In) {
- System.out.print(In);
- }
- public static void L(String In) {
- System.out.println(In);
- }
- }
- public class KomplexeZahlen {
- public static Scanner sc=new Scanner(System.in);
- public static Tools t;
- public static void main(String[] args) {
- C z[]=new C[] {new C()};
- int c=1;
- boolean B=true;
- while (B) {
- t.L("Z"+c+":");
- z=t.CArrayErw(z, new C("Z"+c++));
- t.P("Für Eingabe-Abbruch schreibe Stop");
- if (sc.next().equalsIgnoreCase("Stop"))B=false;
- }
- int a=0,b=0;
- char r='q';
- B=true;
- t.L("Rechenoperationen: \n-Addition '+'\n-Subtraktion '-'\n-Multiplikation '*'\n-Division '/'\n-Absoluter Wert 'A'\n-Konjugierte Zahl 'K'\n-Testaufgleichheit '='");
- t.L("-Skalarmultiplikation '#'\n-Auf nächster Stelle speichern (Nur im bereits erstellten!) '~'");
- t.L("Tipp: Das Ergebnis der letzten Rechnung wird an der Stelle Z0 abgespeichert.");
- t.L("Gebe als Rechenoperation '!' ein, um das Programm zu beenden");
- while(B) {
- a=z.length;
- b=z.length;
- while(a>=z.length) {
- t.P("Z");a=sc.nextInt();
- }
- t.P("Rechenoperation: ");r=sc.next().charAt(0);
- switch (r) {
- case('+'):
- t.P("Z");b=sc.nextInt();
- z[0].add(z[a],z[b]);
- z[0].print();t.P(" = ");z[a].print();t.P(" + ");z[b].print();t.L("");
- break;
- case('-'):
- t.P("Z");b=sc.nextInt();
- z[0].sub(z[a],z[b]);
- z[0].print();t.P(" = ");z[a].print();t.P(" - (");z[b].print();t.L(")");
- break;
- case('*'):
- t.P("Z");b=sc.nextInt();
- z[0].mult(z[a],z[b]);
- z[0].print();t.P(" = ");t.P("(");z[a].print();t.P(") * (");z[b].print();t.L(")");
- break;
- case('/'):
- t.P("Z");b=sc.nextInt();
- z[0].div(z[a],z[b]);
- z[0].print();t.P(" = ");t.P("(");z[a].print();t.P(") / (");z[b].print();t.L(")");
- break;
- case('k'):
- z[0]=z[a];
- z[0].konj();
- z[a].print();t.P(" konjugiert ist gleich ");z[0].print();t.L("");
- break;
- case('A'):
- t.L("|Z"+a+"| = "+z[a].Abs());
- break;
- case('='):
- t.P("Z");b=sc.nextInt();
- if (z[a].equals(z[b])) t.L("Z"+a+" = Z"+b);
- else t.L("Z"+a+" ≠ Z"+b);
- break;
- case('~'):
- t.P("Z"+a);t.P("↦");t.P("Z");b=sc.nextInt();
- z[b]=z[a];
- t.P("Z"+b+" = ");z[b].print();t.L("");
- break;
- case('#'):
- t.P("Skalar = ");double S=sc.nextDouble();
- z[0]=z[a];
- z[0].ska(S);
- t.P("Z"+a+ " * " +S+" = ");z[0].print();t.L("");
- break;
- case('!'):
- B=false;
- t.L("");
- break;
- default: break;
- }
- t.L("");
- }
- c=0;
- while (c<z.length) {
- t.P("Z"+c+" = ");z[c].print();t.L("");
- c++;
- }
- t.sc.close();
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment