1.运用java 编写一个复数类,有实部和虚部,并实现复数的加减乘除运算?public class ComplexNumber {
/**
* @param args
*/
int shi,xu;//复数的实部和虚部
public ComplexNumber(int n,int ni){
shi = n;
xu = ni;
}
public void ComplexShow(){
String output = "";
output =shi;
if(xu=0){
output =" ";
}
output =xu;
output ="i";
System.out.println(output);
}
public void ComplexShow1(){//不要换行
String output = "";
output =shi;
if(xu=0){
output =" ";
}
output =xu;
output ="i";
System.out.print(output);
}
public static void ComplexAdd(ComplexNumber x1,ComplexNumber x2){//实现两个复数相加
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shix2.shi;
cn.xu = x1.xux2.xu;
cn.ComplexShow();
}
public static void ComplexMinus(ComplexNumber x1,ComplexNumber x2){//实现两个复数相减,第一个数减第二个数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi - x2.shi;
cn.xu = x1.xu - x2.xu;
cn.ComplexShow();
}
public static void ComplexMultiply(ComplexNumber x1,ComplexNumber x2){//实现两个复数相乘
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2.shi - x1.xu * x2.xu;
cn.xu = x1.xu * x2.shix2.xu * x1.shi;
cn.ComplexShow();
}
public static void ComplexDivide(ComplexNumber x1,ComplexNumber x2){//实现两个复数相除 , 第一个数除以第二个数
ComplexNumber x2_gong = new ComplexNumber(x2.shi,0-x2.xu);//求被除数的共轭复数
ComplexNumber cn = new ComplexNumber(0, 0);//将两个复数相加等于cn
cn.shi = x1.shi * x2_gong.shi - x1.xu * x2_gong.xu;//x1/x2,求分子实部
cn.xu = x1.xu * x2_gong.shix2_gong.xu * x1.shi;//x1/x2,求分子虚部
int fenMu = x2.shi * x2.shix2.xu * x2.xu;
if(fenMu!=0){
System.out.print("(");
cn.ComplexShow1();
System.out.print(")");
System.out.println("/" fenMu);
}
else
System.out.println("分母为零,无法相除");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ComplexNumber cn = new ComplexNumber(-1, -1);//初始化复数
cn.ComplexShow();//显示复数
ComplexNumber c1 = new ComplexNumber(-1, -1);
ComplexNumber c2 = new ComplexNumber(1, 1);
System.out.print("加:");
ComplexAdd(c1, c2);
System.out.print("减:");
ComplexMinus(c1, c2);
System.out.print("乘:");
ComplexMultiply(c1, c2);
System.out.print("除:");
ComplexDivide(c1, c2);//自己化简
}
}
我编写了一个用于计算两复数的和以及乘积的java程序,如何定义一个result()函数专门用于输出结果?前面的回答是对的,有几个地方需要修改一下:
1、Add 和 Multiply方法的返回类型需要改成:String
2、result 方法要用static修饰符,即 public static void result(Complex fushu1,Complex fushu2) ,result作为类的静态方法 。
3、main方法也要用static修饰符,即 public static void main(String[] args)
完整代码:
public class Complex {
/**
* @param args
*/
private double r;
private double i;
Complex()
{
this(0,0);
}
Complex(double r,double i){
this.r=r;
this.i=i;
}
public /*void*/String Add(Complex p)
{
Complex result=new Complex();
result.r=this.r p.r;
result.i=this.i p.i;
System.out.println("1 2i");
System.out.println("3 4i");
//System.out.println("加法结果:" result.r " " result.i "i");
return result.r " " result.i;
}
public /*void*/String Multiply(Complex q)
{
Complex result=new Complex();
result.r=this.r*q.r-this.i*q.i;
result.i=this.i*q.r this.r*q.i;
//System.out.println("乘法结果:" result.r " " result.i "i");
return result.r " " result.i;
}
public static void result(Complex fushu1,Complex fushu2)
{
System.out.println("加法结果:" fushu1.Add(fushu2));
System.out.println("乘法结果:" fushu1.Multiply(fushu2));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
//fushu1.Add(fushu2);
//fushu1.Multiply(fushu2);
result(fushu1,fushu2);
}
}
编写Java Applet程序,实现复数矩阵的加法,乘法运算import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class fushu extends JFrame{
DComplexNumber x=new DComplexNumber();
DComplexNumber y=new DComplexNumber();
//窗体布局
JLabel Lab1=new JLabel("实部");
JLabel Lab2=new JLabel("虚部");
JLabel Lab3=new JLabel();
JTextField text1=new JTextField(8);
JTextField text2=new JTextField(8);
JButton btn1=new JButton("设置第一个复数");
JButton btn2=new JButton("设置第二个复数");
JButton btn3=new JButton(" ");
JButton btn4=new JButton("-");
public fushu(){
setTitle("复数运算");
Container contentPane=this.getContentPane();
contentPane.setLayout(new GridLayout(4,1));
JPanel panel1=new JPanel();
panel1.add(Lab1);
panel1.add(text1);
panel1.add(btn1);
JPanel panel2=new JPanel();
panel2.add(Lab2);
panel2.add(text2);
panel2.add(btn2);
JPanel panel3=new JPanel();
panel3.add(btn3);
panel3.add(btn4);
JPanel panel4=new JPanel();
panel4.add(Lab3);
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
contentPane.add(panel4);
registerEventHandlers();
this.pack();
}
//为按钮注册事件
private void registerEventHandlers() {
btndiyifuActionEventHander diyifushu=new btndiyifuActionEventHander();
btn1.addActionListener(diyifushu);
btndierfuActionEventHander dierfushu=new btndierfuActionEventHander();
btn2.addActionListener(dierfushu);
btnjiaActionEventHander jia=new btnjiaActionEventHander();
btn3.addActionListener(jia);
btnjianActionEventHander jian=new btnjianActionEventHander();
btn4.addActionListener(jian);
}
public static void main(String[] args){
fushu fushuys=new fushu();
fushuys.setVisible(true);
}
//第一复数按钮事件
private class btndiyifuActionEventHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
x=new DComplexNumber(Integer.parseInt(text1.getText()),Integer.parseInt(text2.getText()));
text1.setText("");
text2.setText("");
Lab3.setText("第一个复数是" x.Ttostring());
}
}
//第二复数按钮事件
private class btndierfuActionEventHander implements ActionListener{
public void actionPerformed(ActionEvent e){
y=new DComplexNumber(Integer.parseInt(text1.getText()),Integer.parseInt(text2.getText()));
text1.setText("");
text2.setText("");
Lab3.setText("第二个复数是" y.Ttostring());
}
}
//加按钮事件
private class btnjiaActionEventHander implements ActionListener{
public void actionPerformed(ActionEvent e) {
ComplexNumber f=new ComplexNumber();
f=DComplexNumber.he(x,y);
Lab3.setText(x.Ttostring() " " y.Ttostring() "=" f.Ttostring());
}
}
//减按钮事件
private class btnjianActionEventHander implements ActionListener{
public void actionPerformed(ActionEvent e){
DComplexNumber f=new DComplexNumber();
f=DComplexNumber.cha(x,y);
Lab3.setText(x.Ttostring() "-" y.Ttostring() "=" f.Ttostring());
}
}
}
//创建ComplexNumber类
public class ComplexNumber{
public int real;
public int imaginary;
publicComplexNumber(){
}
publicComplexNumber(int a,int b){
real=a;
imaginary=b;
}
【复数相乘java代码 java复数的加减】 public int getReal(){
return real;
}
public void setReal(int mReal){
real=mReal;
}
public int getImaginary(){
return imaginary;
}
public void setImaginary(int mImaginary){
imaginary=mImaginary;
}
public static ComplexNumber he(ComplexNumber X,ComplexNumber Y){
ComplexNumber Complex=new ComplexNumber();
Complex.real=X.real Y.real;
Complex.imaginary=X.imaginary Y.imaginary;
return Complex;
}
public String Ttostring(){
return ("(" real (imaginary0?"-" imaginary*(-1):" " imaginary) "i" ")");
}
}
//创建DComplexNumber类
public class DComplexNumber extends ComplexNumber{
publicDComplexNumber(){
}
publicDComplexNumber(int x,int y){
this.real=x;
this.imaginary=y;
}
public static DComplexNumber cha(DComplexNumber X,DComplexNumber Y){
DComplexNumber c=new DComplexNumber();
c.real=X.real-Y.real;
c.imaginary=X.imaginary-Y.imaginary;
return c;
}
}
如何用java程序编写输入两个复数,然后进行那个加减乘,最后显示结果 。例如:ai bci d
加法:int a,b,c,d;int x,y;x=a c;y=b d
结果就为:xi y这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
减法: int a,b,c,d;int x,y;x=a-c;y=b-d
结果就为:xi y这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
乘法:int a,b,c,d;int x,y;
x=a*d b*cy=b*d-a*c
结果就为:xi y这很简单 再输出的时候要变为字符串型 这样你就可以再x后加个i 形成xi的形式了
java 中实现复数的加减(1):具体代码(附注释)
复数类:
public class Complex {
private float shibu;
private float xubu;
Complex()
{this(0,0);
}
Complex(float shibu,float xubu){
this.shibu=shibu;
this.xubu=xubu;
}
public void Add(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu p.shibu;
result.xubu=this.xubu p.xubu;
System.out.print("加法结果为:" result.shibu " " result.xubu "i");
}
public void Sub(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu-p.shibu;
result.xubu=this.xubu-p.xubu;
System.out.print("加法结果为:" result.shibu " " result.xubu "i");
}
public void Mul(Complex p)
{
Complex result=new Complex();
result.shibu=this.shibu*p.shibu-this.xubu*p.xubu;
result.xubu=this.shibu*p.xubu p.shibu*this.xubu;
System.out.print("乘法结果为:" result.shibu " " result.xubu "i");
}
public static void main(String[] args) {
Complex fushu1=new Complex(1,2);
Complex fushu2=new Complex(3,4);
fushu1.Add(fushu2);
fushu1.Sub(fushu2);
fushu1.Mul(fushu2);
}
}
(2):提供一个例子:
源代码:
import java.io.*;
public class Book{
double sb;
double xb;
Book(double x,double y){
this.sb=x;
this.xb=y;
}
Book(){
}
public static void main(String args[]){
System.out.println("请输入数据:");
double a=0;
double b=0;
double c=0;
double d=0;
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入第一个复述的实部:");
try{
s = in.readLine();
a=Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第一个复述的虚部:");
try{
s = in.readLine();
b =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的实部:");
try{
s = in.readLine();
c =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
System.out.println("请输入第二个复述的虚部:");
try{
s = in.readLine();
d =Double.parseDouble(s);
}
catch(IOException e)
{ System.out.println("抛掷异常");}
Book h;
h=new Book(a,b);
Book j;
j=new Book(c,d);
System.out.println("您输入的一个数为:");
toString(h);
System.out.println("您输入的二个数为:");
toString(j);
Book k;
k=new Book();
char z='y';
do{
System.out.println("请选择您要进行的计算:");
System.out.println("1 :进行加法运算");
System.out.println("2 :进行减法运算");
System.out.println("3 :进行修改");
System.out.println("4 :进行乘法运算");
System.out.println("5 :进行除法运算");
System.out.println("6 :查看修改结果");
int i=0;
try{
i= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
switch(i)
{
case 1:
k.sb=jia(h.sb,j.sb);
k.xb=jia(h.xb,j.xb);
System.out.println("计算结果的实部为:" k.sb);
System.out.println("计算结果的虚部为:" k.xb);
toString(k);
break ;
case 2:
k.sb=jian(h.sb,j.sb);
k.xb=jian(h.xb,j.xb);
System.out.println("计算结果的实部为:" k.sb);
System.out.println("计算结果的虚部为:" k.xb);
toString(k);
break ;
case 3:
System.out.println("请输入您要修改哪个实数:");
int l=0;
try{
l= Integer.parseInt(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
if(l==1)
{
h.xiugais(h);
h.xiugaix(h);
}
else
{
xiugais(j);
xiugaix(j);
}
break ;
case 4:
double f=0;
double e=0;
f=cheng(h.sb,j.sb) cheng(h.xb,j.xb);
e=cheng(h.sb,j.xb) cheng(h.xb,j.sb);
k.sb=(double)(Math.round(f*100)/100.0);
k.xb=(double)(Math.round(e*100)/100.0);
System.out.println("计算结果的实部为:" k.sb);
System.out.println("计算结果的虚部为:" k.xb);
toString(k);
break ;
case 5:
double chushu=cheng(j.sb,j.sb)-cheng(j.xb,-j.xb);
double beichushus=jian(cheng(h.sb,j.sb),cheng(h.xb,-j.xb));
double beichushux=jia(cheng(h.sb,-j.xb),cheng(h.xb,j.sb));
k.sb=chu(beichushus,chushu);
k.xb=chu(beichushux,chushu);
System.out.println("计算结果的实部为:" k.sb);
System.out.println("计算结果的虚部为:" k.xb);
toString(k);
break ;
case 6:
System.out.println("修改后的结果为:");
System.out.println("第一个复数:" toString(h));
System.out.println("第二个复数:" toString(j));
break ;
}
System.out.println("请问您是否还要继续 y/n:");
try{
z=(char)System.in.read();
System.in.skip(2); //忽略回车换行
}
catch(IOException e){}
} while(z=='y');
}
public static double gets(Book a){
return a.sb;
}
public static double getx(Book b){
return b.xb;
}
public static double xiugais(Book a)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的实部:");
double m=0;
try{
m= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
a.sb=m;
System.out.println("修改成功:");
return 0;
}
public static double xiugaix(Book b)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入您要修改的虚部:");
double n=0;
try{
n= Double.parseDouble(in.readLine());
}
catch(IOException e)
{ System.out.println("抛掷异常");}
b.xb=n;
System.out.println("修改成功:");
return 0;
}
public static double jia(double a,double b)//
{
double c=0;
c=a b;
System.out.println("加法成功:");
return c ;
}
public static double jian(double a,double b)
{
double c=0;
c=a-b;
System.out.println("减法成功:");
return c;
}
public static double cheng(double a,double b)
{
double c=0;
c=a*b;
System.out.println("乘法成功:");
returnc;
}
public static double chu(double a,double b)
{
double d=0;
double c=0;
d=a/b;
c=(double)(Math.round(d*100)/100.0);
System.out.println("除法成功:");
return c ;
}
publicstatic double toString(Book a){
System.out.println("结果为:" a.sb " " a.xb "*i");
return 0;
}
}
(3)测试结果截图:
用java设计一个MyComplex,该类可完成复数的加减乘除等运算功能 。Java程序:
public class Test31 {
public static void main(String[] args) {
MyComplex m1 = new MyComplex(3.4, 8.0000002);
MyComplex m2 = new MyComplex(3.4, 8.0000002);
MyComplex m3 = new MyComplex(4.4,-8.8999998);
MyComplex m4 = m1.Add(m3);
MyComplex m5 = m1.Subtract(m3);
System.out.println("m1=m2="(m1==m2));
System.out.println("m1.equals(m2)="(m1.equals(m2)));
System.out.println("m4="m4.toString());
System.out.println("m5="m5.toString());
}
}
class MyComplex{
protected double real;//实部
protected double image;//虚部
public MyComplex(){
}
public MyComplex(double real, double image){
this.real = real;
this.image = image;
}
public MyComplex Add(MyComplex num){
return new MyComplex(this.realnum.real, this.imagenum.image);
}
public MyComplex Subtract(MyComplex num){
return new MyComplex(this.real - num.real, this.image - num.image);
}
public MyComplex Product(MyComplex num){
return new MyComplex(this.real * num.real - this.image * num.image, this.real * num.imagethis.image * num.real);
}
public MyComplex Divide(MyComplex num){
return new MyComplex((this.real * num.realthis.image * num.image)/(num.real*num.real num.image*num.image), (this.image * this.image - this.real * this.real)/(num.real*num.real num.image*num.image));
}
public boolean equals(MyComplex num){
return (this.real==num.realthis.image==num.image) ? true : false;
}
public String toString(){
if(real == 0)
if(image == 0)
return "0";
else if(image0)
return image"i";
else
return "-"image"i";
else
if(image == 0)
return real"";
else if(image0)
return real" "image"i";
else
return realimage"i";
}
}
关于复数相乘java代码和java复数的加减的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 电脑模拟手机游戏推荐免费,电脑游戏用手机玩的模拟器
- 如何推广电影,如何推广电影视频素材
- 什么是cpu倍频,cpu倍频是哪个选项
- 拍摄属于什么合同,拍摄属于什么合同
- 培训vb.net 培训美容学校
- 飞翔物流有限公司使用erp系统,飞翔物流官网
- 虚拟显卡怎么用,虚拟显卡设置方法
- 现在网络游戏比较火,现在网络最火的游戏
- python关键字函数 python关键字用法