用c#编写一个复数类ComplexNumber并测试其功能,要求能进行复数间的基本数学运算:+

大家帮帮忙啊,谢谢了... 大家帮帮忙啊,谢谢了 展开
 我来答
书含云8y
2014-12-09 · TA获得超过469个赞
知道小有建树答主
回答量:370
采纳率:57%
帮助的人:79.3万
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fushu
{
class Complex
{
public double RealPart;//代表复数的实数部分
public double ImaginPart;//代表复数的虚数部分
public Complex()//构造函数,将复数的实部和虚部都置0。
{
this.RealPart = 0;
this.ImaginPart = 0;
}
public Complex(double r, double i)// 构造函数,形参 r 为实部的初值,i为虚部的初值。
{
this.RealPart = r;
this.ImaginPart = i;

}
public double getReal()//获得复数对象的实部;
{
return RealPart;

}
public double getImagin()//获得复数对象的虚部;
{
return ImaginPart;
}
//将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者。
public Complex complexAdd(Complex a)
{

a.RealPart = a.RealPart + this.RealPart;
a.ImaginPart = a.RealPart + this.ImaginPart;

return a;
}
// 将当前复数对象与形参复数对象相减,所得的结果仍是一个复数值,返回给此方法的调用者。
public Complex complexSub(Complex a)
{
Complex temp = new Complex();
a.RealPart = a.RealPart - temp.RealPart;
a.ImaginPart = a.RealPart -temp.ImaginPart;

return temp;
}
// 将当前复数对象与形参复数对象相乘,所得的结果仍是一个复数值,返回给此方法的调用者。
public Complex complexMulti(Complex a)
{
Complex temp = new Complex();
a.RealPart = a.RealPart * temp.RealPart;
a.ImaginPart = a.RealPart * temp.ImaginPart;

return a;
}
public static Complex operator +(Complex a, Complex b)//重载+
{
return new Complex(a.RealPart + b.RealPart, a.ImaginPart + b.ImaginPart);
}
public static Complex operator -(Complex a, Complex b)//重载-
{
return new Complex(a.RealPart - b.RealPart, a.ImaginPart - b.ImaginPart);
}
public static Complex operator *(Complex a, Complex b)//重载*
{
return new Complex(a.RealPart * b.RealPart, a.ImaginPart * b.ImaginPart);
}
public void input()
{
Console.WriteLine("请输入复数的实数部分!");
this.RealPart = double .Parse (Console.ReadLine());
Console.WriteLine("请输入复数的虚数部分!");
this.ImaginPart = double.Parse(Console.ReadLine());
Console.WriteLine("您输入的复数是:{0}",this.ToString ());
}
public override string ToString()
{
return RealPart +"+"+ImaginPart +"i";
}
}
class Program
{
static void Main(string[] args)
{
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
Console .WriteLine ("({0},{1})",complex1 .getReal(),complex1 .getImagin () );
Console.WriteLine("({0},{1})", complex2.getReal(), complex2.getImagin());
Console.WriteLine(complex1 .ToString ());
Console.WriteLine(complex2.ToString());
Complex temp = new Complex();
Console.WriteLine(temp.ToString());
temp = complex1 + complex2;
Console.WriteLine("({0})+({1})={2}", complex1.ToString(), complex2.ToString(), temp.ToString());
temp = complex1 - complex2;
Console.WriteLine("({0})-({1})={2}", complex1.ToString(), complex2.ToString(), temp.ToString());
temp = complex1 * complex2;
Console.WriteLine("({0})*({1})={2}", complex1.ToString(), complex2.ToString(), temp.ToString());
Complex c1 = new Complex();
Complex c2 = new Complex();
Complex c3 = new Complex();
c1.input();
c2.input();
c3 = c1 + c2;
Console.WriteLine("({0})+({1})={2}", c1.ToString(), c2.ToString(), c3.ToString ());
c3 = c1 - c2;
Console.WriteLine("({0})-({1})={2}", c1.ToString(), c2.ToString(), c3.ToString());
c3 = c1 * c2;
Console.WriteLine("({0})*({1})={2}", c1.ToString(), c2.ToString(), c3.ToString());
Console.ReadKey();
}
}
}
你面功能很全,按你需要的用
D_FMing
2012-05-28
知道答主
回答量:63
采纳率:0%
帮助的人:17.7万
展开全部
public class FuShu
{
public int ShiBu;
public int Xubu;

public static FuShu operator +(FuShu src, FuShu des)
{
FuShu ret=new FuShu();
ret.ShiBu=src.ShiBu+des.ShiBu;
ret.Xubu=src.Xubu+des.Xubu;
return ret;
}

public override string ToString()
{
return "The Value is: " + this.ShiBu + "+" + this.Xubu+"!";
}
}

FuShu des = new FuShu();
des.ShiBu = 4;
des.Xubu = 3;
FuShu src = new FuShu();
src.Xubu = 66;
src.ShiBu = 7;

FuShu ret = src + des;

MessageBox.Show(ret.ToString());
转来的,问题属于操作符重载问题
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式