0

我对 C++ 比较陌生,今年才开始学习。我无法编写一个小程序来接收文本文档并将其存储在数组中。然后取该数组并以某种方式随机化这些值。这是一个排班计划,将骑手和马匹安排到合适的位置。

我在这个函数(gen())中尝试的是将 entry[num] 复制到 entry2[num2] 。其中 num 是条目的数量,num2 是随机生成的 rand() 数。然后我希望它检查新条目是否比旧条目至少多 15 个位置(在这种情况下.. 所以骑马的人可以在他的下一个事件之前休息一下)

所以你可以猜到..这个应用程序不起作用,我猜有一个更简单的方法?我怎样才能将变量发送到这个数组中,我怎样才能让这个函数将数组返回到我的 main .. 我通过指针猜测?

我在下面附上了整个代码。

/*   ________________________________________________________________________________
*    |   TITLE:     main.cpp                                                        |
*    |   AUTHOR:    Samuel Abbott (samuel.d.abbott@gmx.com)                         |
*    |   PURPOSE:   to take camp draft data in, randomise it with certain values,   |
*    |              and output the new roster as a text document                    |
*    |   DATE:      may 1, 2012                                                     |
*    |   LAST EDIT: may 3,2012                                                      |
*    |______________________________________________________________________________|
*/

#include <iostream>
#include <string>
#include <stdlib.h> //random number
#include <time.h> //random number
#include <fstream> //used for input / output of external files

using namespace std;
/*
*   TITLE: gen
*   PURPOSE: to randomise the entries , check to see if they are more then 15 units  apart, and return a pointer to the randomised array
*   string entry[] holds original values
*   int num [] holds number of entries
*/
sting gen(string entry[], int num)
{
   int count = 0;
   string entry2[num]; //randomised array

   /* initialize random seed: */
   srand(time(NULL));

   for (count =0; count < num)
   {
      num2 = rand() % num; //generate random number
      entry[num] = entry2[num2]; //copy number from array to a random position in the next array

      /* a series of if checks to make sure the entry is no less then 15 positions from the previous entry */
      if (entry2[num2]=entry[num-15])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-14])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-13])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-12])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-11])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-10])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-9])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-8])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-7])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-6])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-5])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-4])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-3])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-2])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else if (entry2[num2]=entry[num-1])
      {
         num2 = rand() % num; //generate random number
         entry[num] = entry2[num2]; //copy number from array to a random position in the next array
      }

      else
      {
         entry[num] = entry2[num2];
      }
   }

   string pointer* = entry2[]
   return pointer
}

/*
*   Title : Loading
*   Purpose: This function generates a loading animation. in other words. just a fancy     graphic.
*   HAS NO LOADING PURPOSE
*/

void loading()
{
   /* Declare loading variable, this variable is just the screen printout */
   string loading = "...";
   int time = 0;
   int loadtime = 10;

   /* print out loading animation */
   while (time != loadtime)
   {
      cout << loading;
      time++;
   }

}
void loading();

/*
*   Title: Main
*   Purpose: this is the main function for the randomisation program
*/
int main()
{
//declare all variables
   string fname = "";
   int count;

   string line;
   ifstream inputfile;
   char c;
   int num=0;


   /* introduction */
   cout << "Roster Randomisation Generator" << endl;
   /* Get input for roster name */
   cout << "Enter a name for the roster, such as 13022012coolah:"<<endl;
   cin>> fname;
   /* Begin generating roster */
   cout << "Roster Generation in progress." << endl;
   loading();
   /* get amount of lines in text file .. used to determine how many entries there are to     avoid printing blank spaces*/

   inputfile.open("output.txt");

   while (inputfile.good())
   {
      c = inputfile.get();

      if (c=='\n')
      {
         num++;
      }
   }

   inputfile.close();
   cout<<"Number of Entries counted is: "<<num<<endl;

   string entry[num];

   cout << "Grabbing entries from entry file..."<<endl;
   /* open text file*/
   inputfile.open("output.txt");

   /* read all data into the array entry[] */
   for (count = 0; count < num; count++)
   {
      getline(inputfile,line);  //Gets a single line from example.txt
      entry[count]=line;

      // inputfile >> entry[count];
   }

   inputfile.close();
   cout <<"Found the following entries: " <<endl;

   /* output captured entries*/
   for (count =0; count < num; count++)
   {
      cout <<entry[count] <<endl;
   }


   return 0;
}
4

3 回答 3

2

两个主要问题立即出现。

首先,在您的 gen 函数的第二行中,您创建一个具有参数大小的数组。

string entry2[num];

您不能在 C++ 中这样做,必须以恒定大小创建正常的“自动”/堆栈数组。

相反,您可能应该查看 std::vector 以存储可以具有可变大小的数组。


其次,你的 if、else if 语句的巨大链似乎没有必要,更不用说有问题了。

当新num2的也出错时会发生什么?

您应该考虑使用 for 循环来用更少的代码解决这个问题。


第三个问题是您在 if、else if 语句的巨大链中使用了错误的运算符。

您需要== 用于比较。当你使用 assignment( =) 时,它可能看起来有效,但它实际上总是返回 true。

于 2012-05-03T05:18:21.487 回答
2

最简单的方法可能是按原样复制数组,然后使用std::random_shuffle随机复制。

于 2012-05-03T05:14:01.137 回答
1

首先,您是/想要返回一个字符串*,但您的函数定义是字符串 gen(...)。如果你把它改成 string* gen(...) 就好了。

其次,你正在做一个作业 if(entry2[num2] = entry[num-12])。但我想你想做一个比较。你用 if(entry2[num2] == entry[num-12]) 来做这个。使用双 = 进行比较,使用单 = 进行分配。

第三,你做了很多复制和粘贴,这可以封装为循环。

for(int x = 15; x > 0; x--) 
{
    if(entry2[num2] == entry[num - x] 
    {
        num2 = rand() % num;
        entry[num] = entry[num - x];
        break; //maybe ?!
    }
}

第四,为什么在检查值是否相等之后还要创建一个新数字?也许你应该保留你的 num2 而不是在比较之后出错。

第五,如果我没看错,你永远不会增加计数。这将导致无限循环。

第六,您应该使用 new 语句而不是 string[num] 这在 C++ 中不起作用。

于 2012-05-03T05:35:12.743 回答