摘要: 阅读全文

这是一个定义的一个矢量类, 然后用矢量类模拟一个酒鬼的随机漫步

问题很简单, 实现也不麻烦, 但是这个小程序却可以呈现出许多语法知识。而且代码风格也不错,因此保存在了这篇博客中。

建议:

1. 类的声明以及函数的声明放到一个文件夹内, 并且在一些必要的地方加上注释!

   2. 函数的实现放到另一个文件内。

3. 将程序要具体解决的问题放到另外的一个文件里。(详见代码!)

好处: 把类的接口和实现细节分离开, 易于更改某个函数的功能。

把函数的声明和定义分开, 提高代码可读性。

把类的声明和定义 与 要解决的问题分开, 提高,类的重用性!

定义类(声明类内的函数)

// vect.h -- Vector class with <<, mode state
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{class Vector{public:enum Mode{RECT, POL};// RECT for rectangular, POL for Polar modesprivate:double x;        // horizontal valuedouble y;        // vertical valuedouble mag;      // length of vector in degreesdouble ang;// direction of vector in degreesMode mode;  // private methods for setting valuesvoid set_mag();void set_ang();void set_x();void set_y();public:Vector();Vector(double n1, double n2, Mode form = RECT);void reset(double n1, double n2, Mode form = RECT);~Vector();double xval() const {return x; }        // report x valuedouble yval() const {return y; }        // report y valuedouble magval() const {return mag; }    // report magnitudedouble angval() const {return ang; }    // report anglevoid polar_mode();                      // set mode to POLvoid rect_mode();                       // set mode to RECT// operator overloadingVector operator+(const Vector & b) const;Vector operator-(const Vector & b) const;Vector operator-()const;Vector operator*(double n) const;// friendsfriend Vector operator*(double n, const Vector & a);friend std::ostream & operator<<(std::ostream & os, const Vector & v);};
}    // end namespace VECTOR
#endif

上面代码涵盖了许多关于类的基础知识:  名称空间与作用域 ,  实现类内部常量的方法,  构造函数, 析构函数, 运算符重载, 友元函数, 关于多种实现方法等。

PS: 实现类内部常量的方法: (1)枚举类型。 (2) static const int  常量

运算符重载: 注意操作数的顺序。

函数定义

// vect.cpp -- methods for the Vector class
#include <cmath>
#include "vect.h"  // include <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;namespace VECTOR
{// compute degree in one radianconst double Rad_to_deg = 45.0/atan(1.0);// should be about 57.2957795130823// private methods// calculate magnitude from x and yvoid Vector::set_mag(){mag = sqrt(x*x + y*y);}void Vector::set_ang(){if(x == 0.0&& y== 0.0)ang = 0.0;else ang = atan2(y, x);}//set x from polar coordinatevoid Vector::set_x(){x = mag*cos(ang);}//set y from polar coodinatevoid Vector::set_y(){y = mag * sin(ang);}// public methodsVector::Vector() // default constructor
    {x = y = mag = ang = 0.0;mode = RECT;}// construct vector from rectangular coordinates if form is r// (the default) or else from polar coordinates if form is pVector::Vector(double n1, double n2, Mode form){mode = form;if(form == RECT){x = n1;y = n2;set_mag();set_ang();}else if(form == POL){mag = n1;ang = n2/Rad_to_deg;set_x();set_y();}else{cout << "Incorrect 3rd argument to Vector() --";cout << "vector set to 0.0";mode = RECT;}}// reset vector from rectangular coodinates if form is// RECT (the default) or else form polar coordinates if// form is POLvoid Vector::reset(double n1, double n2, Mode form){mode = form;if(form == RECT){x = n1;y = n2;set_mag();set_ang();}else if (form == POL){mag = n1;ang = n2/Rad_to_deg;set_x();set_y();}else{cout << "Incorrect 3rd argument to Vector() -- ";cout <<"vector set to 0.0\n";x = y = mag = ang = 0.0;mode = RECT;}}Vector::~Vector() // destructor
    {}void Vector::polar_mode()  //set to polar mode
    {mode = POL;}void Vector::rect_mode()// set to rectangular mode
    {mode = RECT;}// operator overloading// add two VectorsVector 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 VectorVector Vector::operator-() const{return Vector(-x, -y);}// multiply vector by nVector Vector::operator*(double n) const{return Vector(n*x, n*y);}// friend methods// multiply n by Vector aVector operator*(double n, const Vector & a){return a * n;}//display rectangular coordinates if mode is RECT// else display polar coordinates if mode is POLstd::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.mag << ", "<< v.ang*Rad_to_deg << ")"; }elseos << "Vector object mode is invalid";return os;}
}  // end namespace VECTOR

具体解决的问题

// randwalk.cpp -- using the Vector class
// compile with the vect.cpp file
#include <iostream>
#include <cstdlib> // rand(), srand() pototypes
#include <ctime>   // time() pototype
#include "vect.h"int main()
{using namespace std;using VECTOR::Vector;srand(time(0));   // seed random-number generatordouble 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 subject ""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;
}

二。一个简易的string类。

锻炼内容:

拷贝构造函数(深拷贝与浅拷贝)

重载赋值运算符(深赋值)

许多的细节与技巧!

类的声明

//sting1.h -- fixed and augmented string class definition
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using std::ostream;
using std::istream;class String
{
private:char * str;                            // pointer ot stringint len;                            // length of stringstatic int num_strings;                // number of objectsstatic const int CINLIM = 80;        // cin input limit
public:// construction and other methodsString(const char * s);             // constructorString();                            // default constructorString(const String &);                // copy constructor~String();                            // destructor int length() const { return len; }// overloaded operator methodsString & operator=(const String &);String & operator=(const char *);char & operator[](int i);const char & operator[](int i)const;// overloaded operator friendsfriend bool operator<(const String &st, const String &st2);friend bool operator>(const String &st1, const String &st2);friend bool operator==(const String &st, const String &st2);friend ostream & operator<<(ostream & os, const String & st);friend istream & operator>>(istream & is, String & st);//static functionstatic int HowMany();
};
#endif

View Code

类方法的实现。

// string1.cpp -- String class methods
#include <cstring>                    // string.h for some
#include "string1.h"                // includes <iostream>
using std::cin;
using std::cout;// initializing static class member
int String::num_strings = 0;// static method
int String::HowMany()
{return num_strings;
}// class methods
String::String(const char * s)        // construct String from C string
{len = std::strlen(s);             // set sizestr = new char[len + 1];        // allot storagestd::strcpy(str, s);            // initialize pointernum_strings++;                     // set object count
}String::String()                    // default constructor
{len = 4;str = new char[1];str[0] = '\0';                    // default stringnum_strings++;
}String::String(const String & st)
{num_strings++;                     // handle static member updatelen = st.len;                    // same lengthstr = new char [len + 1];        // allot space std::strcpy(str, st.str);        // copy string to new location
}String::~String()                      // necesserary destructor
{--num_strings;                    // requireddelete [] str;                     // required
}// overloaded operator methods// assign a String to a String
String & String::operator=(const String & st)
{if(this == &st)return *this;delete [] str;len = st.len;str = new char[len + 1];std::strcpy(str, st.str);return *this;
}// assign a C string to a String
String & String::operator=(const char * s)
{delete [] str;len = std::strlen(s);str = new char[len + 1];std::strcpy(str, s);return *this;
}// read-write char access for non-const String
char & String::operator[](int i)
{return str[i];
}// read-only char access for const string
const char & String::operator[](int i) const
{return str[i];
}// averloaded operator friendsbool operator<(const String &st1, const String &st2)
{return (std::strcmp(st1.str, st2.str) < 0);
}bool operator>(const String &st1, const String &st2)
{return st2 < st1;
}bool operator==(const String &st1, const String &st2)
{return (std::strcmp(st1.str, st2.str)==0);
}// simple String output
ostream & operator<<(ostream & os, const String & st)
{os << st.str;return os;
}// quick and dirty String input
istream & operator>>(istream & is, String & st)
{char temp[String::CINLIM];is.get(temp, String::CINLIM);if(is)st = temp;while (is && is.get() != '\n')continue;return is;
}

View Code

main(), 测试String类。

// saying1.cpp -- using expanded String class
// complile with string1.cpp
#include <iostream>
#include "string1.h"
const int MaxLen = 81;
int main()
{using std::cout;using std::cin;using std::endl;String name;cout << "Hi, what's your name?\n>> ";cin >> name;cout << name << ", please enter up to " << ArSize<< " short sayings <empty line to quit>:\n";String sayings[ArSize];         // array of objectschar temp[MaxLen]                // temporary string storageint i;for (i = 0; i < ArSize; i++){cout << i + 1 << ": ";cin.get(temp, MaxLen);while(cin && cin.get()!='\n')continue;if(!cin||temp[0] == '\0')    // empty line?break;                    // i not increamentedelsesayings[i] = temp;        // overloaded assignment
    }int total = i;                     // total # of lines readif( total > 0){cout << "Here are your sayings:\n";for (i = 0; i < total; i++)cout << sayings[i][0] << ": " << sayings[i] << endl;int shortest = 0;int first = 0;for(i = 1; i < total; i++){if(sayings[i].length() < sayings[shortest].length())shortest = i;if(sayings[i] < sayings[first])first = i;}cout << "Shortest saying:\n" << sayings[shortest] << endl;cout << "First alphabetically:\n" << sayings[first] << endl;cout << "This program used " << String::HowMany()<< " String objects. Bye.\n"}elsecout << "No input! Bye.\n";return 0;
}

View Code

代码源自: C++ Primer Plus  。 小恪亲自敲写!

感悟:  如果一部书经久不衰, 一定是有它的理由的! 正如这部书,  内容细致而深刻, 全面而严谨。获益良多!此书有点儿厚,与诸君共勉。

转载于:https://www.cnblogs.com/acm1314/p/4854000.html

酒鬼随机漫步(一个矢量类)相关推荐

  1. 再谈重载:一个矢量类

    矢量 用户修改了矢量的一种表示后,对象将自动更新另一种表示.使对象有这种智能.该清单将类声明放在VECTOR名称空间中.该程序使用枚举创建两个常量(RECT和POL),用于标识两种表示法. vecto ...

  2. c++学习日记之使用类来模拟随机漫步

    本文主要介绍运算符重载和友元的类设计,通过定义一个矢量类来模拟随机漫步.矢量类分两种模式,直角坐标模式和极坐标模式.该函数允许用户选择行走的距离和补偿.该程序用一个变量表示位置,并报告到达指定距离住所 ...

  3. python随机画散点图-python散点图实例之随机漫步

    随机漫步是这样行走得到的途径:每次行走都是完全随机的,没有明确的方向,结果是由一系列随机决策决定的. random_walk.py #random_walk.py from random import ...

  4. 【Pyhton】随机漫步散点图

    random_walk.py from random import choiceclass RandomWalk():"""初始化随机漫步数据的类"" ...

  5. 【Matplotlib】【Python】如何使用matplotlib绘制绘制随机生成的点--随机漫步详解

    目录 1.绘制随机漫步 2.模拟多次随机漫步 3.给点着色 4.重新绘制起点和终点 5.隐藏坐标轴 随机漫步是指每次行走都完全是随机的,没有明确的方向,结果是由一系列随机决策决定的. 为模拟随机漫步, ...

  6. 【Python-3.5】绘制随机漫步图

    代码中我们定义两个模型,一个是RandomWalk.py模型,用于随机的选择前进方向.此模型中的RandomWalk类包含两个方法,一个是__init__(),一个是fill_walk(),后者是计算 ...

  7. 【Python】随机漫步

    欢迎来到博主 Apeiron 的博客,祝您旅程愉快 !时止则止,时行则行.动静不失其时,其道光明. 目录 1.缘起 2.创建 RandomWalk 类 3.随机选择方向 4.绘制随机漫步图 5.总结 ...

  8. chatGPT 生成随机漫步代码

    目录 1.  chatGPT 写的随机漫步代码 2.  笔者写的随机漫步代码 3.  总结 1.  chatGPT 写的随机漫步代码 最近在学习 Python 中的 随机漫步 知识点,突发奇想,心血来 ...

  9. 一个java随机数据的工具类

    获得随机数据工具类 在一些业务还有测试中可能需要一些有实际意义的测试数据,编写这些数据可能比较头大,这里写了一个随机数据的工具类,具有以下功能: 获得指定长度的随机大写.小写.数字字母 获得随机的性别 ...

最新文章

  1. Python代码注释
  2. python安装教程mac-Mac 上安装python3——手把手教程
  3. 一次性动态绑定多个droplistdown
  4. 基于Vue-SSR优化方案归纳总结
  5. 名校教授:把研究生扔到河里,游过去的就成为博士
  6. react 图片放在src里面还是public_手写Webpack从0编译Vue/React项目
  7. 解决导出CSV后在EXCEL打开纯数字前面0丢失问题
  8. 【Qt开发】关于QWSServer
  9. CesiumJS 中文学习手册
  10. putty使用ssh登录时出现Access denied
  11. 我研究了3年,终于找出2021年完美的听歌方案!
  12. 只有英语四级和计算机二级,通过英语四级和计算机二级用英语怎么说
  13. 《数据通信与网络》笔记--广域网SONET/SDH
  14. 园区网络—中小型企业网络工程项目实践(思科模拟器)
  15. 关于放大器失真的原因你了解多少呢?
  16. 统计单词个数———有穷状态机编程
  17. 海量数据处理 大量数据中找出最大的前10个数 (Top K 问题)
  18. Think PHP 提示验证码输入错误
  19. html 怎么做分屏效果,纯css实现水平方向分屏和垂直方向分屏
  20. Trunking学习总结

热门文章

  1. 2d的公式_旋转之二 - 三维空间中的旋转:罗德里格旋转公式
  2. 软件工程 --第七章 -- 实现(未完)
  3. 【算法竞赛学习】学术前沿趋势-论文作者统计
  4. QT 文字字体和颜色设置
  5. Windows XP 系统 使用笔记一
  6. NoSQL数据库Redis使用命令简介
  7. 梯度下降优化方法'原理_优化梯度下降的新方法
  8. 经典的卷积神经网络简介
  9. 贷款机构如何审核个人征信?
  10. 说两句电视剧《东归英雄》