用着这么麻烦吗?杀鸡你非得用拖拉机给它压死,至于吗

class Complex

{public :

Complex(){real=0;imag=0;}//定义结构函数

Complex(double r,double i){real=r;imag=i;}//构造函数重载

Complex comeplex_add(Complex &c2);//声明复数的加减函数

void diplay();//声明输出函数

private:

double real;

double imag;

};

Complex Complex::complex_add(Complex &c2)//这个是复数的相加减函数

{Complex c;

c.real=real+c2.real;

c.imag=imag+c2.imag;

return c;

}

void Complex ::display()

{cout <

}

你看下行不行,不行可以继续追问,呵呵,我也是新手,那个要是满意就给个最佳哈。

import java.util.*;public class ComplexTest{static class ComplexNumber{private double real,image;public ComplexNumber(){this(0.0,0.0);}public ComplexNumber(double a,double b){real=a;image=b;}public ComplexNumber add(ComplexNumber x){return new ComplexNumber(real+x.real,image+x.image);}public ComplexNumber sub(ComplexNumber x){return new ComplexNumber(real-x.real,image-x.image); }public ComplexNumber mul(ComplexNumber x){return new ComplexNumber(real*x.real-image*x.image,real*x.image+image*x.real);}public ComplexNumber div(ComplexNumber x){if(x.real==0&&x.image==0){System.out.println("无法进行除法!");return new ComplexNumber();}else return new ComplexNumber((real*x.real+image*x.image)/(x.real*x.real+x.image*x.image),(image*x.real-real*x.image)/(x.real*x.real+x.image*x.image));}public double getReal (){return real;}public double getImage (){return image;}public void show(){System.out.println(this.toString());}public String toString(){if(image<0)return ""+real+image+"i";else return ""+real+"+"+image+"i";}}static class Test{public Test(){Scanner sr=new Scanner(System.in);ComplexNumber a,b,c;try{System.out.println("请输入第一个实部和虚部:");a=new ComplexNumber(sr.nextDouble(),sr.nextDouble());System.out.println("请输入第二个实部和虚部:");b=new ComplexNumber(sr.nextDouble(),sr.nextDouble());System.out.print("第一个复数:");a.show();System.out.print("第二个复数:");b.show();c=a.add(b);System.out.print("两个复数的和:");c.show();c=a.sub(b);System.out.print("两个复数的差:");c.show();c=a.mul(b);System.out.print("两个复数的积:");c.show();c=a.div(b);System.out.print("两个复数的商:");c.show();}catch(InputMismatchException e){System.out.println("输入有误!");}}}public static void main(String[] args){new Test();}}

class Complex

{

private double a;

public double A

{

get { return a; }

set { a = value; }

}

private double b;

public double B

{

get { return b; }

set { b = value; }

}

public Complex()

{

a = 0;

b = 3;

}

public void SetNumber(double x, double y)

{

a = x;

b = y;

}

public static Complex operator +(Complex x, Complex y)

{

Complex temp=new Complex();

temp.A = x.a + y.A;

temp.B = x.B + y.B;

return temp;

}

public void Display()

{

if (b == 0)

{

Console.WriteLine("{0}", a);

}

if (a == 0 && b != 0)

{

Console.WriteLine("{0}i", b);

}

if (a != 0 && b > 0)

{

Console.WriteLine("{0}+{1}i", a, b);

}

if (a != 0 && b < 0)

{

Console.WriteLine("{0}{1}i", a, b);

}

}

}

static void Main(string[] args)

{

Complex num1=new Complex();

Complex num2=new Complex();

Complex temp = new Complex();

num1.Display();

num2.Display();

num1.SetNumber(1, 2);

num2.SetNumber(3, 4);

num1.Display();

num2.Display();

temp = num1 + num2;

Console.Write("(1+2i)+(3+4i)=");

temp.Display();

num1.SetNumber(3, 0);

num2.SetNumber(2, 3);

num1.Display();

num2.Display();

temp = num1 + num2;

Console.Write("3+(2+3i)=");

temp.Display();

}

楼上大哥那是C++啊。

这是我以前写的,可以实现加减

不过什么叫复数转换成字符串啊?

import java.io.*;

class complex{

//复数类

public float realPart;

public float imagPart;

}

public class Example extends complex{

public static String readString(){

//从键盘读取字符串

BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);

String s1="";

try{

s1=br.readLine();

}catch(IOException ex){

System.out.println(ex);

}

return s1;

}

public static float readFloat(){

//字符串转换为浮点数

return Float.parseFloat(readString());

}

public static char plus(float r1,float r2,float i1,float i2){

//实现复数相加并输出

complex sum=new complex();

sum.imagPart=i1+i2;

sum.realPart=r1+r2;

if(sum.imagPart<0){

//解决虚部为负时同时显示+ -号问题

sum.imagPart=-sum.imagPart;

System.out.println("两数相加="+sum.realPart+"-"+sum.imagPart+"i");

return 'f';

}

System.out.println("两数相加="+sum.realPart+"+"+sum.imagPart+"i");

return 'z';

}

public static char minus(float r1,float r2,float i1,float i2){

//实现复数相减并输出

complex cha=new complex();

cha.imagPart=i1-i2;

cha.realPart=r1-r2;

if(cha.imagPart<0){

//解决虚部为负时同时显示+ -号问题

cha.imagPart=-cha.imagPart;

System.out.println("两数相减="+cha.realPart+"-"+cha.imagPart+"i");

return 'f';

}

System.out.println("两数相减="+cha.realPart+"+"+cha.imagPart+"i");

return 'z';

}

public static void in(complex x){//实现从键盘输入的过程

System.out.print("

实部=");

x.realPart=Example.readFloat();

System.out.print("虚部=");

x.imagPart=Example.readFloat();

}

public static void main(String[] args) {

//主方法

complex a=new complex();

complex b=new complex();

System.out.println("输入第一个复数...");

in(a);

System.out.println("

输入第二个复数...");

in(b);

System.out.println("

");

plus(a.realPart,b.realPart,a.imagPart,b.imagPart);

minus(a.realPart,b.realPart,a.imagPart,b.imagPart);

}

}

java 定义一个复数类

: //余下的自己完成import java.util.Scanner;public class ComplexOperation { static Scanner s = new Scanner(System.in); public Complex option(Complex c1, Complex c2, String opch) { Complex r = new Complex(); if("+".equals(opch)) { r....

用JAVA定义个复数类

: public class Complex { private double x;//实部 private double y;//虚部 public Complex(){}/**构造函数 * @param x 实数部分 * @param y 虚数部分 */ public Complex(double x,double y){ super(); this.x = x; this.y = y; } /**求模 * @return 该复数的模...

用Java定义一个复数类complex,它的内部具有两个实例变量:realPart和imagPart,分别代表复数的实部和虚部 -

: import java.util.Scanner; public class Complex { private int realPart; private int imagPart; public int getRealPart() { return realPart; } public void setRealPart(int realPart) { this.realPart = realPart; } public int getImagPart() { return imagPart; } public void ...

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

: public class ComplexDemo { // main方法 public static void main(String[] a) { Complex b = new Complex(2, 5); Complex c = new Complex(3, -4); System.out.println(b + "+" + c + "=" + b.add(c)); System.out.println(b + "-" + c + "=" + b.minus(...

用java 编写一个复数类 -

: public class Complex { protected int a; protected int b; public Complex(int a, int b) { this.a = a; this.b = b; } public String toString() { return this.a + " + " + this.b + "i"; } public static Complex addition(Complex complex1, Complex complex2) { int a =...

用java定义一个复数类,能实现加减,获得实部和虚部,将复数转换成字符串这几个方法

: 楼上大哥那是C++啊.这是我以前写的,可以实现加减不过什么叫复数转换成字符串啊?import java.io.*;class complex{ //复数类 public float realPart; public float imagPart;}public class Example extends complex{ public static String readString()...

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加... : public class Complex{ public int RealPart; public int ImaginPart; public Complex(){ this.RealPart = 0; this.ImaginPart = 0; } public Complex(int r, int i){ this.RealPart = r; this.ImaginPart = i } public Complex complexAdd(Complex a){ this.RealPart += a....

用java构造一个复数类

: import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.*; import javax.swing.WindowConstants;public class MainFrame extends JFrame implements ActionListener { private ...

JAVA编程 实现一个复数类 急!! -

: import java.io.*; class complex{ //复数类 public float realPart; public float imagPart; } public class Main extends complex{ public static String readString(){ //从键盘读取字符32313133353236313431303231363533e59b9ee7ad...

java复数类求模_用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算 用java编写一个复数类...相关推荐

  1. C语言(CED)编写一个程序,求两个字符之间的加减运算。

    (请先看置顶博文)https://blog.csdn.net/GenuineMonster/article/details/104495419 复习C语言,不同的心境遇到了不同的问题: 问题: 编写一 ...

  2. 分数加减运算(java实现)leecode网题目

    input:-1/2+1/2+1/3 out:1/3package 每日算法题;import java.util.Arrays;public class 分数加减运算 {public String f ...

  3. java实现表达式求值_如何编写一个高效的Java表达式求值程序

    虽然,这个题目是有一点夺人眼球,但我真实这么做了(关是以否信任基准测试效果,这是其他一个话题). 所以,上周我一贯在找一个小型.适用的竞赛争辩数学表达式的类库.有功夫我在stackoverflow上看 ...

  4. java时间差的百分之二十,Java对日期Date类进行加减运算、年份加减月份加减、时间差等等...

    实现代码一: import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public c ...

  5. java当前月份减一个月_Java对日期Date类进行加减运算、年份加减月份加减、时间差等等...

    实现代码一: import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public c ...

  6. java 加减运算_JAVA日期加减运算 | 学步园

    1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); Sys ...

  7. java 日期 减法_JAVA日期加减运算

    1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); Sys ...

  8. java 时分秒加减_JAVA日期加减运算

    public static Date getBeforeMonth(Date date,int months) { Calendar calendar = Calendar.getInstance() ...

  9. node-rsa 加减密与java rsa加减密 互调 相互加减密

    注意:每次rsa加密产生的密文是不一样的,不同的密文解密可以得到相同的内容. 注意:每次rsa加密产生的密文是不一样的,不同的密文解密可以得到相同的内容. 1.公钥与私钥的生成 使用node-rsa来 ...

  10. 同指数幂相减公式_同底指数加减运算法则

    直接是无法相加减的,可以将指数高的那个数分成两个同底指数的乘积,按照合并同类项的方式进行加减.比如22+2^1的3=22+2^1×22=(1+2^1)×22. 乘除法则 乘法:底数不变,指数相加:除法 ...

最新文章

  1. [转载]PhotoShop性能优化
  2. 图的基本操作实现(数据结构实验)
  3. UEditor 百度富文本编辑器 .Net实例
  4. leetcode: Roman to Integer
  5. 数据结构实验之栈:行编辑器
  6. 上班第一天,Myeclipse 2014上SVN部署以及maven 配置
  7. 网易传媒回应“变相裁员 ”说法:假消息,将提起诉讼
  8. c语言divide error,Python numpy.corrcoef()RuntimeWarning:在true_divide中遇到无效值c / = stddev [:,None]...
  9. kerberos验证_SQL Server中的服务主体名称和Kerberos身份验证概述
  10. [转]ASP.NET 页生命周期概述
  11. 基桩测试软件,智博联ZBL-U5700/5600机内软件测桩模块更新软件
  12. python调用nmap扫描全端口_Python-通过调用Nmap来进行端口扫描
  13. 教你免费使用刷脸支付设备,蜻蜓二代返还政策解析
  14. 2017计算机基础教学大纲,《计算机应用基础》教学大纲
  15. 蓝桥杯---史丰收速算
  16. ubuntu 安裝deb_Ubuntu离线安装deb包和依赖
  17. 小游戏制作-其他系列-数独
  18. vbs脚本和windows定时任务实现qq消息表情包定时发送
  19. 自学Java day17 jvav网络编程 从jvav到架构师
  20. Linux系统的PAM模块认证文件含义说明总结

热门文章

  1. 用计算机表白我不喜欢你了,【北邮表白墙】与卿初相识,犹如故人归。表白计算机院的苏苏,喜欢你是我最正确的选择?...
  2. mc小刘yeah粉丝网
  3. DirectX11编程11 Blend混合
  4. 7z文件linux怎么解压,Ubuntu 12.04下解压7z文件
  5. PHPExcel浏览器输出Excel2007出错
  6. 一文搞懂基因融合(gene fusion)的定义、产生机制及鉴定方法
  7. Android设备图标显示模糊问题
  8. 简单粗暴日文键盘布局改为其他语言键盘布局
  9. 一份新的lilypond谱子,能设置页边距和设置换页符了
  10. 凑数算法 c语言,算法题解 - 牛客编程巅峰赛S1第4场 - 黄金钻石组