C++ 类和函数输出问题

标签 c++ function class cout

我的程序输出有问题。一直吐出12345。

详情如下: 它分为三个文件:program8.cpp(运行测试的部分)、myRandom.cpp(类的实现)和 myRandom.h(类的规范)。

myRandom.h:

#ifndef MYRANDOM_H_
#define MYRANDOM_H_
class myRandom
{
    public:
            myRandom();                         //Constructor
            ~myRandom();                        //Destructor
            void seed(unsigned long theSeed);   //Mutator for current
            unsigned long next();               //Mutator or Accessor for current
            int randInt(int start, int end);    //Scales result to a range
            double randNormal();                //Future expansion
    private:
            unsigned long current;                      //Current random #
            static const unsigned long a = 1103515245;  //Multiplier for LGC
            static const unsigned long c = 12345;       //Increment for LGC
            static const unsigned long m = 2147483648;  //Modulus for LGC
};
#endif /* MYRANDOM_H_ */

我的随机.cpp:

#include <iostream>
#include <cstdlib>
#include "myRandom.h"

using namespace std;

myRandom::myRandom()                                        //Constructor
{
    current = 0;
}

myRandom::~myRandom()                                       //Destructor
{
}

void myRandom::seed(unsigned long theSeed)                  //Mutator for current
{
    if (theSeed < 0 || theSeed > m-1)
    {
        // ERROR
        return;
    }
    else
        current = theSeed;
}

unsigned long myRandom::next()                              //Mutator or Accessor for current
{
    if (current < 0)
    {
        cout << "Error: cannot set seed to a negative number" << endl;
        return 0;

    }
    else
    {
        current = (m*current+c)%m;                              //Formula
        return current;
    }
}

int myRandom::randInt(int start, int end)                   //Scales result to a range
{
    if (start >= end)
    {
        cout << "Error: cannot set start greater than or equal to end" << endl;
        return 0;
    }
    else
    {
        return ((this->next() % (end - start)) + start);
    }

}

double myRandom::randNormal()                               //Future expansion
{
    cout << "Warning: randNormal not implemented" << endl;
    return 0;
}

program8.cpp:

#include <iostream>
#include <cstdlib>
#include "myRandom.h"

using namespace std;

int main()
{
    myRandom theRand;
    unsigned long theSeed;

    cout << "Verify that the sequence generated by next() is the same on each run" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << theRand.next() << endl;
    }

    cout << "Verify that you can set the seed to 0 and 1" << endl;
    theSeed = 0;
    cout << theRand.next() << endl;
    theSeed = 1;
    cout << theRand.next() << endl;

    cout << "Verify that attempting to set the seed to -1 generates an error" << endl;
    theSeed = -1;
    cout << theRand.next() << endl;

    cout << "Verify that you can set the seed to m-2 and m-1" << endl;
    theSeed = 2147483648-2;
    cout << theRand.next() << endl;
    theSeed = 2147483648-1;
    cout << theRand.next() << endl;

    cout << "Verify that attempting to set the seed to m generates and error" << endl;
    theSeed = 2147483648;
    cout << theRand.next() << endl;

    cout << "Verify that next() produces a sequence predicted by hand/calc for the chosen seed" << endl;
    cout << "Please enter a seed: ";
    cin >> theSeed;
    cout << theRand.next() << endl;

    cout << "Verify that using start == end generates and error. Set both to 10." << endl;
    theRand.randInt(10,10);
    cout << theRand.next() << endl;

    cout << "Verify that using start > end generates and error. Set start to 10 and end to 5." << endl;
    theRand.randInt(10,5);
    cout << theRand.next() << endl;

    theRand.seed(theSeed);
    cout << "Testing randInt for start=0 end=1,000" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << theRand.randInt(0 , 1000) << endl;
    }

    return 0;
}

我认为问题在于 next() 函数,因为它一直在 program8.cpp cout 语句中被调用。我可以理解得到 12345 一次,但是一旦该函数连续运行一次就应该更新它。如果这是一个愚蠢的问题,我深表歉意。感谢您的时间和耐心等待。

最佳答案

您的问题不是特定于代码的问题 - 它与这里的数学相关:

current = (m*current+c)%m;

总是返回 c 的值如果c < m ,否则(或更一般地)它返回 c % m .为什么?从这个定理:

(m*n + a)%m = a

例子:

m = 10
n = 3
a = 7
(10*3 + 7)%10 = 7

查看更多:

http://en.wikipedia.org/wiki/Modulo_operation

关于C++ 类和函数输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751464/

相关文章:

c++ - 为什么 sleep() 会阻塞 std::ostream

function - 在 Lisp 中乘以 2 个列表

java - 满足字符串选择条件,不调用类

java - Java 中私有(private)静态嵌套类中的访问修饰符

c++ - 有助于理解函数对象的工作原理?

c++ - 没有明显目标的 std::cout 留在发布的代码中是一件坏事吗?

c++ - FreeGLUT 窗口在启动时消失

javascript - 一个函数,它将在数组中搜索字母并返回这些字母所在的位置

swift - 使用通用 where 条件和指定参数类型有什么区别?

java - 从 .java 文件创建 .class 文件 - J2ME