我的变量black1和black2在c ++中给出了错误的答案?

时间:2013-10-25 05:34:41

标签: c++

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

以下是代码:

#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;
}

1 个答案:

答案 0 :(得分:3)

我很抱歉,我不会修复你的整个代码,我让它更具可读性,删除了丑陋的using namespace std;并为你留下了一些评论。检查一下,修复代码,并根据需要查看一些教程。我用这段代码一遍又一遍地得到一个调试断言。猜测你从未运行任何调试器并且只启用了Release模式。

也不要忘记初始化变量。您存在未定义的行为风险。

  

这两根弦走进一个酒吧坐下。酒保说:“那又怎样呢?”   第一个字符串说,“我想我会有一个啤酒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;
}