1、 程序包含的模块
a) 用户输入
b) 输入检测
c) 字符串处理
正则表达式:(以下形式为合法输入) a+bi a-bi -a+bi -a-bi a -a bi -bi(支持浮点数)
匹配的正则表达式:[-]?\\d*\\.?\\d*[\\+|-]?\\d*\\.?\\d*i?
d) 复数计算
2、源码
package cpxnum_calculator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class CpxNum {
double _real;//实部
double _imag;//虚部
CpxNum() {
}
CpxNum(String str) {//初始化复数
//※这里需要复制一份字符串,以免在对符号进行提取的时候损伤了原来的字符串
String string = str;
int indexfirst = string.indexOf("-");
int indexlast = string.lastIndexOf("-");
int count = Tools.fuhaocount(string);
boolean start_fu = string.startsWith("-");//复数是否以-号开始
boolean end_i = string.endsWith("i");//复数是否有虚部
String strings[] = str.split("\\+|-|i");//分离实部和虚部
if (end_i == false) {//只有实部,没有虚部 a -a
_real = Double.parseDouble(string);
} else {//有虚部
if (count == 2) {//有两个符号,有实部和虚部
if (start_fu == true) {//实部为负数 -a+bi -a-bi
_real = -Double.parseDouble(strings[1]);
if (indexlast != indexfirst) {//虚部为负数 -a-bi
_imag = -Double.parseDouble(strings[2]);
} else {//虚部为正数 -a+bi
_imag = Double.parseDouble(strings[2]);
}
}
} else {//一个符号,有虚部
if (start_fu == true) {//开头为'-' -bi
_imag = -Double.parseDouble(strings[1]);
} else {// a+bi a-bi bi
if (strings.length == 1) {//bi
_imag = Double.parseDouble(strings[0]);
} else {//a+bi a-bi
if (indexlast != -1) {//a-bi
_real = Double.parseDouble(strings[0]);
_imag = -Double.parseDouble(strings[1]);
} else {//a+bi
_real = Double.parseDouble(strings[0]);
_imag = Double.parseDouble(strings[1]);
}
}
}
}
}
}
void print() {
//注意:此处若漏掉空格,编译器会认为是相加的数值
System.out.println(this._real + " + " + this._imag + "i");
}
}
class CpxCalculator {
public CpxNum cplus(CpxNum... c) {
CpxNum cpxNum = new CpxNum();
for (int i = 0; i < c.length; i++) {
cpxNum._real = cpxNum._real + c[i]._real;
cpxNum._imag = cpxNum._imag + c[i]._imag;
}
System.out.print("以上" + c.length + "个复数相加的结果为:");
cpxNum.print();
return cpxNum;
}
public CpxNum cmilus(CpxNum... c) {
CpxNum cn = new CpxNum();
cn._real = c[0]._real;
cn._imag = c[0]._imag;
for (int i = 0; i < c.length - 1; i++) {
cn._real = cn._real - c[i+1]._real;
cn._imag = cn._imag - c[i+1]._imag;
}
System.out.print("以上" + c.length + "个复数相减的结果为:");
cn.print();
return cn;
}
public CpxNum cmultiply(CpxNum... c) {
CpxNum cn = new CpxNum();
double real = c[0]._real;
cn._real = c[0]._real;
cn._imag = c[0]._imag;
for (int i = 0; i < c.length - 1; i++) {
// 这里不能连在一起算,前一步的结果会影响到后面
// cn._real = cn._real * c[i + 1]._real - cn._imag * c[i + 1]._imag;
// cn._imag = cn._real * c[i + 1]._imag + cn._imag * c[i + 1]._real;
real = real * c[i + 1]._real - cn._imag * c[i + 1]._imag;
cn._imag = cn._real * c[i + 1]._imag + cn._imag * c[i + 1]._real;
}
System.out.print("以上" + c.length + "个复数相乘的结果为:");
cn._real = real;
cn.print();
return cn;
}
public CpxNum cdivide(CpxNum... c) {
CpxNum cn = new CpxNum();
double real = c[0]._real;
cn._real = c[0]._real;
cn._imag = c[0]._imag;
for (int i = 0; i < c.length - 1; i++) {
real = (cn._real * c[i + 1]._real + cn._imag * c[i + 1]._imag) /
(c[i + 1]._real * c[i + 1]._real + c[i + 1]._imag * c[i + 1]._imag);
cn._imag = (cn._imag * c[i + 1]._real - cn._real * c[i + 1]._imag) /
(c[i + 1]._real * c[i + 1]._real + c[i + 1]._imag * c[i + 1]._imag);
}
System.out.print("以上" + c.length + "个复数相除的结果为:");
cn._real = real;
cn.print();
return cn;
}
}
class CpxNumTest {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
List<String> strlist = new LinkedList<>();
CpxCalculator calculator = new CpxCalculator();
int flag = 1;
System.out.println("当输入0的时候结束输入");
System.out.print("是否开始使用复数计算器(1/0):");
flag = reader.nextInt();
if (flag == 0) {
System.out.println("程序已停止运行");
return;
}
while (flag != 0) {
System.out.print("请输入以i为虚部符号的一个复数:");
String inputstr = reader.next();
while (Tools.inputtest(inputstr) == false) {//复数检测
System.out.print("输入的复数不合格,请重新输入:");
inputstr = reader.next();
}
strlist.add(inputstr);
System.out.print("是否需要继续输入复数(1/0):");
flag = reader.nextInt();
if (flag == 0) {
System.out.println("您刚刚输入的复数为:");
break;
}
}
CpxNum[] cpxNums = new CpxNum[strlist.size()];
//将刚刚输入的字符串生成复数对象
for (int i = 0; i < strlist.size(); i++) {
cpxNums[i] = new CpxNum(strlist.get(i));
cpxNums[i].print();
}
calculator.cplus(cpxNums);
calculator.cmilus(cpxNums);
calculator.cmultiply(cpxNums);
calculator.cdivide(cpxNums);
}
}
3、结果