c++ - 我如何将一个类合并到这个随机生成器中?

标签 c++ class oop

我想添加选项以将 Integer 和 Double 类包含到我的项目中,并且由于我遇到困难而需要一些帮助。

标题:

#ifndef RANDOM
#define RANDOM

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include "Double.h"

const int Int_b = 250;//these are for main
const int Int_s = 225;

class Random
{
private:
    std::vector<double> vectx;
    double _min, _max;
    int currIndex;
    void fillVect(double min, double max);
    //void fillVect(Double min, Double max);
    void shuffle();

public:
    Random();
    Random(double min, double max);
    //Random(Double min, Double max);
    Random(int seed);
    int nextInt();
    //Integer nextInteger();
    double nextDbl();
    //Double nextDouble();
    //void setRange(Double min, Double max);
    void setRange(double min, double max);
};
#endif

注释的代码是我想添加的代码

cpp:

#include "Random.h"
#include "Double.h"
using namespace std;
Random::Random() {
    srand(unsigned(time(0)));
    fillVect(0.0, RAND_MAX);
}
Random::Random(double min, double max) {
    srand(unsigned(time(0)));
    fillVect(min, max);
}
/*Random::Random(Double min, Double max)
{
    srand(unsigned(time(0)));
    fillVect(min, max);
}*/
Random::Random(int seed) {
    srand(seed);
    fillVect(0.0, RAND_MAX);
}
void Random::fillVect(double min, double max) {
    vectx.clear();
    currIndex = 0;
    for (unsigned i = 0; i < Int_b; ++i)
    {
        double r = (((double)rand() / (double)RAND_MAX * (max - min)) + min);
        vectx.push_back(r);
    }
    shuffle();
}
/*void Random::fillVect(Double min, Double max) {
    vectx.clear();
    currIndex = 0;
    for (unsigned i = 0; i < Int_b; ++i)
    {
        Double r = (((Double)rand() / (Double)RAND_MAX * (max - min)) + min);
        vectx.push_back(r);
    }
    shuffle();
}*/
void Random::shuffle() {
    random_shuffle(vectx.begin(), vectx.end());
}
int Random::nextInt() {
    if (currIndex > Int_s)
    {
        currIndex = 0;
        shuffle();
    }
    return (int)vectx[currIndex++];
}
double Random::nextDbl() {
    if (currIndex > Int_s)
    {
        currIndex = 0;
        shuffle();
    }
    return vectx[currIndex++];
}
void Random::setRange(double min, double max) {
    vectx.clear();
    fillVect(min, max);
}

这是我的尝试,但我无法理解它。

这是我的双人类。

class Double
{
private:
    double data;
public:
    Double();
    Double(double d);
    Double(const Double &d);
    Double(const Integer &i);
    void equals(double d);
    Double add(const Double &d);
    Double sub(const Double &d);
    Double mul(const Double &d);
    Double div(const Double &d);
    Double add(double d);
    Double sub(double d);
    Double mul(double d);
    Double div(double d);
    double toDouble() const;
    Double operator + (const Double &d);
    Double operator - (const Double &d);
    Double operator * (const Double &d);
    Double operator / (const Double &d);
    Double operator = (const Double &d);
    Double operator = (double d);
    bool operator == (const Double &d);
    bool operator == (double d);
    bool operator != (const Double &d);
    bool operator != (double d);
};

最佳答案

Random::Random(Double min, Double max)

你能做的最好的就是

Random::Random(Double min, Double max)
{
    fillVect(min.toDouble(), max.toDouble());
}

并丢弃 void Random::fillVect(Double min, Double max) 的多余概念。但如果你必须实现它,那么

Random::Random(Double min, Double max)
{
    fillVect(min, max);
}

void Random::fillVect(Double min, Double max) {
    vectx.clear();
    currIndex = 0;
    for (unsigned i = 0; i < Int_b; ++i)
    {
        double r = (((double)rand() / (double)RAND_MAX * (max.toDouble() - min)) + min.toDouble());
        vectx.push_back(r);
    }
    shuffle();
}

但请注意,这是愚蠢的。对 fillVect 算法所做的任何更改都必须在两个地方进行,这会带来一系列可能的错误。假设您更改了一个而忘记更改另一个。或者你发现了一个错误,只在一个中修复它。或者由于打字错误,您只有一个错误。重复的代码会浪费时间编写、调试和维护。为了什么?因此,您在不同的地方调用了 toDouble(并且可能调用了很多次,因为现在它在一个 for 循环中。阅读“循环提升”来解决这个问题)?

对于Double nextDouble();,利用Double::Double(double d)

关于c++ - 我如何将一个类合并到这个随机生成器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39970975/

相关文章:

c++ - Qt:如何调整QGraphicsItem动态改变大小

c++ - 通过 new[] 分配 n 个字节并用任何类型填充它?

c++指针赋值运算符重载(不仅是对象赋值,还有指针赋值)

class - 工厂设计模式和管道设计模式有什么区别?

java - 找不到符号,如何编写自定义异常类

c++ - 嵌套类对象的工作?

c++ - 文件线程

c++ - 结构中的类对象数组

java - 我将如何重载接口(interface)中的方法?

c++ - 在 C++ 中通过析构函数销毁动态数组的 vector