?? 練習(xí).txt
字號:
18.1
//************************
//** main.cpp **
//************************
#include <iostream.h>
#include "complex.h"
void main()
{
Complex a(2,5), b(7,8), c(0,0);
c=a+b;
c.display();
c= b + 5.6;
c.display();
c= 4.1 + a;
c.display();
}
//*************************
//** complex.h **
//*************************
#include <iostream.h>
#include <iomanip.h>
class Complex
{
public:
Complex(float a=0.0,float b=0.0);
Complex operator + (Complex &b);
friend Complex operator + (Complex &a,double d);
friend Complex operator + (double d, Complex &a);
void display();
protected:
float real;
float img;
};
Complex::Complex(float a,float b)
{
real=a;
img=b;
}
Complex Complex::operator + (Complex &b)
{
float tR=real+b.real;
float tI=img+b.img;
Complex result(tR,tI);
return result;
}
void Complex::display()
{
cout<<setw(6)<<real<<" +"<<setw(6)<<img<<"i"<<endl;
}
Complex operator + (Complex &a,double d)
{
float tR=a.real+d;
float tI=a.img;
Complex result(tR,tI);
return result;
}
Complex operator + (double d, Complex &a)
{
return a + d;
}
18.2
//************************
//** main.cpp **
//************************
#include <iostream.h>
#include "time.h"
void main()
{
Time a(10,30,0),b(5,30,2),c(0,0,0);
c = a + b;
c.display();
//a.display();
//b.display();
c = a - b;
c.display();
}
//**********************
//** time.h **
//**********************
#include <iostream.h>
//#include <iomanip.h>
class Time
{
public:
Time(int h=0,int m=0,int s=0);
Time operator + (Time &b);
Time operator - (Time &b);
void display();
protected:
int hour;
int min;
int sec;
};
Time::Time(int h,int m,int s)
{
hour=h;
min=m;
sec=s;
}
Time Time::operator + (Time &b)
{
int tH,tM,tS;
tH = hour + b.hour;
tM = min + b.min;
tS = sec + b.sec;
while(tS>=60)
{
tS-=60;
tM++;
}
while(tM>=60)
{
tM-=60;
tH++;
}
tH = tH%24;
Time result(tH,tM,tS);
return result;
}
Time Time::operator - (Time &b)
{
int tH=0,tM=0,tS=0;
tS=sec-b.sec;
if(tS<0)
{
tS+=60;
tM--;
}
tM=tM+min-b.min;
if(tM<0)
{
tM+=60;
tH--;
}
tH=tH+hour-b.hour;
if(tM<0)
{
tM+=60;
tH--;
}
tH %= 24;
Time result(tH,tM,tS);
return result;
}
void Time::display()
{
cout<<"time: ";
if(hour<10) cout<<0;
cout<<hour<<":";
if(min<10) cout<<0;
cout<<min<<":";
if(sec<10) cout<<0;
cout<<sec<<endl;
}
18.3
#include <iostream.h>
class RMB
{
public:
RMB(unsigned int d,unsigned int c);
RMB(double d)
{
yuan=d;
jf=(d-yuan)*100+0.5;
}
RMB operator + (RMB&);
RMB& operator ++ ();
friend RMB operator * (const RMB&, double);
friend RMB operator * (double, const RMB&);
void display()
{
cout<<(yuan + jf/100.0)<<endl;
}
protected:
unsigned int yuan;
unsigned int jf;
};
RMB::RMB(unsigned int d,unsigned int c)
{
yuan=d;
jf=c;
while(jf>=100)
{
yuan++;
jf-=100;
}
}
RMB RMB::operator + (RMB& s)
{
unsigned int c=jf + s.jf;
unsigned int d=yuan + s.yuan;
RMB result(d,c);
return result;
}
RMB& RMB::operator ++ ()
{
jf++;
if(jf>=100)
{
jf-=100;
yuan++;
}
return *this;
}
RMB operator * (const RMB&a, double b)
{
float rmbA,rmbRe;
rmbA = (a.yuan + a.jf/100.0); //注意此處 a.jf/100.0, 而不是/100
rmbRe = rmbA * b;
RMB result(rmbRe);
return result;
}
RMB operator * (double b, const RMB&a)
{
return a * b;
}
void main()
{
RMB d1(1,60);
RMB d2(2,50);
RMB d3(0,0);
d3=d1 + d2;
d3.display();
++d3;
d3.display();
d3 = 2.2 * d1;
d3.display();
}
18.4
//包含18.3的結(jié)果
#include <iostream.h>
class RMB
{
public:
RMB(unsigned int d,unsigned int c);
RMB(double d)
{
yuan=d;
jf=(d-yuan)*100+0.5;
}
RMB operator + (RMB&);
RMB& operator ++ ();
friend RMB operator * (const RMB&, double);
friend RMB operator * (double, const RMB&);
RMB& operator += (const RMB&);
RMB& operator += (double);
RMB& operator -= (const RMB&);
RMB& operator -= (double);
void display()
{
cout<<(yuan + jf/100.0)<<endl;
}
protected:
unsigned int yuan;
unsigned int jf;
};
RMB::RMB(unsigned int d,unsigned int c)
{
yuan=d;
jf=c;
while(jf>=100)
{
yuan++;
jf-=100;
}
}
RMB RMB::operator + (RMB& s)
{
unsigned int c=jf + s.jf;
unsigned int d=yuan + s.yuan;
RMB result(d,c);
return result;
}
RMB& RMB::operator ++ ()
{
jf++;
if(jf>=100)
{
jf-=100;
yuan++;
}
return *this;
}
RMB operator * (const RMB&a, double b)
{
float rmbA,rmbRe;
rmbA = (a.yuan + a.jf/100.0); //注意此處 a.jf/100.0, 而不是/100
rmbRe = rmbA * b;
RMB result(rmbRe);
return result;
}
RMB operator * (double b, const RMB&a)
{
return a * b;
}
RMB& RMB::operator += (const RMB&b)
{
yuan += b.yuan;
jf += b.jf;
while(jf>=100)
{
jf-=100;
yuan++;
}
return *this;
}
RMB& RMB::operator += (double d)
{
float rmbA;
rmbA = yuan + jf/100.0 + d;
yuan=rmbA;
jf=(rmbA-yuan)*100.0+0.5;
return *this;
}
RMB& RMB::operator -= (const RMB&b)
{
//check
float cmp1,cmp2;
cmp1=yuan+ jf/100.0;
cmp2=b.yuan + b.jf/100.0;
if(cmp1<cmp2)
{
cout<<"error operation."<<endl;
RMB result(0);
return result;
}
//process
while(jf<b.jf) //注意此處jf為整形數(shù),故如果jf<b.jf,則jf將是一個負(fù)數(shù),而int沒有負(fù)數(shù),故將補(bǔ)碼形式的負(fù)數(shù)視為一個非常大的數(shù)
{
jf+=100;
yuan--;
}
jf-=b.jf;
yuan-=b.yuan;
return *this;
}
RMB& RMB::operator -= (double d)
{
float rmbA;
rmbA = yuan + jf/100.0;
rmbA -= d;
yuan = rmbA;
jf = (rmbA-yuan)*100+0.5;
return *this;
}
void main()
{
RMB d1(1,60);
RMB d2(2,50);
RMB d3(0,0);
d3=d1 + d2;
d3.display();
++d3;
d3.display();
d3 = 2.2 * d1;
d3.display();
d3 += d1;
d3.display();
d3 += 0.08;
d3.display();
d3 -= d2;
d3.display();
d3 -=1.5;
d3.display();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -