當前位置:首頁 » 編程軟體 » 編程中復數

編程中復數

發布時間: 2023-03-13 03:18:08

Ⅰ 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());
}
}
}

熱點內容
原神電腦配置怎麼玩不卡 發布:2025-07-20 18:07:06 瀏覽:955
反編譯獲取原代碼 發布:2025-07-20 17:49:43 瀏覽:492
plc編譯怎麼找 發布:2025-07-20 17:48:56 瀏覽:162
無效的宏名稱將編譯啞宏 發布:2025-07-20 17:48:16 瀏覽:155
250萬解壓 發布:2025-07-20 17:44:52 瀏覽:620
騰訊視頻上傳廣告 發布:2025-07-20 17:35:55 瀏覽:406
掘地求升免費腳本 發布:2025-07-20 17:32:06 瀏覽:962
linux設置python環境變數 發布:2025-07-20 17:22:49 瀏覽:242
柱底層加密區 發布:2025-07-20 17:16:20 瀏覽:913
安卓ids是什麼文件 發布:2025-07-20 17:16:14 瀏覽:668