c++编写复数求和

#include<iostream.h>
class Complex
{public:
Complex(double nreal,double nimag);
Complex input();
Complex add(Complex a,Complex b);
void output();
private:
double real;
double imag;
}

Complex::Complex(double nreal,double nimag) //构造函数
{Complex c;
real=nreal;
imag=nimag;
return(c);
}

Complex Complex::input()
{ double nreal, nimag;
Complex c;
cout<<"输入实部"<<endl;
cin>>nreal;
c.real=nreal;
cout<<"输入虚部"<<endl;
cin>>nimag;
c.imag=nimag;

return(c);
}

Complex Complex::add(Complex a,Complex b)
{ Complex c;

c.real=a.real+b.real;
c.imag=a.imag+b.imag;
return(c);

}

Complex Complex::output()
{cout<<real<<"+i"<<imag<<endl;
}

void main()
{
Complex c1,c2,c3;
c1.input();
c2.input();
c3=c3.add(c1,c2);
c3.output();

}
帮忙改一下,谢谢

#include<iostream>
using namespace std;
class complex
{public:
complex(double r=0.0,double i=0.0){real=r; imag=i;}
complex complex_add(complex &c);
void display();
private:
double real; //复数实部
double imag; //复数虚部
};
complex complex::complex_add(complex &c2)
{ complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void complex::display()
{ cout<<"("<<real<<","<<imag<<")"<<endl; }
int main()
{
complex c1(3,4), c2(5,-10),c3;
c3=c1.complex_add(c2);
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
cout<<"c3="; c3.display();
return 0;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2007-10-20
实部加实部 虚部加虚部就可以了 就相当于两个加法

C++ 定义一个复数类 求和,求积,求商,求差
include<iostream> using namespace std;class CComplex { private:double real;double imaginary;public:CComplex(double re=0.0,double im=0.0):real(re),imaginary(im){} \/\/因为本类中没有任何动态内存分配问题,所以,编译器自动提供的默认拷贝构造函数也能正确的完成工作,因而 \/\/不需另外定义。

C++ 一个复数类,运算符重载 + ,实现复数和复数的相加。
include <iostream>using namespace std;class Complex{public: Complex(){} Complex(float i,float j) { this->a = i; this->b = j; } Complex& operator+(Complex &x); float getA(){return a;} float getB(){return b;}private: float a;\/\/实数 ...

c++中怎样求复数相加
double i){real=r;imag=i;} friend Complex operator+(Complex &c1,Complex &c2); void display();private: double real,imag;};Complex operator+(Complex &c1,Complex &c2){ Complex c; c

c++编写复数求和
include<iostream> using namespace std;class complex {public:complex(double r=0.0,double i=0.0){real=r; imag=i;} complex complex_add(complex &c);void display();private:double real; \/\/复数实部 double imag; \/\/复数虚部 };complex complex::complex_add(complex &c2){ complex ...

(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算...
>>”private: double real; double imag;};Complex operator +(Complex &c1,Complex &c2){ return Complex(c1.real+c2.real,c1.imag+c2.imag);}Complex operator +(Complex &c,double i){ return Complex(i+c.real,c.imag);}Complex operator +(double i,Complex &c)...

用c语言编写程序:输入两个复数,计算并输出它们的和及乘积
输入两个复数,计算并输出它们的和及乘积c语言编写:include<stdio.h> include<math.h> void main(){ int i1,i2,j1,j2;int sum(int i1,int i2,int j1,int j2);int mul(int i1,int i2,int j1,int j2);printf("请输入第一个复数的实部和虚部:");scanf("%d %d",&i1,&i2)...

...程序(输入两个复数(格式如2+3i),输出两个复数相加的结果。)_百度知 ...
{ int x1,y1, x2, y2;printf("Please input complex 1:");scanf("%d+%di", &x1, &y1);printf("Please input complex 2:");scanf("%d+%di", &x2, &y2);printf("The sum is=%d+%di\\n", x1+x2, y1+y2);return 0;} 以上程序是最简单的形式,输入时要注意只能x+yi这样...

如何用c语言编一个复数的四则运算
要求实现下列六种基本运算1)由输入的实部和虚部生成一个复数2)两个复数求和;3)两个复数求差;4)两个复数求积,5)从已知复数中分离出实部;6)从已知复数中分离出虚部。运算结果以相应的复数或实数的表示形式显示(最好用结构体的方法)要是能用c++和stl,可以这样写#include <complex>#include <iostream...

c++中怎样求复数相加?
定义复数类,中间加入运算符重载等。

C++复数与整数相加,复数与复数相加?
\/*简单的+号重载,复数类+复数类+整数,是不是你想要的效果?*\/ include <iostream> include<cmath> include<fstream> include <cstring> using namespace std;class fs { int a, b;public:void set(int x, int y) { a = x, b = y;} void show() { cout << a;if (b >= ...

相似回答