(一二六)第十一章编程练习

这是我创建的专辑,所有学习笔记都以word格式上传,放在里面,相对更美观和直接。欢迎下载浏览。

http://download.csdn.net/album/detail/2971




1.修改程序清单11.5,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标志。另外,让该程序将初始条件(目标距离和步长)以及结果小结写入到该文件中。该文件的内容与下面类似:

Target Distance: 100, Step Size: 20

0: (x,y) = (0, 0)

1: (x,y) = (-11.4715, 16.383)

2: (x,y) = (-868807, -3.42232)

....

26: (x,y) = (42.2919, -78.2594)

27: (x,y) = (58.6749, -89.7309)

After 27 steps, the subject has the following location:

(x,y) = (58.6749, -89.7309)

or

(m,a) = (107.212, -56.8194)

Average outward distance per step = 3.97081



答:

注:这里我不修改源文件了,在自己的基础上,重新写一个

//Point.h 包含类定义
#pragma once
class Point
{
public:
	enum mode { xy = 0, ji = 1 };	//常量
private:
	double x;	//x,y坐标
	double y;
	double jiaodu;	//极坐标:角度和长度
	double changdu;
	int mode;	//模式,根据模式来决定坐标输出时的形式
	void setxy();	//设置x、y坐标(根据极坐标)
	void setji();	//设置极坐标(根据x,y坐标)
public:

	Point(double a = 0, double b = 0, int c = xy);	//默认构造函数。默认为x,y坐标赋值
	void reset(double a = 0, double b = 0, int c = xy);	//重置坐标
	void show(std::ostream &os)const;	//输出坐标
	double get_changdu() { return changdu; }	//返回极坐标的长度
	Point operator+(const Point& m)const;	//运算符+重载,不用引用是因为要创建一个新对象,然后返回这个对象
	friend std::ostream& operator<<(std::ostream &os, const Point&m);	//用于cout或其他输出
};

//Point.cpp 包含了Point类的成员函数以及友元函数定义
#include<iostream>
#include<cmath>
#include<fstream>
#include"Point.h"
void Point::setxy()	//设置x、y坐标(根据极坐标),函数内容是拿以前的改改
{
	const double jiao_to_angle = 57.29577951;	//角度/弧度的值大概是这个数
	double hudu = jiaodu / jiao_to_angle;	//角度除以这个值得到弧度
	y = sin(hudu)*changdu;	//对边(y) = sin弧度*斜边。
	x = cos(hudu)*changdu;	//临边(x) = sin弧度*斜边
}

void Point::setji()	//设置极坐标(根据x,y坐标)
{
	const double jiao_to_angle = 57.29577951;	//角度/弧度的值大概是这个数
	changdu = sqrt(x*x + y*y);	//利用坐标求距离
	jiaodu = atan2(y, x)*jiao_to_angle;	//利用坐标求角度
}

Point::Point(double a, double b, int c)
{
	if (c == xy)
	{
		x = a;
		y = b;
		mode = c;
		setji();
	}
	else if (c == ji)
	{
		jiaodu = a;
		changdu = b;
		mode = c;
		setxy();
	}
	else std::cout << "参数输入错误。如果你需要输入x、y坐标,请第三个参数输出0或者不输入第三个参数;如果你需要输入极坐标,请第三个参数输入1。" << std::endl;
}
void Point::reset(double a, double b, int c)
{
	if (c == xy)
	{
		x = a;
		y = b;
		setji();
	}
	else if (c == ji)
	{
		jiaodu = a;
		changdu = b;
		setxy();
	}
	else std::cout << "参数输入错误。如果你需要输入x、y坐标,请第三个参数输出0或者不输入第三个参数;如果你需要输入极坐标,请第三个参数输入1。" << std::endl;
}
void Point::show(std::ostream &os)const	//输出坐标
{
		os << "现在报告x,y坐标:" << std::endl;
		os << "x:" << x << ",y:" << y << std::endl;
		os << "现在报告极坐标:" << std::endl;
		os << "长度为:" << changdu << ",角度为:" << jiaodu << "度" << std::endl;
}
Point Point::operator+(const Point& m)const	//运算符+重载,不用引用是因为要创建一个新对象,然后返回这个对象
{
	Point q;
	q.x = x + m.x;
	q.y = y + m.y;
	q.mode = mode;	//默认模式为运算符前面的对象的显示模式
	q.setji();
	return q;
}
std::ostream& operator<<(std::ostream &os, const Point&m)
{
	os << " (x,y) = (" << m.x << ", " << m.y << ")";
	return os;
}

//1.cpp main()函数所在,用于使用和验证类
#include<iostream>
#include<fstream>
#include"Point.h"
#include<ctime>

int main()
{
	using namespace std;
	srand(time(0));
	ofstream q;	//创建osftream对象
	q.open("111.txt");	//打开111.txt文件
	int distance;
	cout << "请输入距离:";
	cin >> distance;
	int step_size;
	cout << "请输入每一步的距离:";
	cin >> step_size;

	Point m(0, 0);
	q << "0:" << m << endl;
	
	int steps;
	for (steps = 0;m.get_changdu() < distance;steps++)
	{
		double d = rand() % 360;
		Point newone(d, step_size, 1);
		m = m + newone;
		q << steps+1 << ":" << m << endl;
	}
	q << "After " << steps << " steps, the subject has the following location:" << endl;
	m.show(q);
	q << "Average outward distance per step = " << m.get_changdu() / steps << endl;
	system("pause");
	return 0;
}

文本文件结果:


0: (x,y) = (0, 0)
1: (x,y) = (-8.45237, 18.1262)
2: (x,y) = (7.09055, 30.7126)
3: (x,y) = (24.0515, 20.1142)
4: (x,y) = (4.05152, 20.1142)
5: (x,y) = (-0.447505, 0.626777)
6: (x,y) = (-5.62389, 19.9453)
7: (x,y) = (7.75873, 34.8082)
8: (x,y) = (27.0772, 39.9846)
9: (x,y) = (24.6399, 59.8355)
10: (x,y) = (33.4073, 77.8114)
11: (x,y) = (17.4346, 65.7751)
12: (x,y) = (15.6915, 45.8512)
13: (x,y) = (25.6915, 63.1717)
14: (x,y) = (6.20405, 67.6707)
15: (x,y) = (19.0598, 82.9916)
16: (x,y) = (38.0809, 76.8113)
17: (x,y) = (23.6941, 62.9181)
18: (x,y) = (43.1001, 67.7565)
19: (x,y) = (53.9928, 50.9831)
20: (x,y) = (73.3987, 55.8216)
21: (x,y) = (67.5513, 74.9477)
After 21 steps, the subject has the following location:
现在报告x,y坐标:
x:67.5513,y:74.9477
现在报告极坐标:
长度为:100.898,角度为:47.9712度
Average outward distance per step = 4.80465

 

 

 

 

 

2.对于Vector类的头文件(程序清单11.13)和实现文件(程序清单11.14)进行修改,使其不再储存矢量的长度和角度,而是在magval()和angval()被调用时计算它们。

应保留公有接口不变(公有方法及其参数不变),但对私有部分(包括一些私有方法)和方法实现进行修改。然后,使用程序清单11.15对修改后的版本进行测试,结果应该与以前相同,因为Vecotr类的公有接口与原来相同。

 

答:

//vect.cpp
#include<cmath>
#include"vect.h"
using std::sqrt;
using std::sin;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
	void Vector::set_x(double mag, double ang)
	{
		x = mag*cos(ang);
	}
	void Vector::set_y(double mag, double ang)
	{
		y = mag*sin(ang);
	}
	Vector::Vector()
	{
		x = y = 0.0;
		mode = RECT;
	}
	Vector::Vector(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
		}
		else if (form == POL)
		{
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0;
			mode = RECT;
		}
	}
	void Vector::reset(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
		}
		else if (form == POL)
		{
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0;
			mode = RECT;
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::polar_mode()
	{
		mode = POL;
	}
	void Vector::rect_mode()
	{
		mode = RECT;
	}
	Vector Vector::operator+(const Vector&b)const
	{
		return Vector(x + b.x, y + b.y);
	}
	Vector Vector::operator-(const Vector &b)const
	{
		return Vector(x - b.x, y - b.y);
	}
	Vector Vector::operator-()const
	{
		return Vector(-x, -y);
	}
	Vector Vector::operator*(double n)const
	{
		return Vector(n*x, n*y);
	}
	Vector operator*(double n, const Vector &a)
	{
		return a*n;
	}
	std::ostream & operator<<(std::ostream &os, const Vector &v)
	{
		if (v.mode == Vector::RECT)
			os << "(x,y) = (" << v.x << ", "<<v.y << ")";
		else if (v.mode == Vector::POL)
		{
			os << "(m,a) = (" << v.magval() << ", " << v.angval() << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}
}

//vect.cpp
#include<cmath>
#include"vect.h"
using std::sqrt;
using std::sin;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
	void Vector::set_x(double mag, double ang)
	{
		x = mag*cos(ang);
	}
	void Vector::set_y(double mag, double ang)
	{
		y = mag*sin(ang);
	}
	Vector::Vector()
	{
		x = y = 0.0;
		mode = RECT;
	}
	Vector::Vector(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
		}
		else if (form == POL)
		{
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0;
			mode = RECT;
		}
	}
	void Vector::reset(double n1, double n2, Mode form)
	{
		mode = form;
		if (form == RECT)
		{
			x = n1;
			y = n2;
		}
		else if (form == POL)
		{
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0;
			mode = RECT;
		}
	}
	Vector::~Vector()
	{
	}
	void Vector::polar_mode()
	{
		mode = POL;
	}
	void Vector::rect_mode()
	{
		mode = RECT;
	}
	Vector Vector::operator+(const Vector&b)const
	{
		return Vector(x + b.x, y + b.y);
	}
	Vector Vector::operator-(const Vector &b)const
	{
		return Vector(x - b.x, y - b.y);
	}
	Vector Vector::operator-()const
	{
		return Vector(-x, -y);
	}
	Vector Vector::operator*(double n)const
	{
		return Vector(n*x, n*y);
	}
	Vector operator*(double n, const Vector &a)
	{
		return a*n;
	}
	std::ostream & operator<<(std::ostream &os, const Vector &v)
	{
		if (v.mode == Vector::RECT)
			os << "(x,y) = (" << v.x << ", "<<v.y << ")";
		else if (v.mode == Vector::POL)
		{
			os << "(m,a) = (" << v.magval() << ", " << v.angval() << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}
}


//randwalk.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vect.h"
int main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	cout << "Enter target distance (q to quit):";
	while (cin >> target)
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		while (result.magval()<target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			steps++;
		}
		cout << "After " << steps << " steps, the subjct has the following location:\n";
		cout << result << endl;
		result.polar_mode();
		cout << " or\n" << result << endl;
		cout << "Average outward distance per step = " << result.magval() / steps << endl;
		steps = 0;
		result.reset(0.0, 0.0);
		cout << "Enter target distance (q to quit): ";
	}
	cout << "Bye!\n";
	cin.clear();
	while (cin.get() != '\n')
		continue;
	return 0;
}



 

 

 

 

 

3.修改程序清单11.15,使之报告N次测试中的最高、最低和平均步数(其中N是用户输入的整数),而不是报告每次测试的结果。

答:

为了方便,我在编程练习2的基础上修改。

//randwalk.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"vect.h"
int main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	cout << "Enter target distance (q to quit):";
	while (cin >> target)
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		cout << "输入你想测试的次数:";
		int N;
		cin >> N;
		if (N == 0)break;
		while (result.magval()<target)
		{
			direction = rand() % 360;
			step.reset(dstep, direction, Vector::POL);
			result = result + step;
			steps++;
		}
		double Max, Min, Average;
		Max = Min = Average = steps;
		for (int i = 1;i < N;i++)
		{
			steps = 0;
			result.reset(0.0, 0.0);
			while (result.magval()<target)
			{
				direction = rand() % 360;
				step.reset(dstep, direction, Vector::POL);
				result = result + step;
				steps++;
			}
			if (Max < steps)Max = steps;
			if (Min > steps)Min = steps;
			Average = Average + steps;
		}
		Average = Average / N;
		cout << "次数:" << N << endl;
		cout << "最大步数:" << Max << endl;
		cout << "最小步数:" << Min << endl;
		cout << "平均步数:" << Average << endl;
		cout << "Enter target distance (q to quit):";
	}
	cout << "Bye!\n";
	cin.clear();
	cin.sync();
	system("pause");
	return 0;
}

显示:

Enter target distance (q to quit):50
Enter step length: 2
输入你想测试的次数:1000
次数:1000
最大步数:3083
最小步数:72
平均步数:646.003
Enter target distance (q to quit):50
Enter step length: 2
输入你想测试的次数:1000
次数:1000
最大步数:3034
最小步数:67
平均步数:646.13
Enter target distance (q to quit):q
Bye!
请按任意键继续. . .


 

 

 

 

 

 

 

 

4.重新编写最后的Time了示例(程序清单11.10、程序清单11.11和程序清单11.12),使用友元函数来实现所有的重载运算符。

答:


//mytime3.h
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include<iostream>

class Time
{
private:
	int hours;
	int minutes;
public:
	Time();
	Time(int h, int m = 0);
	void AddMin(int m);
	void AddHr(int h);
	void Reset(int h = 0, int m = 0);
	friend Time operator+(const Time & t1, const Time & t2);
	friend Time operator-(const Time & t1, const Time & t2);
	friend Time operator*(const Time & t1, double n);
	friend Time operator*(double n, const Time & t1);
	friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif

//mytime3.cpp
#include"mytime3.h"

Time::Time()
{
	hours = minutes = 0;
}

Time::Time(int h, int m)
{
	hours = h;
	minutes = m;
}

void Time::AddMin(int m)
{
	minutes += m;
	hours += minutes / 60;
	minutes %= 60;
}
void Time::AddHr(int h)
{
	hours += h;
}
void Time::Reset(int h, int m)
{
	hours = h;
	minutes = m;
}

Time operator +(const Time &t1, const Time &t2)
{
	int minutes, hours;
	minutes = t1.minutes + t2.minutes;
	hours = t1.hours + t2.hours + minutes / 60;
	minutes %= 60;
	Time sum(hours, minutes);
	return sum;
}

Time operator-(const Time & t1, const Time & t2)
{
	int tot1, tot2;
	int minutes, hours;
	tot1 = t1.minutes + 60 * t1.hours;
	tot2 = t2.minutes + 60 * t2.hours;
	minutes = (tot1 - tot2) % 60;
	hours = (tot1 - tot2) / 60;
	Time diff(hours, minutes);
	return diff;
}
Time operator*(const Time & t1, double n)
{
	int h, m;
	long totalminutes = t1.hours*n * 60 + t1.minutes*n;
	h = totalminutes / 60;
	m = totalminutes % 60;
	Time result(h, m);
	return result;
}
Time operator*(double n, const Time & t1)
{
	return t1*n;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
	os << t.hours << " hours, " << t.minutes << " minutes";
	return os;
}

//usetime3.cpp
#include<iostream>
#include "mytime3.h"

int main()
{
	using std::cout;
	using std::endl;
	Time aida(3, 35);
	Time tosca(2, 48);
	Time temp;

	cout << "Aida and Tosca:\n";
	cout << aida << ";" << tosca << endl;
	temp = aida + tosca;
	cout << "Aida + Tosca: " << temp << endl;
	temp = aida*1.17;
	cout << "Aida*1.17:" << temp << endl;
	cout << "10.0*Tosca: " << 10.0*tosca << endl;

	system("pause");
	return 0;
}

 

 

 

 

 

 

5.重新编写Stonewt类(程序清单11.16和程序清单11.17),使它有一个状态成员,由该状态成员控制对象应转换为英石格式、整数磅格式还是浮点磅格式。重载<<运算符,使用它来替换show_stn()和show_lbs()方法。重载加法、减法和重发运算符,以便可以对Stonewt值进行加、减、乘运算。编写一个使用所有类方法和友元的小程序,来测试这个类。

答:

// stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
	enum { Lbs_per_stn = 14 };
	enum MODE{STONE,INTPOUNDS,FLOATPOUNDS};
	int stone;
	double pds_left;
	double pounds;
	int Mode;
public:
	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	Stonewt();
	~Stonewt();
	void setmode();
	friend std::ostream& operator<<(std::ostream& os, Stonewt &m);
	Stonewt operator+(Stonewt&m);
	Stonewt operator-(Stonewt&m);
	Stonewt operator*(double m);
};
#endif

//stonewt.cpp
#include<iostream>
using std::cout;
using std::endl;
#include "stonewt.h"
enum MODE { STONE, INTPOUNDS, FLOATPOUNDS };
Stonewt::Stonewt(double lbs)
{
	stone = int(lbs) / Lbs_per_stn;
	pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
	pounds = lbs;
	Mode = INTPOUNDS;
}
Stonewt::Stonewt(int stn, double lbs)
{
	stone = stn;
	pds_left = lbs;
	pounds = stn*Lbs_per_stn + lbs;
	Mode = STONE;
}
Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::setmode()
{
	cout << "你想要以何种方式输出数据:" << endl;
	cout << "a.x英石y磅\tb.xx磅\tc.xx.yy磅" << endl;
	char a;
	std::cin >> a;
	while(!isalpha(a))
	{
		cout << "输入错误,请重新输入:";
		std::cin.clear();
		std::cin.sync();
		std::cin >> a;
	}
	switch (a)
	{
	case'A':
	case'a':Mode = STONE;
		break;
	case'B':
	case'b':Mode = INTPOUNDS;
		break;
	case'C':
	case'c':Mode = FLOATPOUNDS;
		break;
	default:cout << "输入错误,默认以x英石y磅格式输出。" << endl;
		Mode = STONE;
		break;
	}
}
std::ostream& operator<<(std::ostream& os,Stonewt &m)
{
	if (m.Mode == STONE)
	{
		os << m.stone << "英石" << m.pds_left << "磅";
	}
	else if (m.Mode == INTPOUNDS)
	{
		os << int(m.pounds) << "磅";
	}
	else if (m.Mode == FLOATPOUNDS)
	{
		os << m.pounds << "磅";
	}
	else
	{
		os << "出错,无法输出。";
	}
	return os;
}
Stonewt Stonewt::operator+(Stonewt&m)
{
	Stonewt q;
	q.pounds = pounds + m.pounds;
	q.stone = int(q.pounds) / Lbs_per_stn;
	q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);
	q.Mode = STONE;
	return q;
}
Stonewt Stonewt::operator-(Stonewt&m)
{
	Stonewt q;
	q.pounds = pounds - m.pounds;
	q.stone = int(q.pounds) / Lbs_per_stn;
	q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);
	q.Mode = STONE;
	return q;
}
Stonewt Stonewt::operator*(double m)
{
	Stonewt q;
	q.pounds = pounds*m;
	q.stone = int(q.pounds) / Lbs_per_stn;
	q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);
	q.Mode = STONE;
	return q;
}

#include<iostream>
#include"stonewt.h"

int main()
{
	using namespace std;
	Stonewt a(100);
	Stonewt b(50, 10.3);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	Stonewt c = a + b;
	cout << "c=a+b=" << c << endl;
	c.setmode();
	cout << "c=" << c << endl;
	Stonewt d = b - a;
	cout << "d=b-a=" << d << endl;
	Stonewt e = a*3.3;
	cout << "e=a*3.3=" << e << endl;

	system("pause");
	return 0;
}

显示:


a=100磅
b=50英石10.3磅
c=a+b=57英石12.3磅
你想要以何种方式输出数据:
a.x英石y磅      b.xx磅  c.xx.yy磅
c
c=810.3磅
d=b-a=43英石8.3磅
e=a*3.3=23英石8磅
请按任意键继续. . .

 

 

 

 

 

 

 

6.重新编写Stonewt类(程序清单11.16和程序清单11.17),重载全部6个关系运算符。运算符对pounds成员进行比较,并返回一个bool值。编写一个程序,它声明一个包含6Stonewt对象的数组,并在数组声明中初始化前3个对象。然后使用循环来读取用于设置剩余3个数组元素的值。接着报告最小的元素、最大的元素以及大于或等于11英石的元素的数量(最简单的方法是创建一个Stonewt对象,并将其初始化为11英石,然后将其同其他对象进行比较)。

答:

// stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
	enum { Lbs_per_stn = 14 };
	enum MODE{STONE,INTPOUNDS,FLOATPOUNDS};
	int stone;
	double pds_left;
	double pounds;
	int Mode;
public:
	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	Stonewt();
	~Stonewt();
	void setmode();
	friend std::ostream& operator<<(std::ostream& os, Stonewt &m);
	Stonewt operator+(Stonewt&m);
	Stonewt operator-(Stonewt&m);
	Stonewt operator*(double m);
	bool operator<(Stonewt&m);
	bool operator<=(Stonewt&m);
	bool operator>(Stonewt&m);
	bool operator>=(Stonewt&m);
	bool operator==(Stonewt&m);
	bool operator!=(Stonewt&m);
};
#endif

//stonewt.cpp
#include<iostream>
using std::cout;
using std::endl;
#include "stonewt.h"
enum MODE { STONE, INTPOUNDS, FLOATPOUNDS };
Stonewt::Stonewt(double lbs)
{
	stone = int(lbs) / Lbs_per_stn;
	pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
	pounds = lbs;
	Mode = INTPOUNDS;
}
Stonewt::Stonewt(int stn, double lbs)
{
	stone = stn;
	pds_left = lbs;
	pounds = stn*Lbs_per_stn + lbs;
	Mode = STONE;
}
Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::setmode()
{
	cout << "你想要以何种方式输出数据:" << endl;
	cout << "a.x英石y磅\tb.xx磅\tc.xx.yy磅" << endl;
	char a;
	std::cin >> a;
	while(!isalpha(a))
	{
		cout << "输入错误,请重新输入:";
		std::cin.clear();
		std::cin.sync();
		std::cin >> a;
	}
	switch (a)
	{
	case'A':
	case'a':Mode = STONE;
		break;
	case'B':
	case'b':Mode = INTPOUNDS;
		break;
	case'C':
	case'c':Mode = FLOATPOUNDS;
		break;
	default:cout << "输入错误,默认以x英石y磅格式输出。" << endl;
		Mode = STONE;
		break;
	}
}
std::ostream& operator<<(std::ostream& os,Stonewt &m)
{
	if (m.Mode == STONE)
	{
		os << m.stone << "英石" << m.pds_left << "磅";
	}
	else if (m.Mode == INTPOUNDS)
	{
		os << int(m.pounds) << "磅";
	}
	else if (m.Mode == FLOATPOUNDS)
	{
		os << m.pounds << "磅";
	}
	else
	{
		os << "出错,无法输出。";
	}
	return os;
}
Stonewt Stonewt::operator+(Stonewt&m)
{
	Stonewt q;
	q.pounds = pounds + m.pounds;
	q.stone = int(q.pounds) / Lbs_per_stn;
	q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);
	q.Mode = STONE;
	return q;
}
Stonewt Stonewt::operator-(Stonewt&m)
{
	Stonewt q;
	q.pounds = pounds - m.pounds;
	q.stone = int(q.pounds) / Lbs_per_stn;
	q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);
	q.Mode = STONE;
	return q;
}
Stonewt Stonewt::operator*(double m)
{
	Stonewt q;
	q.pounds = pounds*m;
	q.stone = int(q.pounds) / Lbs_per_stn;
	q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);
	q.Mode = STONE;
	return q;
}
bool Stonewt::operator<(Stonewt&m)
{
	if (pounds < m.pounds)return true;
	else return false;
}
bool Stonewt::operator<=(Stonewt&m)
{
	if (pounds <= m.pounds)return true;
	else return false;
}
bool Stonewt::operator>(Stonewt&m)
{
	if (pounds > m.pounds)return true;
	else return false;
}
bool Stonewt::operator>=(Stonewt&m)
{
	if (pounds >= m.pounds)return true;
	else return false;
}
bool Stonewt::operator==(Stonewt&m)
{
	if (pounds == m.pounds)return true;
	else return false;
}
bool Stonewt::operator!=(Stonewt&m)
{
	if (pounds != m.pounds)return true;
	else return false;
}

#include<iostream>
#include"stonewt.h"

int main()
{
	using namespace std;
	Stonewt m[6] = { 100,150,170.4 };
	for (int i = 3;i < 6;i++)
	{
		double q;
		cout << "请输入第:" << i + 1 << "个成员的重量(单位:磅):";
		cin >> q;
		m[i] = q;
	}

	Stonewt over = 11 * 14;	//标准11英石对象
	Stonewt max, min;
	max = min = m[0];
	int ma, mi, ov=0;
	for (int i = 1;i < 6;i++)
	{
		if (max < m[i])
		{
			max = m[i];
			ma = i + 1;
		}
		if (min > m[i])
		{
			min = m[i];
			mi = i + 1;
		}
		if (m[i] >= over)ov++;
	}
	cout << "最大是的第" << ma << "个,重量为:" << max << endl;
	cout << "最小是的第" << mi << "个,重量为:" << min << endl;
	cout << "超过" << ov << "个大于等于11英石。" << endl;

	system("pause");
	return 0;
}

显示:


请输入第:4个成员的重量(单位:磅):400
请输入第:5个成员的重量(单位:磅):70
请输入第:6个成员的重量(单位:磅):190
最大是的第4个,重量为:400磅
最小是的第5个,重量为:70磅
超过3个大于等于11英石。
请按任意键继续. . .

 

 

 

 

 

7.复数有两个部分组成:实数部分和虚数部分。复数的一种书写方式是:(3.0,4.0),其中,3.0是实数部分,4.0是虚数部分。假设a=(A, Bi), c= (C, Di),则下面是一些复数运算。

①加法:a + c = (A+C, (B+D)i)

②减法:a - c = (A-C, (B-D)i)

③乘法:a * c = (A*C-B*D, (A*D + B*C )i)

④乘法::x * c = (x * C, x * Di),其中x为实数

⑤共轭: ~a = (A, -Bi)

请定义一个复数类,以便下面的程序可以使用它来获得正确的结果。

#include<iostream>

using namespace std;

#include "complex0.h" //to avoid confusion with complex.h

int main()
{
complex a (3.0, 4.0); // initialize to (3,4i)

complex c;

cout << "Enter a complex number (q to quit):\n";

while (cin >> c)

{

cout << "c is " << c << '\n';

cout << "complex conjugate is " << ~c << '\n';

cout << "a is " << a << '\n';

cout << " a + c is " << a + c << '\n';

cout << " a - c is " << a - c << '\n';

cout << " a * c is " << a * c << '\n';

cout << " 2 * c is " << 2 * c << '\n';

cout << "Enter a complex number (q to quit):\n";

}

cout << "Done!\n";

return 0;

}

注意,必须重载运算符<<和>>。标准C++使用头文件complex提供了比这个示例更广泛的复数支持,因此应将自定义的头文件命名为complex0.h,以免发生冲突。应尽可能使用const

下面是该程序的运行情况。

Enter a complex number (q to quit):

real: 10

imaginary: 12

c is (10,12i)

complex conjugate is (10, -12i)

a is (3,4f)

a + c is (13,16i)

a - c is (-7, -8i)

a * c is (-18, 76i)

2 * c is (20.24i)

Enter a complex number (q to quit):

real: q

Done!

请注意,经过重载后,cin>>c将提示用户输入实数和虚数部分。

答:

//complex0.h
#ifndef COMPLEX0_
#define COMPLEX0_
#include<iostream>
class complex
{
	double a;
	double b;
public:
	complex() {};
	complex(double A, double B) { a = A;b = B; }
	complex operator+(complex& m)const;
	complex operator-(complex& m)const;
	complex operator*(complex& m)const;
	complex operator~();
	friend complex operator*(double a, complex& m);
	friend std::ostream& operator<<(std::ostream&os, const complex &m);
	friend std::istream& operator>>(std::istream&is, complex&m);
};

#endif // !COMPLEX0

//complex0.cpp
#include<iostream>
#include"complex0.h"

complex complex::operator+(complex& m)const
{
	complex q;
	q.a = a + m.a;
	q.b = b + m.b;
	return q;
}
complex complex::operator-(complex& m)const
{
	complex q;
	q.a = a - m.a;
	q.b = b - m.b;
	return q;
}
complex complex::operator*(complex& m)const
{
	complex q;
	q.a = a*m.a - b*m.b;
	q.b = a*m.b + b*m.a;
	return q;
}
complex complex::operator~()
{
	complex q;
	q.a = a;
	q.b = -b;
	return q;
}
complex operator*(double a, complex& m)
{
	complex q;
	q.a = m.a*a;
	q.b = m.b*a;
	return q;
}
std::ostream& operator<<(std::ostream&os, const complex &m)
{
	os << "(" << m.a << "," << m.b << "i)";
	return os;
}
std::istream& operator>>(std::istream& is, complex&m)
{
	double x, y;
	std::cout << "real: ";
	is >> x;
	if (!is)return is;
	std::cout << "imagiary: ";
	is >> y;
	m.a = x;
	m.b = y;
	return is;
}

//1.cpp main函数,用于测试
#include<iostream>
using namespace std;
#include "complex0.h"		//to avoid confusion with complex.h

int main()
{
	complex a(3.0, 4.0);		// initialize to (3,4i)
	complex c;
	cout << "Enter a complex number (q to quit):\n";
	while (cin >> c)
	{
		cout << "c is " << c << '\n';
		cout << "complex conjugate is " << ~c << '\n';
		cout << "a is " << a << '\n';
		cout << " a + c is " << a + c << '\n';
		cout << " a - c is " << a - c << '\n';
		cout << " a * c is " << a * c << '\n';
		cout << " 2 * c is " << 2 * c << '\n';
		cout << "Enter a complex number (q to quit):\n";
	}
	cout << "Done!\n";
	system("pause");	//加上这段,否则我的编译器上窗口会一闪而过
	return 0;
}

显示结果和要求完全相同。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值