2

所以我刚开始接触编程,我不得不说这是我做过的最有成就感但最令人沮丧的事情。我正在承担难度越来越大的项目,其中最近的项目涉及使用蒙特卡洛方法和大量循环。以下是目前完成的代码:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;




int main ()
{
    srand (time(0));
    string operation;
    cout << "Using the letters 'o', or 'q', please indicate if you would like to simulate once, or quit the program: " << endl;
    cin >> operation;
    string reservoir_name; // Creating variables for reservoir
    double reservoir_capacity;
    double outflow;
    double inflow_min;
    double inflow_max;

    if (operation == "q")
    {
        cout << "Exiting program." << endl;
        system ("pause");
        return 0;
    }

    while (operation == "o") // Choose one or multiple simulations.
        {

                string reservoir_name; // Creating variables for reservoir function
                double reservoir_capacity;

                double inflow_min = 0;
                double inflow_max = 0;
                double inflow_range = inflow_min + inflow_max;
                double inflow_difference = inflow_max - inflow_min;
                double inflow_threshold = .9 * inflow_range/2; // Math for acceptable flow threshold.



                cout << "What is the name of the reservoir?" << endl;
                cin.ignore ();
                getline (cin,reservoir_name); // Grab whole string for reservoir name.
                cout << "What is the capacity of the reservoir in MAF (Millions of Acre Feet)?" << endl;
                cin >> reservoir_capacity;
                cout << "What is the minimum inflow?" << endl;
                cin >> inflow_min;
                cout << "What is the maximum inflow?" << endl;
                cin >> inflow_max;
                cout << "What is the required outflow?" << endl;
                cin >> outflow;
                inflow_range = inflow_min + inflow_max;
                inflow_threshold = .9 * inflow_range/2;
                cin.ignore ();

                if (outflow > inflow_threshold) // Check for unacceptable outflow levels.
                {
                    cout << "Warning! The outflow is over 90% of the average inflow. Simulation aborted. Returning to main menu." << endl;
                }
                else
                {
                    const int number_simulations = 10;
                    double fill_level = 0;
                    int years = 1;
                    cout << "Running simulation." << endl;
                    for (int i = 1; i < number_simulations; i++) // Each year
                    {

                        for (years; fill_level < reservoir_capacity; years++ ) 
                        {
                            double r = rand() * 1.0 / RAND_MAX;
                            double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.
                            if (fill_level < 0)
                            {
                            fill_level = 0;
                            }
                        }   // Simulate the change of water level.
                    cout << years << endl;
                    }

                }
                    cout << "What would you like to do now?" << endl; // Saving for later. The menu re-prompt message and code.
                    cout << "Using the letters 'o', or 'q', please indicate if you would like to simulate once, or quit the program: " << endl;
                    cin >> operation;
    }


    system ("pause");
    return 0;
}

所以我想我的主要问题是我遇到了关于在“运行模拟”下面设置 for 循环的问题的内部 for 循环从查询随机值中得出可接受结果范围内的随机数。有人告诉我,这个想法是使用蒙特卡洛方法,即

double r = rand() * 1.0 / RAND_MAX;
double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.

所以程序将为流入创建一个随机值。这个想法是内部 for 循环将继续运行,直到从 0 开始的水库的填充水平达到水库容量。模拟多少年的过程(内部for循环的每次迭代代表一年)要由fill_level模拟for循环的父for循环重复10次。

当我尝试运行您在此处看到的程序时,它会一直运行到“运行模拟”,然后它不会继续进行。是否有比我更有经验的人理解我在说什么并知道发生了什么?

4

1 回答 1

1
for (years; fill_level < reservoir_capacity; years++ ) 
{
    double r = rand() * 1.0 / RAND_MAX;
    double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.
    if (fill_level < 0)
    {
    fill_level = 0;
    }
}   // Simulate the change of water level.

你永远不会fill_level在这个循环中增加。这是一个无限循环。

于 2012-10-05T03:29:39.330 回答