[英]How to take user input for complex number
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());
}
}
I am not able to receive the correct user input. 我无法收到正确的用户输入。 I want to be able to receive 2+4i
in first complex number and 4+5i
in second complex number from user input. 我希望能够从用户输入接收第一个复数中的2+4i
和第二个复数中的4+5i
。 But it's not working. 但它不起作用。
At the start of your main method: 在主要方法的开头:
Pattern p = Pattern.compile("(.*)([+-].*)i");
double real, imaginary;
Then in case 1:
然后在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);
You'll probably want to add some error handling if the user's input doesn't match the required pattern (otherwise real
and imaginary
will both be 0). 如果用户的输入与所需模式不匹配,您可能希望添加一些错误处理(否则real
和imaginary
都将为0)。
Add following method in Driver class 在Driver类中添加以下方法
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]));
}
and then Instead of firstComplex=get.nextDouble(); 然后代替firstComplex = get.nextDouble(); use firstComplex=getComplexNumber(get); 使用firstComplex = getComplexNumber(get); handle exceptions as well 处理异常
You need get real and imaginary path of complex number. 你需要得到复数的真实和想象的路径。
By making a few changes in your code I have this result: 通过对代码进行一些更改,我得到了以下结果:
Main logic is: 主要逻辑是:
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);
}
All 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");
}
}
Here is something if you have integer parts. 如果你有整数部分,这是一些东西。 Please amend for doubles. 请修改双打。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.