请教,用C#写一个复数类!

bufan2000 2007-10-23 02:09:11
写一个复数类,实现
(1)构造函数重载(三种:缺省,实部+虚部,幅角+模)
(2)实现+,-,×,/运算,尤其是注意/运算分母不等于零,包含和整数,和浮点数,和复数相乘的情况。
(3)定义>,<,==,!=等
(4)实现实数到复数的转换(显式,隐式),实现复数到实数的转换(显式,隐式),如果可以的话,如果不可以请显示错误信息。
请各位大侠,帮帮忙!!!
...全文
801 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bufan2000 2007-10-24
  • 打赏
  • 举报
回复
实现实数到复数的转换(显式,隐式),实现复数到实数的转换(显式,隐式),如果可以的话,如果不可以请显示错误信息。

这一步怎么实现呢?
Apollo_pl 2007-10-23
  • 打赏
  • 举报
回复
public struct Complex
{
public int real;
public int imaginary;

public Complex(int real, int imaginary) //constructor
{
this.real = real;
this.imaginary = imaginary;
}

public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

// Override the ToString() method to display a complex number in the traditional format:
public override string ToString()
{
return (System.String.Format("{0} + {1}i", real, imaginary));
}
}

class TestComplex
{
static void Main()
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);

// Add two Complex objects through the overloaded plus operator:
Complex sum = num1 + num2;

// Print the numbers and the sum using the overriden ToString method:
System.Console.WriteLine("First complex number: {0}", num1);
System.Console.WriteLine("Second complex number: {0}", num2);
System.Console.WriteLine("The sum of the two numbers: {0}", sum);
}
}
bufan2000 2007-10-23
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace 复数
{

public struct Complex
{
public double real;
public double imaginary;
public Complex(double real, double imaginary)

{ this.real = real;
this.imaginary = imaginary;

}
public static Complex operator +(Complex c1, Complex c2) //重载"+"运算符
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static Complex operator -(Complex c1, Complex c2) //重载"-"运算符
{
return new Complex(c1.real -c2.real, c1.imaginary - c2.imaginary);
}
public static Complex operator *(Complex c1, Complex c2) //重载"*"运算符
{
return new Complex((c1.real*c2.real-c1.imaginary*c2.imaginary),( c1.real*c2.imaginary+ c2.real*c1.imaginary));
}
public static Complex operator / (Complex c1, Complex c2)//重载"/"运算符
{

return new Complex((c1.real * c2.real + c1.imaginary * c2.imaginary) / (c2.real * c2.real + c2.imaginary * c2.imaginary), (c2.real * c1.imaginary - c1.real * c2.imaginary) / (c2.real * c2.real + c2.imaginary * c2.imaginary));

}
public static bool operator > (Complex c1,Complex c2)//重载">"运算符
{
if(c1.real>c2.real)
return true;
else return false;}

public static bool operator < (Complex c1,Complex c2)
{
if(c1.real<c2.real)
return true;
else return false;
}

public static bool operator ==(Complex c1, Complex c2)//重载"<"运算符
{
if ((c1.real == c2.real) && (c1.imaginary == c2.imaginary))
return true;
else return false;
}

public static bool operator !=(Complex c1,Complex c2)
{
if((c1.real==c2.real)&&(c1.imaginary==c2.imaginary))
return false;
else return true;
}
public override string ToString( )
{
return (String.Format("{0} + {1} I", real, imaginary));
}


/// <summary>
/// 应用程序的主入口点。
/// </summary>

public static void Main( )
{
Complex value1= new Complex(4,3);
Complex value2 = new Complex(5,4);
Complex sum = value1 + value2;
Console.WriteLine("第一个复数是: {0}",value1);
Console.WriteLine("第二个复数是: {0}", value2);
Console.WriteLine("两个复数的和是: {0}", sum);




}
}
}
这是自己编的,怎么也运行不出来,不知道问题出在哪里??????????
bufan2000 2007-10-23
  • 打赏
  • 举报
回复
还有大侠在指点一下吗?
ansili521 2007-10-23
  • 打赏
  • 举报
回复
好贴
支持ing....
bufan2000 2007-10-23
  • 打赏
  • 举报
回复

(4)实现实数到复数的转换(显式,隐式),实现复数到实数的转换(显式,隐式),如果可以的话,如果不可以请显示错误信息。

这一步怎么实现呢?还是不会啊
hanshufan 2007-10-23
  • 打赏
  • 举报
回复
学习
MMNNQQWW 2007-10-23
  • 打赏
  • 举报
回复
帮顶一下,这个应该不难,不过得花点时间,我现在做项目遇到问题了暂时没空.如果问题解决了帮你写出来
hooo 2007-10-23
  • 打赏
  • 举报
回复
public struct Complex
{
public int real;
public int imaginary;

public Complex(int real, int imaginary) //constructor
{
this.real = real;
this.imaginary = imaginary;
}

// Declare which operator to overload (+),
// the types that can be added (two Complex objects),
// and the return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

// Override the ToString() method to display a complex number in the traditional format:
public override string ToString()
{
return (System.String.Format("{0} + {1}i", real, imaginary));
}
}

class TestComplex
{
static void Main()
{
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);

// Add two Complex objects through the overloaded plus operator:
Complex sum = num1 + num2;

// Print the numbers and the sum using the overriden ToString method:
System.Console.WriteLine("First complex number: {0}", num1);
System.Console.WriteLine("Second complex number: {0}", num2);
System.Console.WriteLine("The sum of the two numbers: {0}", sum);
}
}

//一个简单的复数类。。
bufan2000 2007-10-23
  • 打赏
  • 举报
回复
谢谢过楼上的了,去看了一下,不过都是用VB编的,我以前没有学过VB,不知道用C#怎么编写?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧