7_61.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:2k
- #include<iostream.h>
- class complex
- {
- float a,b;
- public:
- complex(){a=0.0;b=0.0;}
- complex(float x,float y) {a=x;b=y;}
- void Add(complex& comp1,complex& comp2);
- void Sub(complex& comp1,complex& comp2);
- void Mul(complex& comp1,complex& comp2);
- void Div(complex& comp1,complex& comp2);
- void disp()
- {
- if(b>0)
- cout<<a<<"+"<<b<<"i"<<endl;
- else
- cout<<a<<"-"<<-b<<"i"<<endl;
- }
- };
- void complex::Add(complex& comp1,complex& comp2)
- {
- float x=comp1.a+comp2.a;
- float y=comp1.b+comp2.b;
- a=x;
- b=y;
- cout<<"复数s1、s2之和为:";
- }
- void complex::Sub(complex& comp1,complex& comp2)
- {
- float x=comp1.a-comp2.a;
- float y=comp1.b-comp2.b;
- a=x;
- b=y;
- cout<<"复数s1、s2之差为:";
- }
- void complex::Mul(complex& comp1,complex& comp2)
- {
- float x=comp1.a*comp2.a-comp1.b*comp2.b;
- float y=comp1.a*comp2.b+comp1.b*comp2.a;
- a=x;
- b=y;
- cout<<"复数s1、s2之积为:";
- }
- void complex::Div(complex& comp1,complex& comp2)
- {
- if((comp2.a*comp2.a+comp2.b*comp2.b)!=0)
- {
- float x=(comp1.a*comp2.a+comp1.b*comp2.b)/(comp2.a*comp2.a+comp2.b*comp2.b);
- float y=(comp1.b*comp2.a-comp1.a*comp2.b)/(comp2.a*comp2.a+comp2.b*comp2.b);
- a=x;
- b=y;
- cout<<"复数s1、s2之商为:";
- if(b>0)
- cout<<a<<"+"<<b<<"i"<<endl;
- else
- cout<<a<<"-"<<-b<<"i"<<endl;
- }
- else
- cout<<"除数为零,不能相除!";
- }
- void main()
- {
- complex s1(1,2),s2(3,4),s3,s4,s5,s6;
- cout<<"复数s1:";
- s1.disp();
- cout<<"复数s2:";
- s2.disp();
- s3.Add(s1,s2);
- s3.disp();
- s4.Sub(s1,s2);
- s4.disp();
- s5.Mul(s1,s2);
- s5.disp();
- s6.Div(s1,s2);
- cout<<endl;
- }