小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. C/C++
  5. 正文

C++ Primer Plus(第五版)第11章编程题答案

2018年11月18日 1738点热度 0人点赞 1条评论

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

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "vector.h"
using namespace std;
using VECTOR::Vector;
int main() {
	ofstream outFile;
	outFile.open("exercise1.txt");
	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;
		outFile << "Target Distance: " << target << ", Step Size: " << dstep << endl;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.set(dstep, direction, 'p');
			result = result + step;
			outFile << steps << ": (x,y) = " << "(" << step.xval() << ", " << step.yval() << ")" << endl;
			steps++;
		}
		outFile << "After " << steps << " steps, the subject "
			"has the following location:\n";
		outFile << "(x,y)=" << result << endl;
		result.polar_mode();
		outFile << " or\n" << result << endl;
		outFile << "Average outward distance per step = "
			<< result.magval() / steps << endl;
		outFile << "===================================================="<<endl;
                outFile << "COPYRIGHT 2018 WWW.YUSHUAI.XYZ" << endl;
		steps = 0;
		result.set(0.0, 0.0);
		cout << "Enter target distance (q to quit): ";
	}
	outFile.close();
	cout << "Bye~" << endl;
	system("pause");
	return 0;
}

2.对Vector类的头文件(程序清单11.13)和实现文件(程序清单11.14)进行修改,使其不再存储矢量的长度和角度,而是在magval()和angval()被调用时计算它们。
应保留公有接口不变(公有方法及其参数不变),但对私有部分(包括一些私有方法)和方法实现进行修改。然后,使用程序清单11.15对修改后的版本进行测试,结果应该与以前相同,因为Vector类的公有接口与原来相同。

vector.h

// vect.h -- Vector class with <<, mode state
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
	class Vector
	{
	private:
		double x;          // horizontal value
		double y;          // vertical value
		char mode;         // 'r' = rectangular, 'p' = polar
						   // private methods for setting values
		void set_x(double mag, double ang);
		void set_y(double mag, double ang);
	public:
		Vector();
		Vector(double n1, double n2, char form = 'r');
		void set(double n1, double n2, char form = 'r');
		~Vector();
		double xval() const { return x; }       // report x value
		double yval() const { return y; }       // report y value
		double magval() const { return sqrt(x * x + y * y); }   // report magnitude
		double angval() const// report angle
		{
			if (x == 0.0 && y == 0.0)
				return 0.0;
			else
				return atan2(y, x);
		}
		
		void polar_mode();                    // set mode to 'p'
		void rect_mode();                     // set mode to 'r'
											  // operator overloading
		Vector operator+(const Vector & b) const;
		Vector operator-(const Vector & b) const;
		Vector operator-() const;
		Vector operator*(double n) const;
		// friends
		friend Vector operator*(double n, const Vector & a);
		friend std::ostream & operator<<(std::ostream & os, const Vector & v);
	};

}   // end namespace VECTOR
#endif
#pragma once

vector.cpp

#include <cmath>
#include "vector.h"   // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan2;
using std::cout;

namespace VECTOR
{

	const double Rad_to_deg = 57.2957795130823;

	// private methods
	// calculates magnitude from x and y
	// set x from polar coordinate
	void Vector::set_x(double mag, double ang)
	{
		x = mag * cos(ang);
	}

	// set y from polar coordinate
	void Vector::set_y(double mag, double ang)
	{
		y = mag * sin(ang);
	}

	// public methods
	Vector::Vector()             // default constructor
	{
		x = y = 0.0;
		mode = 'r';
	}

	// construct vector from rectangular coordinates if form is r
	// (the default) or else from polar coordinates if form is p
	Vector::Vector(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
		}
		else if (form == 'p')
		{
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0.0;
			mode = 'r';
		}
	}

	// set vector from rectangular coordinates if form is r (the
	// default) or else from polar coordinates if form is p
	void Vector::set(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
		}
		else if (form == 'p')
		{
			set_x(n1, n2);
			set_y(n1, n2);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0.0;
			mode = 'r';
		}
	}

	Vector::~Vector()    // destructor
	{
	}

	void Vector::polar_mode()    // set to polar mode
	{
		mode = 'p';
	}

	void Vector::rect_mode()     // set to rectangular mode
	{
		mode = 'r';
	}

	// operator overloading
	// add two Vectors
	Vector Vector::operator+(const Vector & b) const
	{
		return Vector(x + b.x, y + b.y);
	}

	// subtract Vector b from a
	Vector Vector::operator-(const Vector & b) const
	{
		return Vector(x - b.x, y - b.y);
	}

	// reverse sign of Vector
	Vector Vector::operator-() const
	{
		return Vector(-x, -y);
	}

	// multiple vector by n
	Vector Vector::operator*(double n) const
	{
		return Vector(n * x, n * y);
	}

	// friend methods
	// multiply n by Vector a
	Vector operator*(double n, const Vector & a)
	{
		return a * n;
	}

	// display rectangular coordinates if mode is r,
	// else display polar coordinates if mode is p
	std::ostream & operator<<(std::ostream & os, const Vector & v)
	{
		if (v.mode == 'r')
			os << "(x,y) = (" << v.x << ", " << v.y << ")";
		else if (v.mode == 'p')
		{
			os << "(m,a) = (" << v.magval() << ", "
				<< v.angval() * Rad_to_deg << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}

}  // end namespace VECTOR

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

#include<iostream>
#include<cstdlib>
#include<ctime>
#include "vector.h"
using namespace std;
using VECTOR::Vector;
int main() {
	srand(time(0));
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;
	unsigned long max = 0;
	unsigned long min = 999;
	double avg = 0;
	int N;
	cout << "请输入测试的次数:N=";
	cin >> N;
	cout << "第一次:" << endl;
	int i;
	for (i = 0; i < N; i++)
	{
		cout << "Enter target distance(ONLY ENTER NUMBER): ";
		cin >> target;
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;
		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.set(dstep, direction, 'p');
			result = result + step;
			steps++;
		}
		if (max < steps)
			max = steps;
		if (min > steps)
			min = steps;
		avg += steps;
		result.polar_mode();
		steps = 0;
		result.set(0.0, 0.0);
	}
	avg /= N;
	cout << "max: " << max << endl << "min: " << min << endl << "average: " << avg << endl;
	cout << "Bye~";
	system("pause");
	return 0;
}

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

mytime3.h

#pragma once
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include<iostream>
using namespace std;
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 &t, double n);
	friend Time operator*(double m, const Time & t) { return t*m; }
	friend ostream & operator<<(ostream & os, const Time & t);
};
#endif // !MYTIME_H_

mytime3.cpp

#include<iostream>
#include "mytime3.h"
using namespace std;
Time::Time() {
	hours = minutes = 0;

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

}
void Time::AddMin(int m) {
	minutes += m;
	hours += minutes / 0;
	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) {
	Time sum;
	sum.minutes = t1.minutes + t2.minutes;
	sum.hours = t1.hours + t2.hours + sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}
Time operator-(const Time & t1, const Time & t2) {
	Time diff;
	int tot1, tot2;
	tot1 = t2.minutes + 60 * t2.hours;
	tot2 = t1.minutes + 60 * t1.hours;
	diff.minutes = (tot2 - tot1) % 60;
	diff.hours = (tot2 - tot1) / 60;
	return diff;

}

Time operator*(const Time &t, double n) {
	Time result;
	long totalminutes = t.hours*n * 60 + t.minutes*n;
	result.hours = totalminutes / 60;
	result.minutes = totalminutes % 60;
	return result;
}

ostream & operator<<(ostream & os, const Time & t) {
	os << t.hours << " Hours, " << t.minutes << " minutes.";
	return os;
}

usetime3.cpp

#include<iostream>
#include"mytime3.h"
using namespace std;
int main() {
	Time aida(3, 35);
	Time tosca(2, 48);
	Time temp;
	cout << "Aida and Tosca:\n";
	cout << aida << ";" << tosca << endl;
	temp = aida + tosca;
	temp = aida*1.17;
	cout << "Aida*1.17: " << temp << endl;
	cout << "10*Tosca: " << 10 * tosca << endl;

	system("pause");
	return 0;
}

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

stonewt.h

// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
    enum {Lbs_per_stn = 14};      // pounds per stone
    int stone;                    // whole stones
    double pds_left;              // fractional pounds
    double pounds;                // entire weight in pounds
public:
    Stonewt(double lbs);          // constructor for double pounds
    Stonewt(int stn, double lbs); // constructor for stone, lbs
    Stonewt();                    // default constructor
    ~Stonewt();
    void show_lbs() const;        // show weight in pounds format
    void show_stn() const;        // show weight in stone format
};
#endif

stonewt.cpp

// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt.h"

// construct Stonewt object from double value
Stonewt::Stonewt(double lbs)
{
    stone = int (lbs) / Lbs_per_stn;    // integer division
    pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
    pounds = lbs;
}

// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs)
{
    stone = stn;
    pds_left = lbs;
    pounds =  stn * Lbs_per_stn +lbs;
}

Stonewt::Stonewt()          // default constructor, wt = 0
{
    stone = pounds = pds_left = 0;
}

Stonewt::~Stonewt()         // destructor
{
}

// show weight in stones
void Stonewt::show_stn() const
{
    cout << stone << " stone, " << pds_left << " pounds\n";
}

// show weight in pounds
void Stonewt::show_lbs() const
{
    cout << pounds << " pounds\n";
}

stone.cpp

// stone.cpp -- user-defined conversions
// compile with stonewt.cpp
#include <iostream>
using std::cout;
#include "stonewt.h"
void display(const Stonewt & st, int n);
int main()
{
    Stonewt pavarotti = 260; // uses constructor to initialize
    Stonewt wolfe(285.7);    // same as Stonewt wolfe = 285.7;
    Stonewt taft(21, 8);

    cout << "The tenor weighed ";
    pavarotti.show_stn();
    cout << "The detective weighed ";
    wolfe.show_stn();
    cout << "The President weighed ";
    taft.show_lbs();
    pavarotti = 265.8;       // uses constructor for conversion
    taft = 325;      
    cout << "After dinner, the tenor weighed ";
    pavarotti.show_stn();
    cout << "After dinner, the President weighed ";
    taft.show_lbs();
    display(taft, 2);
    cout << "The wrestler weighed even more.\n";
    display(422, 2);
    cout << "No stone left unearned\n";
	system("pause");
    return 0;
}

void display(const Stonewt & st, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << "Wow! ";
        st.show_stn();
    }
}

7.复数有两个部分组成:实数部分和虚数部分。复数的一种书写方式是:(3.0,4.0)。请定义一个复数类,以便下面的程序可以使用它来获得正确的结果。
(……代码省略……)
注意。必须重载运算符<<和>>。标准C++使用头文件complex提供了比这个示例更广泛的复数支持,因此应将自定义的头文件命名为complex0.h,以免发生冲突。应尽可能使用const。
请注意,经过重载后,cin>>c将提示用户输入实数和虚数部分。

complex0.h

#pragma once
#include<iostream>
#ifndef COM0_H_
using namespace std;
class complex {
	double a;
	double b;
public:
	complex();
	complex(double x, double y);
	~complex();
	complex operator+(const complex & t) const;
	complex operator-(const complex & t) const;
	friend complex operator*(const complex & t,double n);//Êý³Ë
	friend complex operator*(const complex & t1, const complex & t2);
	friend complex operator~(const complex & t);
	friend ostream & operator<<(ostream & os, const complex & c);
	friend istream & operator >> (istream &in, complex &c);


};
#endif // !COM0_H_

complex0.cpp

#include "complex0.h"
#include<iostream>
complex::complex() {
	a = 0;
	b = 0;
}
complex::complex(double x, double y) {
	a = x;
	b = y;

}
complex::~complex() {}
complex complex::operator+(const complex & t) const
{
	
	complex sum;
	sum.a = a + t.a;
	sum.b = b + t.b;
	return sum;

}
complex complex::operator-(const complex & t) const
{
	complex diff;
	diff.a = a - t.a;
	diff.b = b - t.b;
	return diff;

}
complex operator*(const complex & t, double n)  //Êý³Ë
{
	return complex((t.a)*n, (t.b)*n);

}
	
complex operator*(const complex & t1, const complex & t2)
{
	return complex((t1.a*t2.a - t1.b*t2.b), (t1.a*t2.b + t1.b*t2.a));

}
complex operator~(const complex & t)
{
	return complex(t.a, (-1.0)*t.b);

}
ostream & operator<<(ostream & os, const complex & c)
{
	os << "(" << c.a << "," << c.b << "!" << ")";
	return os;

}
istream & operator >> (istream &in, complex &c)
{
	in >> c.a >> c.b;
	return in;
}

备注:

操作系统:Windows 10 x64(1803)

编译器:Microsoft Visual Studio 2015

 

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: C++学习笔记
最后更新:2018年11月18日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程小奥看房之鸿荣源珈誉府论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
腾讯C++后台一面面经(基本凉凉~) 济南市生态环境局关于作出《济莱高铁项目前期工作筹备组新建济南至莱芜高速铁路项目环境影响报告书》审批决定的公告 “我们的高中”三部曲即将开始制作发布。 使用 WordPress 页面模板 2015年5月全国大学英语四、六级口语考试报名通知 课上阅读材料有感
标签聚合
鸟哥的linux私房菜 Java Python 学习 算法 python学习 高中 生活 leetcode linux
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 8 个月前(10月20日) :wink:
niming 发布于 9 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号