【发布时间】:2017-02-19 14:59:40
【问题描述】:
我想在我的项目中添加包含 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);
};
【问题讨论】:
-
课程已经制作好了,我只是想把它们插入。
-
srand(unsigned(time(0)));在构造函数中是个坏主意。每次创建该类的新实例时,您都将使用rand为该类的所有实例和程序中的任何其他人重新植入 RNG。可能导致有趣的错误。srand应该只被调用一次,可能在main的早期。 -
需要查看
Double课程才能提出任何有意义的建议。 -
你遇到了什么问题?你看到错误了吗?还是对结果不满意?哪一个以及如何?
-
我没有遇到问题,因为我什至不知道从哪里开始。我是否只是复制没有类的函数,然后用类替换它们?