编程中复数
Ⅰ C++编程,定义一个复数类
//complex类
#include<iostream.h>
#include<math.h>
class complex{
double real,imag;
public:
complex(){real=5;imag=5;}//缺省构造函数
complex(double r){real=r;imag=0;}//只给实部赋值的构造函数
complex(double r,double i){real=r;imag=i;}//同时给实、虚部赋值的函数
double displayreal(){return real;}//返回复数实部
double displayimag(){return imag;}//返回复数虚部
complex operator+(complex c);//实现复数相加
complex operator-(complex c);//实现复数相减
complex operator*(complex c);//实现复数相乘
double cab(complex c);//求复数绝对值(模)
complex sqr(complex c);//求复数平方根
friend ostream &operator<<(ostream &out,complex &obj);//实现复数的输出
};
complex complex::operator+(complex c)//实现复数相加
{
real+=c.real;
imag+=c.imag;
return *this;
}
complex complex::operator-(complex c)//实现复数相减
{
real-=c.real;
imag-=c.imag;
return *this;
}
complex complex::operator*(complex c)//实现复数相乘
{
real=real*c.real-imag*c.imag;
imag=real*c.imag+imag*c.real;
return *this;
}
double complex::cab(complex c)//求复数绝对值(模)
{
double ri;
ri=sqrt(c.real*c.real+c.imag*c.imag);
return ri;
}
complex complex::sqr(complex c)//求复数平方根
{
real=sqrt((cab(c)+c.real)/2);
imag=sqrt((cab(c)-c.real)/2);
return *this;
}
ostream &operator<<(ostream &out,complex &obj)//实现复数的输出
{
if(obj.imag==0)out<<obj.real;
else out<<obj.real<<"+"<<obj.imag<<"i";
return out;
}
void answer(double a,double b,double c)//求根函数
{
complex answer1,answer2;
double an=b*b-4*a*c;
if(an>=0)
{
answer1=complex((-b+sqrt(an))/(2*a));
answer2=complex((-b-sqrt(an))/(2*a));
}
else
{
answer1=complex(-b/(2*a),sqrt(-an)/(2*a));
answer2=complex(-b/(2*a),-sqrt(-an)/(2*a));
}
cout<<"The answer is:"<<endl;
cout<<answer1<<" and "<<answer2<<endl;
}
int main()//主函数
{
complex a,b(2),c(6,9);//以下测试类中定义的各个函数,你可以删除的
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a+b;
cout<<"a+b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a-b;
cout<<"a-b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
c=a*b;
cout<<"a*b="<<c<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
cout<<"cab(a)="<<a.cab(a)<<endl;
cout<<"a="<<a<<", b="<<b<<", c="<<c<<endl;
cout<<"sqr(a)="<<a.sqr(a)<<endl;//以上测试类中定义的各个函数
answer(1,1,1);//方程解为虚数的情况
answer(1,3,1);//方程解为实数的情况
return 0;
}
Ⅱ C#复数的编程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace ConsoleApplication1
{
class Program
{
class Complex
{
public Complex() { realPart = 0; imaginaryPart = 0; }
public Complex(float real, float imaginary) { realPart = real; imaginaryPart = imaginary; }
public override string ToString()
{
string result = realPart.ToString();
if (imaginaryPart >= 0) result += "+";
result += imaginaryPart.ToString() + "i";
return result;
}
static public Complex operator +(Complex a, Complex b)
{
Complex result = new Complex();
result.realPart = a.realPart + b.realPart;
result.imaginaryPart = a.imaginaryPart + b.imaginaryPart;
return result;
}
static public Complex operator -(Complex a)
{
Complex result = new Complex(-a.realPart, -a.imaginaryPart);
return result;
}
static public Complex operator -(Complex a,Complex b)
{
return a + (-b);
}
static public Complex operator *(Complex a, Complex b)
{
Complex result = new Complex ();
result.realPart = (a.realPart * b.realPart) - (a.imaginaryPart * b.imaginaryPart);
result.imaginaryPart = (a.realPart * b.imaginaryPart) + (a.imaginaryPart * b.realPart);
return result;
}
static public Complex operator /(Complex a, Complex b)
{
Complex result = new Complex();
Complex Conjugal = new Complex(b.realPart, -b.imaginaryPart);
float denominator = (b.realPart * b.realPart) + (b.imaginaryPart * b.imaginaryPart);
result = a * Conjugal;
result.realPart /= denominator;
result.imaginaryPart /= denominator;
return result;
}
private float realPart;
private float imaginaryPart;
}
static void Main(string[] args)
{
Complex a = new Complex(3, 5);
Complex b = new Complex(2, 3);
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
Console.WriteLine((a + b).ToString());
Console.WriteLine((a - b).ToString());
Console.WriteLine((a * b).ToString());
Console.WriteLine((a / b).ToString());
}
}
}