博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Chapter 11 homework
阅读量:5987 次
发布时间:2019-06-20

本文共 5236 字,大约阅读时间需要 17 分钟。


1.
#include 
#include
#include
#include "vector.h"int main(){ using namespace std; using VECTOR::Vector; ofstream of; of.open("result.txt"); srand(time(0)); double direction; Vector step; Vector result(0.0,0.0); unsigned int steps = 0; double target; double dstep; cout <<"Enter target distance ( q to quit ): "; cin >> target; cout << "Enter step length: "; cin >> dstep; of << "Target Distance : " << target << ", Step Size: " << dstep << endl; for(steps = 0; result.magval() < target; steps++) { direction = rand() % 360; step.reset (dstep, direction, Vector::POL); result = result + step; of << steps << ": "<< result << endl; } of << "After " << steps << " steps, the subject has the following location:\n"; of << result <

======================================== 答案 =========================================

#include 
#include
#include
#include "TVector.h"using namespace std;using namespace n_vector;intmain (void) { ofstream of;of.open("output.txt");while (true) {double setp;double distance;unsigned cnt_steps = 0;cout << "输入步长:";cin >> setp;if (!cin || setp <= 0) {break;}cin.clear(); // 清空输入缓冲区错误标志位cin.ignore(std::numeric_limits
::max(), '\n'); // 清空输入缓冲区内容cout << "输入距离:";cin >> distance;if (!cin || distance <= 0) {break;}cin.clear(); // 清空输入缓冲区错误标志位cin.ignore(std::numeric_limits
::max(), '\n'); // 清空输入缓冲区内容of << "开始>>>>>>>>>>" << endl;TVector vec(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();cout << endl;return (0);}

 


2.答案

double setp; double distance; int times; int max_setps = 0; int min_setps = numeric_limits
::max(); int sum_setps = 0;
......
for (int i = 0; i < times; ++i) {            int cnt_steps = 0;            TVector vec(0, 0, TVector::POL);            cout << i << ":";            while (vec.getLength() < distance) {                vec += TVector(setp, rand()%360, TVector::POL);                ++cnt_steps;            }            cout << cnt_steps << "steps to " << vec << endl;            if (cnt_steps > max_setps) {                max_setps = cnt_steps;    // get max step            }            if (cnt_steps < min_setps) {                min_setps = cnt_steps;    // get min step            }            sum_setps += cnt_steps;       // total steps        }

 7.  11_7_complex0.h


#ifndef E11_7_COMPLEX0_H#define E11_7_COMPLEX0_H#include 
class Complex{private: double m_a; double m_c;public: Complex(); //default constructor ~Complex(); Complex(double x1, double y1 ); // constructor //operator overloading Complex operator+(const Complex & t) const; Complex operator-(const Complex & t) const; Complex operator*(double n) const; Complex operator*(const Complex & t) const; Complex operator~() const; //friend friend std::ostream & operator<<(std::ostream & os, const Complex & v); friend std::istream & operator>>(std::istream & is, Complex & t); friend Complex operator*(double m, const Complex & t);};#endif //E11_7_COMPLEX0_H
complex0.h

11_7_comlpex0.cpp


#include "complex0.h"using std::endl;Complex::Complex(){    m_a = m_c = 0.0;}Complex::~Complex() {}Complex::Complex(double x1, double y1){    m_a = x1;    m_c = y1;}Complex Complex::operator+(const Complex &t) const{    return Complex(t.m_a + m_a, t.m_c + m_c);}Complex Complex::operator-(const Complex & t) const{    return Complex(m_a - t.m_a, m_c - t.m_c);}Complex Complex::operator*(double n) const{    return Complex(m_a * n, m_c * n);}Complex Complex::operator*(const Complex & t) const{    return Complex(t.m_a * m_a - t.m_c * m_c, t.m_c * m_a + t.m_a * m_c);}Complex Complex::operator~() const{    return Complex(m_a, -m_c);}//friendstd::ostream &operator<<(std::ostream & os, const Complex & v){    os << "( " << v.m_a <<", " << v.m_c <<"i )" << endl;}std::istream &operator>>(std::istream & is, Complex & t){    std::cout << "Real: ";    is >> t.m_a;    std::cout << "Imaginary: ";    is >> t.m_c;}Complex operator*(double m, const Complex & t){    return Complex(t.m_a * m, t.m_c * m);}
complex0.h

11_7_main.cpp


#include 
using namespace std;#include "complex0.h"int main(){ Complex a(3.0, 4.0); 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;}
main.cpp

 

转载于:https://www.cnblogs.com/TadGuo/p/8590298.html

你可能感兴趣的文章
比特币是什么,为什么那么多人对此趋之若鹜?
查看>>
sdkman这个工具好用吗?
查看>>
HDD-FAT32 ZIP-FAT32
查看>>
Navicat MySQL连接Linux下MySQL的问题解决方案
查看>>
[日常] MySQL的哈希索引和原理研究测试
查看>>
拍照之外, 游戏手机会成为手机新品类吗?
查看>>
Lync 小技巧-1-解决搜索不到联系人的方法
查看>>
数据仓库入门(实验6)添加层次结构
查看>>
第一次获得Microsoft MVP应该做的事
查看>>
用OSSIM发现网络扫描
查看>>
IT群侠传第四回大鱼小虾
查看>>
10分钟搭建Kubernetes容器集群平台(kubeadm)
查看>>
我的家庭私有云计划-18
查看>>
当我们谈论知识管理时,我们在谈论什么?
查看>>
我是这样看搜狗搜索与知乎合作的
查看>>
演示:为思科29系列的交换机升级IOS镜像
查看>>
统一沟通-技巧-4-让国内域名提供商“提供”SRV记录
查看>>
一次DPM备份Exchange DAG的故障处理过程
查看>>
Windows Server 2012 NIC Teaming配置实战
查看>>
【马哥教育视频】Linux平台软件包管理系列视频
查看>>