生成一个表示复数的类FS。复数的实部sb和虚部xb作为其数据成员。提供成员函数print()显示复数(格式:-3+5i或6-2i),重载“+”、“-”为FS类的成员函数,用来计算两个复数的和、差。
思路导航:
① 定义类,设计构造函数和显示函数print();
② 重载运算符“+”、“-”为类FS的成员函数。
③ 实例化FS类的2个对象,并利用重载的运算符对其进行计算。
另外,根据上述定义,试将上述的运算符重载成员函数改写成友元函数。
#include"iostream.h"
#include"FS.h"
void FS::SetFS(double s,double x)
{
sb=s;
xb=x;
}
void FS::print()
{
if(sb==0)
{
if(xb==0)
{cout<<"0"<<endl;}
else if(xb==1)
{cout<<"i"<<endl;}
else
{cout<<xb<<"i"<<endl;}
}
if(sb!=0)
{
if(xb==1)
{cout<<sb<<"+"<<"i"<<endl;}
else if(xb==0)
{cout<<sb<<endl;}
else if(xb==-1)
{cout<<sb<<"-"<<"i"<<endl;}
else if(xb>0)
{cout<<sb<<"+"<<xb<<"i"<<endl;}
else
{cout<<sb<<xb<<"i"<<endl;}
}
}
FS operator + (FS &A,FS &B)
{ double m,n;
m=A.sb+B.sb;
n=A.xb+B.xb;
return(FS(m,n));
}
FS operator - (FS &A,FS &B)
{
FS C;
C.sb=A.sb-B.sb;
C.xb=A.xb-B.xb;
return C;
}
FS operator * (FS &A,FS &B)
{
FS C;
C.sb=A.sb*B.sb-A.xb*B.xb;
C.xb=A.xb*B.sb+A.sb*B.xb;
return C;
}
FS operator / (FS &A,FS &B)
{
FS C;
double t;
t=B.sb*B.sb+B.xb*B.xb;
C.sb=(B.sb*A.sb+B.xb*A.xb)/t;
C.xb=(A.xb*B.sb-A.sb*B.xb)/t;
return C;
}
ostream & operator << (ostream &out,FS &s)
{
out<<s.sb<<s.xb;
return out;
}
/*main*/
#include"iostream.h"
#include"FS.h"
void main()
{ FS A,B,C;
double sb,xb;
cout<<"输入A的实部、虚部"<<endl;
cin>>sb>>xb;
A.SetFS(sb,xb);
cout<<"输入B的实部、虚部"<<endl;
cin>>sb>>xb;
B.SetFS(sb,xb);
cout<<"A为:"<<endl;
//cout<<A<<endl;
A.print();
cout<<"B为:"<<endl;
//cout<<B<<endl;
B.print();
/* cout<<"重载为成员函数"<<endl;
cout<<"C=A+B"<<endl;
C=A+B;
C.print();
cout<<"C=A-B"<<endl;
C=A-B;
C.print();*/
cout<<"重载为友元函数"<<endl;
C=A+B;
cout<<"C=A+B"<<endl;
C.print();
//cout<<C<<endl;
C=A-B;
cout<<"C=A-B"<<endl;
C.print();
//cout<<C<<endl;
C=A*B;
cout<<"C=A*B"<<endl;
cout<<C<<endl;
C.print();
C=A/B;
cout<<"C=A/B"<<endl;
//cout<<C<<endl;
C.print();
}
/*.h*/
#include"iostream.h"
class FS
{
public:
FS(double s=0.0,double x=0.0)
{
sb=s;
xb=x;
}
void SetFS(double sb,double xb);
// FS operator + (FS &A);
// FS operator - (FS &A);
friend FS operator + (FS &A,FS &B);
friend FS operator - (FS &A,FS &B);
friend FS operator * (FS &A,FS &B);
friend FS operator / (FS &A,FS &B);
friend ostream & operator <<(ostream &,FS &);
void print();
private:
double sb;
double xb;
};
编写一个时间类,实现时间的加、减、读和输出:
/*.cpp*/
#include"iostream.h"
#include"Time.h"
void Time::SetTime(int h,int m,int s)
{
hour=(h>=0 && h<24)?h:0;
minute=(m>=0 && m<60)?m:0;
second=(s>=0 && s<60)?s:0;
}
/*void Time::Display()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}*/
ostream &operator <<(ostream& output,Time &a)
{
output<<a.hour<<":"<<a.minute<<":"<<a.second<<endl;
return output;
}
istream &operator >>(istream& input,Time &a)
{
input>>a.hour>>":">>a.minute>>":">>a.second;
return input;
}
Time Time::operator + (Time &a)
{
int h=0,m=0,s=0;
int t=a.second+second,k=minute + t;
s= t%60;
m= (a.minute + k / 60)%60;
h= ( a.hour + hour +(k / 60)/60)%24;
/*h=a.hour+hour;
m=a.minute+minute;
s=a.second+second;
if(s>60)
{
s=s%60;
m=m+1;
}
if(m>60)
{
m=m%60;
h=h+1;
}
if(h>24)
{
h=h%24;
}*/
return Time(h,m,s);
}
Time Time::operator - (Time &a)
{
int h,m,s;
h=hour-a.hour;
m=minute-a.minute;
s=second-a.second;
if(s<0)
{
s=(s+60)%60;
m=m-1;
}
if(m<0)
{
m=(m+60)%60;
h=h-1;
}
if(h<0)
{
h=(h+24)%24;
}
return Time(h,m,s);
}
/*main*/
#include"iostream.h"
#include"Time.h"
void main()
{
Time a,b,c;
// istream input;
int h,m,s;
cout<<"请输入A的时间:"<<endl;
cin>>h>>m>>s;
//cin>>a;
a.SetTime(h,m,s);
cout<<"请输入B的时间:"<<endl;
cin>>h>>m>>s;
//cin>>b;
b.SetTime(h,m,s);
cout<<"A的时间为:"<<endl;
cout<<a<<endl;
//a.Display();
cout<<"B的时间为:"<<endl;
// b.Display();
cout<<b<<endl;
cout<<"c=a+b"<<endl;
c=a+b;
cout<<c<<endl;
//c.Display();
cout<<"c=a-b"<<endl;
c=a-b;
cout<<c<<endl;
// c.Display();
}
/*.h*/
#include"iostream.h"
class Time
{
public:
Time(int h=0,int m=0,int s=0)
{
hour=h;
minute=m;
second=s;
}
void SetTime(int hour,int minute,int second);
void Display();
Time operator + (Time &a);
Time operator - (Time &a);
friend ostream & operator <<(ostream &,Time &);
friend istream & operator >>(istream &,Time &);
private:
int hour;
int minute;
int second;
};
来源:CSDN
作者:smalldregs
链接:https://blog.csdn.net/smalldregs/article/details/103657550