gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:52 25 4
gpt4 key购买 nike

我想添加选项以将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com