使用一组规则洗牌数组并从函数返回值 我对C++比较陌生,我今年才开始学习它。 我很难编写一个小程序来接收文本文档并将其存储在数组中。然后取那个数组,以某种方式将值随机化。这是一个排班计划,以安排骑手+马到合适的位置
我试图在这个函数(gen())中尝试将条目[num]复制到entry2[num2]。其中num是存在的条目数,num2是随机生成的rand()数。然后我想让它检查新条目是否至少比旧条目多15个位置(在这种情况下..因此骑手可以在下一个事件之前休息) 所以你可以猜到。。这个应用程序不起作用,我猜有更简单的方法吗? 如何将变量发送到此数组中,以及如何使此函数将数组返回到我的主。。我是通过指针猜的 我在下面附上了全部代码使用一组规则洗牌数组并从函数返回值 我对C++比较陌生,我今年才开始学习它。 我很难编写一个小程序来接收文本文档并将其存储在数组中。然后取那个数组,以某种方式将值随机化。这是一个排班计划,以安排骑手+马到合适的位置,c++,arrays,C++,Arrays,我试图在这个函数(gen())中尝试将条目[num]复制到entry2[num2]。其中num是存在的条目数,num2是随机生成的rand()数。然后我想让它检查新条目是否至少比旧条目多15个位置(在这种情况下..因此骑手可以在下一个事件之前休息) 所以你可以猜到。。这个应用程序不起作用,我猜有更简单的方法吗? 如何将变量发送到此数组中,以及如何使此函数将数组返回到我的主。。我是通过指针猜的 我在下面附上了全部代码 /* ___________________________________
/* ________________________________________________________________________________
* | 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;
}
/*________________________________________________________________________________
*|标题:main.cpp|
*作者:塞缪尔·阿伯特(Samuel.d。abbott@gmx.com) |
*|目的:为了获取营地草案数据,使用特定值将其随机化|
*|并将新名册作为文本文件输出|
*|日期:2012年5月1日|
*|最后一次编辑:2012年5月3日|
* |______________________________________________________________________________|
*/
#包括
#包括
#包含//随机数
#包含//随机数
#include//用于外部文件的输入/输出
使用名称空间std;
/*
*标题:gen
*目的:要将条目随机化,请检查它们之间的间隔是否超过15个单位,并返回指向随机化数组的指针
*字符串项[]保存原始值
*int num[]保存条目数
*/
sting gen(字符串条目[],int num)
{
整数计数=0;
字符串entry2[num];//随机数组
/*初始化随机种子:*/
srand(时间(空));
用于(计数=0;计数for(int x = 15; x > 0; x--)
{
if(entry2[num2] == entry[num - x]
{
num2 = rand() % num;
entry[num] = entry[num - x];
break; //maybe ?!
}
}