短程序在 C 中挂起

发布于 2024-12-07 19:33:30 字数 3597 浏览 2 评论 0原文

每次在 UNIX 中运行此代码时,它都会挂起并显示新行而不提示。它编译没有错误。有人知道这里发生了什么吗?

/*
This program uses a Monte Carlo simulation to maximize widget profits.
 */

#include <stdio.h>
#include <stdlib.h>

// constant values used in the simulation
#define MIN_WIDGETS 1000 // minimum widgets per month
#define MAX_WIDGETS 1500 // maximum widgets per month

#define MIN_COST 0.25 // minimum cost per widget
#define MAX_COST 0.82 // maximum cost per widget

#define MIN_CONVERSION 0.01 // minimum converstion rate
#define MAX_CONVERSION 0.05 // maximum converstion rate

#define MIN_PROFIT 38 // minimum profit per sale
#define MAX_PROFIT 43 // maximum profit per sale

#define FIXED_OVERHEAD 400 // fixed overhead cost

#define NUM_SIMULATIONS 100 // number of simulations performed

void main()
{

  // to keep track of inputs in highest-profit simulation
  int bestW = 0; // widgets per month
  float bestC = 0; // cost per widget
  float bestR = 0; // rate of converstion
  float bestP = 0; // profit per sale
  float bestProfit = 0; // highest profit

  srand(time(0)); // initialize the random number generator
  float h = FIXED_OVERHEAD;

  int i; // loop index 
  for(i = 0; i < NUM_SIMULATIONS; i++)
    {
        // initialize inputs for this individual simulation
        int w = 0; // quantity of widgets bought
        float c = 0; // cost of a widget
        float r = 0; // conversion rate
        float p = 0; // profit per sale
        float profit = 0; // profit

        // simulate quantity of widgets bought per month, between MIN_WIDGETS and MAX_WIDGETS
        w = random() % (MAX_WIDGETS + 1); // to set the maximum value of w at MAX_WIDGETS
        while (w < MIN_WIDGETS) {
            w = random() % (MAX_WIDGETS + 1); // fetch new random number that may fit parameters
        }

        // simulate cost per widget, between MIN_COST and MAX_COST
        c = random() % 100; // to convert random number into an integer between 0 and 99
        while (c < (MIN_COST*100) || c > (MAX_COST*100)) {
            c = random() % 100; // fetch new random number that may fit parameters
        }
        c = c / 100.0; // convert cost back from cents into dollars

        // simulate conversion rate, between MIN_CONVERSION and MAX_CONVERSION
        r = random() % 100; // to convert random number into an integer between 0 and 99
        while (r < (MIN_CONVERSION*100) || r > (MAX_CONVERSION*100)) {
            r = random() % 100; // fetch new random number that may fit parameters
        }
        r = r / 10.0; // convert back into fraction

        // simulate profit per sale, between MIN_PROFIT and MAX_PROFIT
        p = random() % ((MAX_PROFIT + 1)*100); // to convert random number into an integer between 0 and 4300
        while (p < MIN_PROFIT*100) {
            p = random() % (MAX_PROFIT + 1); // fetch new random number that may fit parameters
        }
        p = p / 100.0; // convert back into floating point with two decimal places after period

        profit = (w * r * p) - (h + c * w);

        printf("Current profit is $%.2f, with %d widgets at a %.2f cost per widget with a %.1f conversion rate and %.2f profit/sale.\n",
            profit, w, c, r, p);

        if (profit > bestProfit) {
            bestW = w;
            bestC = c;
            bestR = r;
            bestP = p;
            bestProfit = profit;
        }

    }

  printf("Maximum profit is $%.2f, with %d widgets at a %.2f cost per widget with a %.1f conversion rate and %.2f profit/sale.\n",
    bestProfit, bestW, bestC, bestR, bestP);

}

Every time this code runs in UNIX, it hangs and displays a new line without a prompt. It compiles without error. Anyone know what's up here?

/*
This program uses a Monte Carlo simulation to maximize widget profits.
 */

#include <stdio.h>
#include <stdlib.h>

// constant values used in the simulation
#define MIN_WIDGETS 1000 // minimum widgets per month
#define MAX_WIDGETS 1500 // maximum widgets per month

#define MIN_COST 0.25 // minimum cost per widget
#define MAX_COST 0.82 // maximum cost per widget

#define MIN_CONVERSION 0.01 // minimum converstion rate
#define MAX_CONVERSION 0.05 // maximum converstion rate

#define MIN_PROFIT 38 // minimum profit per sale
#define MAX_PROFIT 43 // maximum profit per sale

#define FIXED_OVERHEAD 400 // fixed overhead cost

#define NUM_SIMULATIONS 100 // number of simulations performed

void main()
{

  // to keep track of inputs in highest-profit simulation
  int bestW = 0; // widgets per month
  float bestC = 0; // cost per widget
  float bestR = 0; // rate of converstion
  float bestP = 0; // profit per sale
  float bestProfit = 0; // highest profit

  srand(time(0)); // initialize the random number generator
  float h = FIXED_OVERHEAD;

  int i; // loop index 
  for(i = 0; i < NUM_SIMULATIONS; i++)
    {
        // initialize inputs for this individual simulation
        int w = 0; // quantity of widgets bought
        float c = 0; // cost of a widget
        float r = 0; // conversion rate
        float p = 0; // profit per sale
        float profit = 0; // profit

        // simulate quantity of widgets bought per month, between MIN_WIDGETS and MAX_WIDGETS
        w = random() % (MAX_WIDGETS + 1); // to set the maximum value of w at MAX_WIDGETS
        while (w < MIN_WIDGETS) {
            w = random() % (MAX_WIDGETS + 1); // fetch new random number that may fit parameters
        }

        // simulate cost per widget, between MIN_COST and MAX_COST
        c = random() % 100; // to convert random number into an integer between 0 and 99
        while (c < (MIN_COST*100) || c > (MAX_COST*100)) {
            c = random() % 100; // fetch new random number that may fit parameters
        }
        c = c / 100.0; // convert cost back from cents into dollars

        // simulate conversion rate, between MIN_CONVERSION and MAX_CONVERSION
        r = random() % 100; // to convert random number into an integer between 0 and 99
        while (r < (MIN_CONVERSION*100) || r > (MAX_CONVERSION*100)) {
            r = random() % 100; // fetch new random number that may fit parameters
        }
        r = r / 10.0; // convert back into fraction

        // simulate profit per sale, between MIN_PROFIT and MAX_PROFIT
        p = random() % ((MAX_PROFIT + 1)*100); // to convert random number into an integer between 0 and 4300
        while (p < MIN_PROFIT*100) {
            p = random() % (MAX_PROFIT + 1); // fetch new random number that may fit parameters
        }
        p = p / 100.0; // convert back into floating point with two decimal places after period

        profit = (w * r * p) - (h + c * w);

        printf("Current profit is $%.2f, with %d widgets at a %.2f cost per widget with a %.1f conversion rate and %.2f profit/sale.\n",
            profit, w, c, r, p);

        if (profit > bestProfit) {
            bestW = w;
            bestC = c;
            bestR = r;
            bestP = p;
            bestProfit = profit;
        }

    }

  printf("Maximum profit is $%.2f, with %d widgets at a %.2f cost per widget with a %.1f conversion rate and %.2f profit/sale.\n",
    bestProfit, bestW, bestC, bestR, bestP);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角迷惘 2024-12-14 19:33:30

我认为错误就在这里:

while (p < MIN_PROFIT*100) {
    p = random() % (MAX_PROFIT + 1); // fetch new random number that may fit parameters
}

如果进入循环体,p 将被设置为 0 到 MAX_PROFIT (= 43) 之间的数字。它永远不会大于或等于 MIN_PROFIT*100 (= 3800),因此它将进入无限循环。

作为旁注,您可能需要考虑在此处使用 do { } while 循环,而不是使用 while 循环并编写循环体两次。编写两次代码是一种极容易出错的方法,因为当您更改其中一个实现时,您始终必须记住更改另一个实现。如果您忘记更新两者,则会引入错误。这似乎就是这里发生的事情。

I think the error is here:

while (p < MIN_PROFIT*100) {
    p = random() % (MAX_PROFIT + 1); // fetch new random number that may fit parameters
}

If the loop body is entered p will be set to a number between 0 and MAX_PROFIT (= 43). It will never be greater than or equal to MIN_PROFIT*100 (= 3800), so it will go into an infinite loop.

As a side-note, you might want to consider using do { } while loops here instead of using a while loop and writing the loop body twice. Writing code twice is an excellent way to make errors, because when you change one of the implementations you always have to remember to change the other. If you forget to update both you will introduce a bug. And that seems to be what happened here.

罪#恶を代价 2024-12-14 19:33:30

很难从那堵代码墙上看出,但它要么花费的时间比您想象的要长,要么您的许多 while 语句之一不正确。

您可以替换 while 语句,并通过获取 0 和各种最小和最大参数之间的差异之间的随机数来加快整个过程,然后将其添加到最小参数,而不是您目前正在做的事情最大参数值的随机数,如果低于最小值则重试。例如,对于您的小部件,您将丢弃 2/3 的结果。

Hard to tell from that wall of code, but it is either taking longer than you think it should or one of your many while statements is incorrect.

You could replace the while statements and speed the whole thing up by getting a random number between 0 and the difference between your various min and max parameters, then adding that to the min parameter, instead of what you are doing at the moment which is getting a random number up to the value of your max parameter, then trying again if is below your min. In the case of your widgets, for example, you are throwing away 2/3 of your results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文