2016-11-14 2 views
2
public class Complex { 

    private double real; 
    private double imaginary; 

    public Complex(){ 
     this.real=0; 
     this.imaginary=0; 
    } 

    public Complex(double real,double imaginary){ 
     this.real=real; 
     this.imaginary=imaginary; 
    } 

    public double getReal() { 
     return real; 
    } 

    public void setReal(double real) { 
     this.real = real; 
    } 

    public double getImaginary() { 
     return imaginary; 
    } 

    public void setImaginary(double imaginary) { 
     this.imaginary = imaginary; 
    } 

    public Complex add(Complex num){ 
     double r=this.real+num.real; 
     double i=this.imaginary + num.imaginary; 
     Complex s= new Complex(r,i); 
     return s; 

    } 

    public Complex sub(Complex num){ 
     double r= this.real- num.real; 
     double i= this.imaginary - num.imaginary; 
     Complex s= new Complex(r,i); 
     return s; 
    } 

    public Complex mul(Complex num){ 
     double r= this.real*num.real - this.imaginary*num.imaginary; 
     double i= this.real*num.imaginary+this.imaginary*num.real; 

     Complex s=new Complex(r,i); 
     return s; 
    } 

    public Complex div(Complex num){ 
     double r= this.real/num.real- this.imaginary/num.imaginary; 
     double i = this.real/num.imaginary+this.imaginary/num.real; 

     Complex s=new Complex(r,i); 
     return s; 
    } 

    public String toString(){ 
     //double x=this.real + this.imaginary; 
     //return " "+x; 

     return this.real+" + "+this.imaginary+"i"; 
    } 
} 



import java.util.*; 
import java.math.*; 

public class Driver { 

    public static final double i=Math.sqrt(-1); 

    public static void main(String[] args) { 

     Scanner get=new Scanner(System.in); 

     int choice; 
     double firstComplex; 
     double secondComplex; 

     //Complex c1 = new Complex(3.0,4.2); 
     //Complex c2 = new Complex(-12.2,3.4); 

     //Complex c4 =c1.sub(c2); 
     //Complex c5 =c1.mul(c2); 
     //Complex c6 =c1.div(c2); 

     while(true){ 
      System.out.println("Please type your choice and enter : "); 

      System.out.println("1.Add Two Complex Numbers"); 
      System.out.println("2.Substract Two Complex Numbers"); 
      System.out.println("3.Multiply Two Complex Numbers"); 
      System.out.println("4.Divide Two Complex Numbers"); 
      System.out.println("5.Exit Program"); 

      choice= get.nextInt(); 

      switch(choice){ 
       case 1 : 
        System.out.println("Enter first complex number: "); 

        firstComplex=get.nextDouble(); 

        System.out.println("Enter Second complex number: "); 

        secondComplex=get.nextDouble(); 

        Complex c1 = new Complex(firstComplex,firstComplex); 
        Complex c2 = new Complex(secondComplex,secondComplex); 
        Complex c3 =c1.add(c2); 

        System.out.println(c3.toString()); 
      } 
     } 

Ich bin nicht in der Lage, die richtigen Benutzereingaben zu erhalten. Ich möchte in der Lage sein, 2+4i in der ersten komplexen Zahl und 4+5i in der zweiten komplexen Zahl von den Benutzereingaben zu empfangen. Aber es funktioniert nicht.Wie Benutzereingabe für komplexe Nummer

+1

Sie müssen genauer darüber sein, was nicht funktioniert und was Sie erwarten. – nhouser9

+3

Sie müssen die Eingabe als Zeichenfolge lesen und in den Real- und Imaginärteil parsen. –

+0

Benutzer geben erste komplexe Zahl als 2 + 4i und zweite komplexe Zahl als 4 + 5i aber ich kann es nicht –

Antwort

2

Zu Beginn der Hauptmethode:

Pattern p = Pattern.compile("(.*)([+-].*)i"); 
    double real, imaginary; 

Dann in case 1:

System.out.println("Enter first complex number: "); 

    real = 0.0; 
    imaginary = 0.0; 
    Matcher m = p.match(get.nextLine()); // read the user input as a string 
    if (m.matches()) { // if the user input matches the required pattern 
     real = Double.parseDouble(m.group(1)); // extract the real part 
     imaginary = Double.parseDouble(m.group(2)); // extract the imaginary part 
    } 
    Complex c1 = new Complex(real, imaginary); // build the Complex object 

    System.out.println("Enter Second complex number: "); 

    real = 0.0; 
    imaginary = 0.0; 
    Matcher m = p.match(get.nextLine()); 
    if (m.matches()) { 
     real = Double.parseDouble(m.group(1)); 
     imaginary = Double.parseDouble(m.group(2)); 
    } 
    Complex c2 = new Complex(real, imaginary); 

    Complex c3 =c1.add(c2); 

Sie werden wahrscheinlich wollen Handhabung einige Fehler addieren, wenn die Eingabe des Benutzers nicht das erforderliche Muster überein (sonst sind real und imaginary beide 0).

+0

Woher kommen diese Muster und Matcher? bitte helfen! –

+0

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html und https://docs.oracle.com/javase/7/docs/api/java/util /regex/Matcher.html – Jason

0

Fügen Sie die folgenden Verfahren in Treiberklasse

public static Complex getComplexNumber(final Scanner get){ 
     String firstComplex=get.nextLine(); 
     String[] arr = firstComplex.split("[-+]i"); 
     return new Complex(Double.parseDouble(arr[0]),Double.parseDouble(arr[1])); 
} 

und dann statt firstComplex = get.nextDouble(); Verwenden Sie firstComplex = getComplexNumber (get); Handle Ausnahmen sowie

+0

Dies deckt nur die komplexen Zahlen mit positiven Imaginärteilen ab. Sie können die Aufteilung in einen regulären Ausdruck ändern, der sowohl positive als auch negative – Akaitenshi

+0

dank @Akaitenshi modifiziert nach Ihrem Vorschlag abdeckt. –

0

Sie müssen echte und imaginäre Pfad der komplexen Zahl.

Indem ein paar Änderungen in Ihrem Code Ich habe dieses Ergebnis:

Haupt Logik ist:

private final static Pattern PATTERN = Pattern.compile("(.*)([+-])(.*)i"); 
// ... 
String complexInput = get.next(); 
final Matcher matcher = PATTERN.matcher(complexInput); 
if (matcher.find()) { 
    final double imgSign = matcher.group(2).equals("+") ? 1D : -1D; 
    final double real = Double.parseDouble(matcher.group(1)); 
    final double img = Double.parseDouble(matcher.group(3)); 
    return new Complex(real, imgSign * img); 
} 

Alle Code:

public class Driver { 

    private final static Pattern PATTERN = Pattern.compile("(.*)([+-])(.*)i"); 
    private static final int CHOICE_EXIT = 5; 
    private static final int CHOICE_ADD = 1; 

    public static void main(String[] args) { 
     Scanner get = new Scanner(System.in); 

     int choice; 
     Complex c1; 
     Complex c2; 

     while (true) { 
      try { 
       printInfoAboutChoice(); 
       choice = get.nextInt(); 
       if (choice == CHOICE_EXIT) { 
        break; 
       } 
       System.out.println("Enter first complex number: "); 
       c1 = getComplexFromString(get.next()); 

       System.out.println("Enter Second complex number: "); 
       c2 = getComplexFromString(get.next()); 
      } catch (RuntimeException e) { 
       System.err.println(e.getMessage()); 
       continue; 
      } 

      switch (choice) { 
       case CHOICE_ADD: { 
        Complex c3 = c1.add(c2); 
        System.out.println(c3.toString()); 
       } 
       // TODO others methods... 
      } 
     } 
    } 

    private static Complex getComplexFromString(String complexInput) throws IllegalFormatException { 
     final Matcher matcher = PATTERN.matcher(complexInput); 
     if (matcher.find()) { 
      final double imgSign = matcher.group(2).equals("+") ? 1D : -1D; 
      final double real = Double.parseDouble(matcher.group(1)); 
      final double img = Double.parseDouble(matcher.group(3)); 
      return new Complex(real, imgSign * img); 
     } 
     throw new IllegalArgumentException("Bad complex number input"); 
    } 

    private static void printInfoAboutChoice() { 
     System.out.println("Please type your choice and enter : "); 
     System.out.println("1.Add Two Complex Numbers"); 
     System.out.println("2.Substract Two Complex Numbers"); 
     System.out.println("3.Multiply Two Complex Numbers"); 
     System.out.println("4.Divide Two Complex Numbers"); 
     System.out.println("5.Exit Program"); 
    } 
} 
+0

Dies entspricht nicht dem Eingabeformat, das vom OP angegeben wurde. Das OP möchte 2 + 4i oder 4 + 5i eingeben können. – Jason

+0

Sie haben Recht. Ich habe die Verwendung des Typs Double für die Variable des komplexen Typs im Beispiel in die Irre geführt. –

0

Hier ist etwas, wenn Sie Ganzzahlige Teile. Bitte für Doppelzimmer ändern.

Complex c1; 
Scanner sline = new Scanner(System.in); 
Pattern p = Pattern.compile("(\+|-){0,1}\\d+[ ]*(\+|-)[ ]*(i\\d+|\\d+i)"); 
if(sline.hasNext(p)) { 
    String str = sline.next(p).replace("+"," +").replace("-"," -"); 
    Scanner sc = new Scanner(str); 
    int r = sc.nextInt(); 
    int i = Integer.parseInt(sc.next().replace('i','')); 
    c1 = new Complex(r, i); 
}