c++ primer plus(第6版)中文版 第十一章编程练习答案
来源:互联网 发布:软件商业计划书模板 编辑:程序博客网 时间:2024/06/05 08:07
第十一章编程练习答案
11.1修改程序清单11.5,将一系列连续的随机漫步者位置写入文件中,另外将初始条件和结果小结写入文件
//TVector.h#include <iostream>namespace n_vector{using std::ostream;class TVector{public:enum Mode {RECT, POL};// 坐标类型,依次为直角坐标、极坐标public:explicit TVector (double a = 0, double b = 0, Mode mode = RECT);// (x, y) OR (length, angle)void reset (double a = 0, double b = 0, Mode mode = RECT);void setMode (Mode mode);Mode getMode (void) const;double getX (void) const;double getY (void) const;double getLength (void) const;double getAngle (void) const;TVector operator+ (const TVector& rvalue) const;TVector& operator+= (const TVector& rvalue);TVector operator- (void) const;private:void m_setPolar (void);void m_setRect (void);private:doublem_x;// 直角坐标的X坐标doublem_y;// 直角坐标的Y坐标doublem_length;// 极坐标的长度doublem_angle;// 极坐标的角度Modem_mode;};ostream& operator<< (ostream& os, const TVector& vec);}
//TVector.cc#include <iostream>#include <cmath>#include "TVector.h"namespace n_vector{static const doublek_rad_to_deg = 57.2958;static double convertRadToDeg (double rad){return (rad * k_rad_to_deg);}static double convertDegToRad (double deg){return (deg / k_rad_to_deg);}TVector::TVector (double a, double b, Mode mode) : m_mode(mode){if (RECT == mode) {m_x = a;m_y = b;m_setPolar();} else if (POL == mode) {m_length = a;m_angle = convertDegToRad(b);m_setRect();} else {;}}void TVector::reset (double a, double b, Mode mode){*this = TVector(a, b, mode);}void TVector::setMode (Mode mode){m_mode = mode;}TVector::Mode TVector::getMode (void) const{return (m_mode);}double TVector::getX (void) const{return (m_x);}double TVector::getY (void) const{return (m_y);}double TVector::getAngle (void) const{return (convertRadToDeg(m_angle));}double TVector::getLength (void) const{return (m_length);}TVector TVector::operator+ (const TVector& rvalue) const{TVectortmp(m_x + rvalue.m_x, m_y + rvalue.m_y);tmp.m_mode = this->m_mode;return (tmp);}TVector& TVector::operator+= (const TVector& rvalue){*this = *this + rvalue;return (*this);}TVector TVector::operator- (void) const{TVectortmp(-m_x, -m_y);tmp.m_mode = this->m_mode;return (tmp);}void TVector::m_setPolar (void){m_length = sqrt(m_x * m_x + m_y * m_y);m_angle = (0 == m_x && 0 == m_y) ? 0 : atan2(m_y, m_x);}void TVector::m_setRect (void){m_x = m_length * cos(m_angle);m_y = m_length * sin(m_angle);}ostream& operator<< (ostream& os, const TVector& vec){if (TVector::RECT == vec.getMode())os << '(' << vec.getX() << ',' << vec.getY() << ')';else os << '(' << vec.getLength() << ',' << vec.getAngle() << "°)";return (os);}}
//mian.cc#include <iostream>#include <fstream>#include <limits>#include "TVector.h"using namespace std;using namespace n_vector;int main () { ofstreamof;of.open("output.txt");while (true) {doublesetp;doubledistance;unsignedcnt_steps = 0;cout << "输入步长:";cin >> setp;if (!cin || setp <= 0)break;cin.clear();// 清空输入缓冲区错误标志位cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');// 清空输入缓冲区内容cout << "输入距离:";cin >> distance;if (!cin || distance <= 0)break;cin.clear();// 清空输入缓冲区错误标志位cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');// 清空输入缓冲区内容of << "开始>>>>>>>>>>" << endl;TVectorvec(0, 0, TVector::POL);srand((unsigned)time(nullptr));while (vec.getLength() < distance) {vec += TVector(setp, rand()%361, TVector::POL);of << cnt_steps++ << ":" << vec << endl;}of << "结束<<<<<<<<<<" << endl;of << "距离" << distance << ",步长" << setp << ",历经" << cnt_steps << "步走完,终点坐标" << vec << endl;of << "==========\n" << endl;}of.close();}
2,3,4,5,6题都为改编书中已有例子,实在是改不下去了。借先人例子来看
http://blog.csdn.net/qq844352155/article/details/24111645
http://blog.csdn.net/qq844352155/article/details/24112981
http://blog.csdn.net/qq844352155/article/details/24125767
http://blog.csdn.net/qq844352155/article/details/24128929
http://blog.csdn.net/qq844352155/article/details/24130805
11.7编写一个复数类,对复数进行运算
//11.7编写一个复数类,对复数进行运算,复数表示方法为(a,b)#include <iostream>using namespace std;class Complex{double a;double b;public:Complex (const double x=0,const double y=0) : a(x),b(y) {}friend ostream & operator << (ostream & os , const Complex & complex){os << "(" << complex.a << " , " << complex.b << ")" ;return (os);}friend istream & operator >> (istream & is , Complex & complex){char ch;is >> complex.a >> ch >> complex.b ;return (is);}Complex operator +(const Complex & complex){return Complex(a+complex.a,b+complex.b);}Complex operator -(const Complex & complex){return Complex(a-complex.a,b-complex.b);}Complex operator *(const Complex & complex){return Complex((a*complex.a - b*complex.b),(a*complex.b + b*complex.a));}friend Complex operator *(int x,const Complex & complex){return Complex(x * complex.a , x * complex.b);}friend Complex operator ~(const Complex & complex){return Complex(complex.a,-complex.b);}};int main(){Complex a(3.0, 4.0);// initialize to (3,4i) Programming ExercisesComplex c;cout << "Enter a complex number (eg:a,b)(q to quit):\n";while (cin >> c){cout << "c is " << c << endl;cout << "complex conjugate is " << ~c << '\n';cout << "a is " << a << endl;cout << "a + c is " << a + c << endl;cout << "a - c is " << a - c << endl;cout << "a * c is " << a * c << endl;cout << "2 * c is " << 2 * c << endl;cout << "Enter a complex number (eg:a,b)(q to quit):\n";}cout << "Done!" << endl;}
5 0
- c++ primer plus(第6版)中文版 第十一章编程练习答案
- C ++ Primer Plus 第六版 第十一章编程练习答案
- c++ primer plus(第6版)中文版 第二章编程练习答案
- c++ primer plus(第6版)中文版 第三章编程练习答案
- c++ primer plus(第6版)中文版 第四章编程练习答案
- c++ primer plus(第6版)中文版 第五章编程练习答案
- c++ primer plus(第6版)中文版 第六章编程练习答案
- c++ primer plus(第6版)中文版 第七章编程练习答案
- c++ primer plus(第6版)中文版 第八章编程练习答案
- c++ primer plus(第6版)中文版 第九章编程练习答案
- c++ primer plus(第6版)中文版 第十章编程练习答案
- c++ primer plus(第6版)中文版 第十二章编程练习答案
- c++ primer plus(第6版)中文版 第十三章编程练习答案
- C++Primer Plus第6版 4.13编程练习答案
- C++ Primer Plus第六版 第十一章 编程练习答案
- 《C Primer Plus(第5版)中文版》第6章编程练习第1题
- 《C Primer Plus(第5版)中文版》第6章编程练习第14题
- 《C Primer Plus(第5版)中文版》第6章编程练习第15题
- 音乐播放器通知栏切歌(Notification+BroadcastReceiver)
- hdu 3592 World Exhibition
- leetcode第一刷_Swap Nodes in Pairs
- English expressions about clothes
- 第12周-项目4-时间日期类
- c++ primer plus(第6版)中文版 第十一章编程练习答案
- hdu 1867 A + B for you again
- [leetcode]Evaluate Reverse Polish Notation
- hdu 1534 Schedule Problem
- java.lang.UnsatisfiedLinkError解决方法汇集
- andorid常见需求修改
- 【MySQL笔记】mysql源码安装/配置步骤及支持中文gbk/gb2312编码的配置方法
- 【Yii】YII执行流程
- 《C和指针》——C语言补漏(基本用法篇)