求大神解救c++的一个复数加和输出程序
//定义描述复数的类型类型Complex,编写加法成员函数Add()完成两个复数的加法运算。//在主函数中定义复数类型变量sum与数组c。用成员函数Input()给数组c的5个元素输入复数值,
//并调用Add()函数完成sum=c+c+c+c+c的操作,最后利用成员函数display()输出数组c的5个复数值,
//及其复数sum的值。
#include<iostream>
using namespace std;
class Complex
{
public:
void Input();//输入复数函数的声明
void Add();//加和函数的声明
void Display();//输复数函数的声明
private:
double c;
double real;//实部
double image;//虚部
};
void Complex::Input()//类外定义公有输入函数
{
cin>>real>>image;
}
void Complex::Add()//类外定义公有加和函数
{
int sum1,sum2;//定义实部的总和um1和虚部的总加和sum2
sum1=c.real+c.real+c.real+c.real+c.real;
sum2==c.image+c.image+c.image+c.image+c.image;
}
void Complex::Display()
{
cout<<c<<endl;
}
int main()
{
cout<<"请输入复数的数字部分(不输入i)"<<endl;
int i;
int sum1,sum2;
Complex c;
Complex t;
for(i=0;i<5;i++)//
{
c.Input();
}
t.Add();
cout<<"结果如下:"<<endl;
for(i=0;i<5;i++)
{
c.Display();
}
cout<<"sum ="<<sum1<<"+"<<sum2<<"i"<<endl;//输出复数的加和结果
return 0;
}
{:5_99:} #include<iostream>
using namespace std;
classComplex
{
float Real, Image;
public:
Complex(float r = 0, float i = 0) { Real = r; Image = i; }
void Show()
{
cout << "Real=" << Real << '\t' << "Image=" << Image << '\n';
}
friend Complex operator *(Complex &, Complex &);
Complex operator /(Complex &); //重载运算符
friend Complex operator -(Complex &, Complex &);
Complex operator +(Complex &);
void display();
};
Complex operator *(Complex &c1, Complex&c2)
{
Complext;
t.Real = c1.Real * c2.Real - c1.Image * c2.Image;
t.Image = c1.Image*c2.Real + c1.Real* c2.Image;
return t;
}
Complex Complex::operator /(Complex &c)
{
Complex t;
t.Real = (Real *c.Real + Image * c.Image) / (c.Real*c.Real + c.Image * c.Image);
t.Image = (Image *c.Real - Real * c.Image) / (c.Real*c.Real + c.Image * c.Image);
return t;
}
Complex operator -(Complex &c1, Complex &c2)
{
Complex t;
t.Real = c1.Real - c2.Real;
t.Image = c1.Image - c2.Image;
return t;
}
Complex Complex::operator +(Complex &c)
{
Complex t;
t.Real = c.Real + Real;
t.Image = c.Image + Image;
return t;
}
void Complex::display()
{
cout << Real << "+" << Image << "i" << endl;
}
int main()
{
Complex c1(1, 2), c2(3, 4), c3, c4, c5, c6;
cout << "设c1=1+2i,c2=3+4i\n";
c3 = c1 + c2;
cout << "c3 = c1 + c2 = ";
c3.display();
c4 = c1 - c2;
cout << "c4 = c1 - c2 = ";
c4.display();
c5 = c1*c2;
cout << "c5 = c1 * c2 = ";
c5.display();
c6 = c1 / c2;
cout << "c6 = c1 / c2 = ";
c6.display();
return 0;
}
页:
[1]