2个回答
展开全部
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();
}
}
}
你面功能很全,按你需要的用
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();
}
}
}
你面功能很全,按你需要的用
展开全部
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());
转来的,问题属于操作符重载问题
{
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());
转来的,问题属于操作符重载问题
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询