0

我的教授强迫我使用一个名为 RAPTOR 的程序,它可以创建和执行流程图和伪代码。而且我一直在转换我的流程图,而不是用实际语言编写。我终于编译了我的程序,但现在它不会给我与 RAPTOR 程序相同的结果,并且我的 black1 变量一直返回 0,而 black2 一直在吐出一个像 1,000 这样的数字。无论如何,任何帮助将不胜感激,谢谢!

这是代码:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>

using namespace std;
int main()
{
   int trials;
   int first;
   string bag;
   int j;
   float percent;
   string temp;
   int black2;
   int runs;
   int l;
   int black1;
   int firstdraw;
   int c;
   string possible;
   int seconddraw;
   srand(time(NULL));
   l = bag.length();
   int r = rand() % 4;

   possible ="bw";
   runs =0;
   while (!(runs>9))
   {
      black1 =0;
      black2 =0;
      runs =runs+1;
  trials =0;
  while (!(trials==1000))
  {
     trials =trials+1;
     bag ="bbwww";
     l = bag.length();
     c =1;
     temp =" ";
     while (!(c==5))
     {
        j = r ;
        temp[1] = bag[c];
        bag[c] = bag[j];
        bag[j] = temp[1];
        c =c+1;
     }
     c =1;
     j = r;
     firstdraw =bag[j];
     if (firstdraw==possible[1])
     {
        black1 = black1+1;
        first = 1;
     }
     else
     {
        first = 0;
     }
     while (!(j>l-1))
     {
        bag[j] = bag[j + 1];
        j =j+1;
     }
     l =l-1;
     j = r;
     seconddraw = bag[j];
     if (seconddraw==possible[1] && first==1)
     {
        black2 =black2+1;
     }
     else
     {
     }
  }
  percent = 100.0 * black2/black1;
  cout << black2 << " " << black1 << endl;
  cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << endl;   }

    return 0;
}
4

1 回答 1

3

很抱歉,我不会修复你的整个代码,我让它更具可读性,删除了那些丑陋using namespace std;的东西并给你留下了一些评论。检查一下,修复您的代码,并在需要时查看一些教程。我用这段代码一遍又一遍地得到一个调试断言。猜你从来没有运行过任何调试器,只是打开了发布模式。

也不要忘记初始化变量。你冒着不确定的行为的风险。

这两根弦走进一个酒吧坐下。酒保说:“那会是什么?” 第一个字符串说:“我想我要一杯啤酒 quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu”“请原谅我的朋友,”第二个字符串说,“他不是空终止的。”

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <time.h>
#include <stdlib.h>

int main()
{
    int trials = 0;
    int first = 0;
    int c = 0;
    int j = 0;
    int l = 0;
    int black2 = 0;
    int runs = 0;
    int black1 = 0;
    int firstdraw = 0;
    int seconddraw = 0;
    float percent = 0.0f;

    std::string temp = "\0";
    std::string bag = "\0";
    std::string possible = "\0";

    srand (time(NULL));
    l = bag.length();

    fflush(stdin); // You may want to do this to get random numbers from rand() otherwise it may use the same number over and over again
    int r = 0;
    r = rand() % 4;

    possible = "bw";
    runs = 0;
    while (!(runs > 9))
    {
        black1 = 0;
        black2 = 0;
        runs = runs + 1;  //Why no for loop ?
        trials = 0;

        while (!(trials == 1000))
        {
            trials = trials + 1; //Why no for loop ?
            bag ="bbwww";
            l = bag.length();
            c = 1;
            temp =" ";
            while (!(c == 5))
            {
                j = r;
                temp[1] = bag[c]; // temp[1] <- debug assertion, can't work
                bag[c] = bag[j];
                bag[j] = temp[1]; // temp[1] <- debug assertion, can't work either
                c = c + 1;
            }
            c = 1;
            j = r;
            firstdraw = bag[j];

            if (firstdraw == possible[1])
            {
                black1 = black1 + 1;
                first = 1;
            }
            else
            {
                first = 0;
            }
            while (!(j > l - 1))
            {
                bag[j] = bag[j + 1]; //Will most likely cause a debug assertion too...
                j = j + 1;
            }
            l = l - 1;
            j = r;
            seconddraw = bag[j];

            if (seconddraw == possible[1] && first==1)
            {
                black2 = black2 + 1;
            }
            else
            {
                //Not needed !?!
            }
        }
        percent = 100.0 * black2 / black1;
        std::cout << black2 << " " << black1 << std::endl;
        std::cout << "Percentage for run #" << runs << " and " << trials << " trials: %" << percent << std::endl;
    }

     return 0;
}
于 2013-10-25T06:37:35.923 回答