展开全部
最近我也遇到这题,虽然有点迟,不过我还是想解答....
#include<iostream>using namespace std;
typedef struct Complex
{
double a;
double b;
}mc; //mc是Complex类型的变量
mc add(mc n,mc m) //复数相加
{
mc result;
result.a = n.a + m.a; //实数部分相加
result.b = n.b + m.b; //虚数部分相加
return result;
}
mc minus(mc n,mc m) //复数相减
{
mc result;
result.a = n.a - m.a; //实数部分相减
result.b = n.b - m.b; //虚数部分相减
return result;
}
mc multily(mc n,mc m) //复数相乘
{
mc result;
result.a = n.a * m.a - n.b * m.b;
result.b = n.b * m.a + m.b * n.a;
return result;
}
void Show(mc x) //定义输出函数
{
if(x.a == 0 && x.b == 0) //如果实数部分等于0且虚数部分等于0
cout<<"0.000\n";
else if(x.a == 0) //实数部分等于0
cout<<x.b<<"i"; //输出虚数部分
else if(x.b == 0) //虚数部分等于0
cout<<x.a; //输出实数部分
else
{
if(x.b < 0) //虚数部分小于0
cout<<x.a<<x.b;
else
cout<<x.a<<"+"<<x.b<<"i"<<endl;
}
}
void main()
{
mc x,y; //定义x,y为结构型变量
cout<<"输入第一个复数 x:\n";
cin>>x.a>>x.b; //输入第一个复数的实数部分和虚数部分
printf("输入第二个复数 y:\n");
cin>>y.a>>y.b;
cout<<"x y的和: "; Show(add(x,y)); //将复数相加得到的值通过show函数输出
cout<<"x y的差: "; Show(minus(x,y)); //将复数相减得到的值通过show函数输出
cout<<"x y的积: ";
Show(multily(x,y)); //将复数相乘得到的值通过show函数输出
}
#include<iostream>using namespace std;
typedef struct Complex
{
double a;
double b;
}mc; //mc是Complex类型的变量
mc add(mc n,mc m) //复数相加
{
mc result;
result.a = n.a + m.a; //实数部分相加
result.b = n.b + m.b; //虚数部分相加
return result;
}
mc minus(mc n,mc m) //复数相减
{
mc result;
result.a = n.a - m.a; //实数部分相减
result.b = n.b - m.b; //虚数部分相减
return result;
}
mc multily(mc n,mc m) //复数相乘
{
mc result;
result.a = n.a * m.a - n.b * m.b;
result.b = n.b * m.a + m.b * n.a;
return result;
}
void Show(mc x) //定义输出函数
{
if(x.a == 0 && x.b == 0) //如果实数部分等于0且虚数部分等于0
cout<<"0.000\n";
else if(x.a == 0) //实数部分等于0
cout<<x.b<<"i"; //输出虚数部分
else if(x.b == 0) //虚数部分等于0
cout<<x.a; //输出实数部分
else
{
if(x.b < 0) //虚数部分小于0
cout<<x.a<<x.b;
else
cout<<x.a<<"+"<<x.b<<"i"<<endl;
}
}
void main()
{
mc x,y; //定义x,y为结构型变量
cout<<"输入第一个复数 x:\n";
cin>>x.a>>x.b; //输入第一个复数的实数部分和虚数部分
printf("输入第二个复数 y:\n");
cin>>y.a>>y.b;
cout<<"x y的和: "; Show(add(x,y)); //将复数相加得到的值通过show函数输出
cout<<"x y的差: "; Show(minus(x,y)); //将复数相减得到的值通过show函数输出
cout<<"x y的积: ";
Show(multily(x,y)); //将复数相乘得到的值通过show函数输出
}
展开全部
#include<iostream>
using namespace std;
struct plural
{
double real,imaginary;
plural()
{
real=imaginary=0.0;
}
plural operator+(plural& s)
{
plural t;
t.real=s.real+real;
t.imaginary=s.imaginary+imaginary;
return t;
}
plural operator-(plural& s)
{
plural t;
t.real=real-s.real;
t.imaginary=imaginary-s.imaginary;
return t;
}
plural operator*(plural& s)
{
plural t;
t.real=s.real*real-s.imaginary*imaginary;
t.imaginary=real*s.imaginary+imaginary*s.real;
return t;
}
~plural()
{
}
};
istream & operator>>(istream & in,plural& s)
{
printf("请输入实部:\n");
scanf("%lf",&s.real);
printf("请输入虚部:\n");
scanf("%lf",&s.imaginary);
return in;
}
ostream & operator<<(ostream& ou,plural& s)
{
printf("%.4lf + %.4lfi",s.real,s.imaginary);
return ou;
}
int main()
{
plural a,b,c;
cin>>a>>b;
c=a+b;
cout<<c<<endl;
c=a-b;
cout<<c<<endl;
c=a*b;
cout<<c<<endl;
system("pause");
}
using namespace std;
struct plural
{
double real,imaginary;
plural()
{
real=imaginary=0.0;
}
plural operator+(plural& s)
{
plural t;
t.real=s.real+real;
t.imaginary=s.imaginary+imaginary;
return t;
}
plural operator-(plural& s)
{
plural t;
t.real=real-s.real;
t.imaginary=imaginary-s.imaginary;
return t;
}
plural operator*(plural& s)
{
plural t;
t.real=s.real*real-s.imaginary*imaginary;
t.imaginary=real*s.imaginary+imaginary*s.real;
return t;
}
~plural()
{
}
};
istream & operator>>(istream & in,plural& s)
{
printf("请输入实部:\n");
scanf("%lf",&s.real);
printf("请输入虚部:\n");
scanf("%lf",&s.imaginary);
return in;
}
ostream & operator<<(ostream& ou,plural& s)
{
printf("%.4lf + %.4lfi",s.real,s.imaginary);
return ou;
}
int main()
{
plural a,b,c;
cin>>a>>b;
c=a+b;
cout<<c<<endl;
c=a-b;
cout<<c<<endl;
c=a*b;
cout<<c<<endl;
system("pause");
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-08-29
展开全部
给你个类的吧,c++中一般不用结构体
#include <iostream.h>
//using namespace std;
class complex
{
private:
float real;
float image;
public:
complex(float r=0,float i=0)
{
real=r;
image=i;
}
/*
void display()
{
cout<<real<<"+"<<image<<"i"<<endl;
}
*/
complex operator +(complex &obj)
{
complex c;
c.real=real+obj.real;
c.image=image+obj.image;
return c;
}
complex operator -(complex &obj)
{
complex c;
c.real=real-+obj.real;
c.image=image-obj.image;
return c;
}
complex operator *(complex &obj)
{
complex c;
c.real=real*obj.real-image*obj.image;
c.image=image*obj.real+obj.image*real;
return c;
}
complex operator /(complex &obj)
{
complex c;
c.real=real*obj.real+image*obj.image;
c.image=image*obj.real-obj.image*real;
float A=obj.real*obj.real+obj.image*obj.image;
c.real=c.real/A;
c.image=c.image/A;
return c;
}
friend ostream & operator<<(ostream & ,complex &);
};
ostream & operator<<(ostream &out ,complex &obj)
{
out<<obj.real<<"+"<<obj.image<<"i"<<endl;
return out;
}
//3 ^ 2 ===>9
int main()
{
complex c1(3,4);
complex c2(c1);
//complex c3(-12,30);
cout<<c1<<endl; //.display();
cout<<c2<<endl; //.display();
complex c;
c=c1+c2;
cout<<c<<endl; //.display();
c=c1-c2;
cout<<c<<endl; //.display();c.display();
c=c1*c2;
cout<<c<<endl; //.display();c.display();
c=c1/c2;
cout<<c<<endl; //.display();c.display ();
return 0;
}
#include <iostream.h>
//using namespace std;
class complex
{
private:
float real;
float image;
public:
complex(float r=0,float i=0)
{
real=r;
image=i;
}
/*
void display()
{
cout<<real<<"+"<<image<<"i"<<endl;
}
*/
complex operator +(complex &obj)
{
complex c;
c.real=real+obj.real;
c.image=image+obj.image;
return c;
}
complex operator -(complex &obj)
{
complex c;
c.real=real-+obj.real;
c.image=image-obj.image;
return c;
}
complex operator *(complex &obj)
{
complex c;
c.real=real*obj.real-image*obj.image;
c.image=image*obj.real+obj.image*real;
return c;
}
complex operator /(complex &obj)
{
complex c;
c.real=real*obj.real+image*obj.image;
c.image=image*obj.real-obj.image*real;
float A=obj.real*obj.real+obj.image*obj.image;
c.real=c.real/A;
c.image=c.image/A;
return c;
}
friend ostream & operator<<(ostream & ,complex &);
};
ostream & operator<<(ostream &out ,complex &obj)
{
out<<obj.real<<"+"<<obj.image<<"i"<<endl;
return out;
}
//3 ^ 2 ===>9
int main()
{
complex c1(3,4);
complex c2(c1);
//complex c3(-12,30);
cout<<c1<<endl; //.display();
cout<<c2<<endl; //.display();
complex c;
c=c1+c2;
cout<<c<<endl; //.display();
c=c1-c2;
cout<<c<<endl; //.display();c.display();
c=c1*c2;
cout<<c<<endl; //.display();c.display();
c=c1/c2;
cout<<c<<endl; //.display();c.display ();
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询