⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 练习.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 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的结果
#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为整形数,故如果jf<b.jf,则jf将是一个负数,而int没有负数,故将补码形式的负数视为一个非常大的数
	{
		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();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -