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());
}
}
Je ne suis pas en mesure de recevoir l'entrée correcte de l'utilisateur. Je veux être en mesure de recevoir 2+4i
dans le premier nombre complexe et 4+5i
en second nombre complexe à partir de l'entrée de l'utilisateur. Mais cela ne fonctionne pas.