• 主页
• 问答

# 100分求一小程序

peter9606 2004-05-11 08:23:13

...全文
48 点赞 收藏 15

15 条回复

vcchunhong 2004-05-12

freefalcon 2004-05-12

——楼主什么口气,为什么要写呢?别人又不是帮你写程序的工具,别人也有自己的事情

languagec 2004-05-12

goodname 2004-05-12

--

languagec 2004-05-12
#include <iostream.h>
class complex
{
public:
complex() {real=imag=0;}
complex(double r,double i)
{
real=r;
imag=i;
}
friend complex operator + (const complex &c1,const complex &c2);
friend complex operator - (const complex &c1,const complex &c2);
friend complex operator * (const complex &c1,const complex &c2);
friend complex operator / (const complex &c1,const complex &c2);
friend void print(const complex &c);
private:
double real,imag;
};

complex operator +(const complex &c1,const complex &c2)
{
return complex(c1.real+c2.real,c1.imag+c2.imag);
}

complex operator -(const complex &c1,const complex &c2)
{
return complex(c1.real-c2.real,c1.imag-c2.imag);
}

complex operator *(const complex &c1,const complex &c2)
{
return complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}

complex operator /(const complex &c1,const complex &c2)
{
return complex((c1.real*c2.real+c1.imag+c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),
(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));
}

void print(const complex &c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i';
else
cout<<c.real<<'+'<<c.imag<<'i';
}

void main()
{
complex c1(2.0,3.0),c2(4.0,-2.0),c3;
c3=c1+c2;
cout<<"\nc1+c2=";
print(c3);
c3=c1-c2;
cout<<"\nc1-c2=";
print(c3);
c3=c1*c2;
cout<<"\nc1*c2=";
print(c3);
c3=c1/c2;
cout<<"\nc1/c2=";
print(c3);
c3=(c1+c2)*(c1-c2)*c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
}

peter9606 2004-05-12

peter9606 2004-05-12
To ： languagec 谢谢啦 已经结贴啦

surstar 2004-05-12
GZ

jmlt1983 2004-05-11

//111111111用库
#include<complex>
#include<iostream>
using namespace std;

void main()
{
complex<int> a(1,2);
complex<int> b(2,1);
complex<int> c;
c=a+b;
cout<<c.real()<<"\t"<<c.imag()<<"\n";
c=a*b;
cout<<c.real()<<"\t"<<c.imag()<<"\n";
c=a-b;
cout<<c.real()<<"\t"<<c.imag()<<"\n";
}

//22222222222用数据结构
#include <stdio.h>

struct data
{
float re;
float im;
};
struct data x,y,z;

int main()
{
float mod;
x.re = 1;
x.im = 2;
y.re = 3;
y.im = 4;

mod = (float)(y.re * y.re + y.im * y.im);

z.re = (float)(x.re * y.re + x.im * y.im) / mod;
z.im = (float)(x.im * y.re - x.re * y.im) / mod;

printf(" x/y = [%f] + [%f]i ", z.re ,z.im);
return 0;
}

peter9606 2004-05-11

zxs790501 2004-05-11
complex类接口：
complex
template<class T>
class complex {
public:
typedef T value_type;
T real() const;
T imag() const;
complex(const T& re = 0, const T& im = 0);
complex(const complex& x);
complex& operator=(const complex& rhs);
complex& operator+=(const complex& rhs);
complex& operator-=(const complex& rhs);
complex& operator*=(const complex& rhs);
complex& operator/=(const complex& rhs);
complex& operator=(const T& rhs);
complex& operator=(const T& rhs);
complex& operator+=(const T& rhs);
complex& operator-=(const T& rhs);
complex& operator*=(const T& rhs);
complex& operator/=(const T& rhs);
friend complex<T>
operator+(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator+(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator-(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator-(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator*(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator*(const T& lhs, const complex<T>& rhs);
friend complex<T>
operator/(const complex<T>& lhs, const T& rhs);
friend complex<T>
operator/(const T& lhs, const complex<T>& rhs);
friend bool operator==(const complex<T>& lhs, const T& rhs);
friend bool operator==(const T& lhs, const complex<T>& rhs);
friend bool operator!=(const complex<T>& lhs, const T& rhs);
friend bool operator!=(const T& lhs, const complex<T>& rhs);
};

peter9606 2004-05-11

sharkhuang 2004-05-11

freefalcon 2004-05-11

#include <complex>

C++ 语言

5.9w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等