C#编程:定义一个复数类,实现复数的简单加法运算,并能显示结果。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#编程:定义一个复数类,实现复数的简单加法运算,并能显示结果。相关的知识,希望对你有一定的参考价值。
求高手解答
参考技术A class fspublic double a;//实部
public double b;//虚部
//加法
public fs add(fs fs1)
fs fs_temp = new fs();
fs_temp.a = this.a + fs1.a;
fs_temp.b = this.b + fs1.b;
return fs_temp;
//减法
public fs jf(fs fs1)
fs fs_temp = new fs();
fs_temp.a = this.a - fs1.a;
fs_temp.b = this.b - fs1.b;
return fs_temp;
public string ToString()
if(b==0)
return "" + a;
else if (a == 0)
return b + "i";
else
return a + "+" + b + "i";
本回答被提问者采纳
用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算
复数具有实部和虚部两部分,如2+5i就是一个复数,其中2为实部,5i为虚部;两复数相加时, 实部与实部相加, 虚部与虚部相加。两复数相减时,实部与实部相减, 虚部与虚部相减。
如(2+5i)+(3-4i)=5+1i
(2+5i)-(3-4i)=-1+9i
现在要求定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算,在main()函数中测试该类。
我需要全部代码,急用,谢谢了~
// 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(c));
System.out.println(b + "*" + c + "=" + b.multiply(c));
System.out.println(b + "/" + c + "=" + b.divide(c));
// Complex类
class Complex
private double m;// 实部
private double n;// 虚部
public Complex(double m, double n)
this.m = m;
this.n = n;
// add
public Complex add(Complex c)
return new Complex(m + c.m, n + c.n);
// minus
public Complex minus(Complex c)
return new Complex(m - c.m, n - c.n);
// multiply
public Complex multiply(Complex c)
return new Complex(m * c.m - n * c.n, m * c.n + n * c.m);
// divide
public Complex divide(Complex c)
double d = Math.sqrt(c.m * c.m) + Math.sqrt(c.n * c.n);
return new Complex((m * c.m + n * c.n) / d, Math.round((m * c.n - n * c.m) / d));
public String toString()
String rtr_str = "";
if (n > 0)
rtr_str = "(" + m + "+" + n + "i" + ")";
if (n == 0)
rtr_str = "(" + m + ")";
if (n < 0)
rtr_str = "(" + m + n + "i" + ")";
return rtr_str;
参考技术A 定义2个成员变量,一个放实数,一个放虚数,再定义一个caculate方法,传进一个complex参数,计算后返回一个complex结果对象。
package test;
public class Complex
private int shishu;
private int xushu;
public Complex(int shishu, int xushu)
this.shishu = shishu;
this.xushu = xushu;
public Complex add(Complex complex)
return new Complex(this.shishu + complex.getShishu(), this.xushu + complex.getXushu() );
public Complex cut(Complex complex)
return new Complex(this.shishu - complex.getShishu(), this.xushu - complex.getXushu() );
public String toString()
if (this.xushu == 0)
return "" + this.shishu;
else if (this.xushu > 0)
return this.shishu + "+" + this.xushu + "i";
else
return this.shishu + "" + this.xushu + "i";
/**
* @param args
*/
public static void main(String[] args)
Complex a = new Complex(5,2);
Complex b = new Complex(4,3);
System.out.println(a);
System.out.println(b);
System.out.println(a.add(b));
System.out.println(a.cut(b));
public int getShishu()
return shishu;
public int getXushu()
return xushu;
运行结果:
5+2i
4+3i
9+5i
1-1i 参考技术B 用着这么麻烦吗?杀鸡你非得用拖拉机给它压死,至于吗
以上是关于C#编程:定义一个复数类,实现复数的简单加法运算,并能显示结果。的主要内容,如果未能解决你的问题,请参考以下文章
用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算
用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算