2012-05-03 55 views
0

我對C++相對來說比較陌生,今年我纔開始學習它。 我無法編寫一個小程序來接收文本文檔並將其存儲在數組中。然後採用該數組並以某種方式隨機化數值。這是一個排班計劃,可以將騎手+馬匹安排在合適的位置。使用一組規則重新排列數組並從函數中返回值

我試圖在這個函數(gen())中嘗試的是將條目[num]複製到entry2 [num2]。其中num是條目數量,num2是隨機生成的rand()數字。然後我想讓它檢查新條目是否比舊條目多至少15個位置(在這種情況下,所以馬的騎手可以在他的下一個事件之前休息)

以便您猜測。這個應用程序不工作,即時猜測有一個更簡單的方法? 我怎樣才能發送變量到這個數組,我怎樣才能得到這個函數返回數組回到我的主..即時通過指針猜測?

我附上了下面的整個代碼。

/* ________________________________________________________________________________ 
* | TITLE:  main.cpp              | 
* | AUTHOR: Samuel Abbott ([email protected])       | 
* | 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; 
} 
+0

請將您的以下問題簡化爲核心問題。 –

回答

2

最簡單的方法可能是按照原樣複製陣列,然後使用std::random_shuffle隨機化副本。

2

兩個主要問題馬上關閉。

首先,在gen函數的第二行中創建一個大小爲參數的數組。

string entry2[num];

可以這樣做,在C++中,正常的「自動」 /堆陣列具有以恆定的大小來創建。

相反,你應該看看std :: vector來存儲一個具有可變大小的數組。


其次,你的if if巨大的鏈條,否則if語句似乎是不必要的,更不用說越野車了。

當新的num2錯誤時會發生什麼?

你應該看看使用for循環來解決這個問題的代碼少得多。


第三個問題是您在if,else if語句的巨鏈中使用了錯誤的運算符。

您需要使用==進行比較。當您使用作業(=)時,它似乎可以工作,但它確實總是返回true。

+0

更不用說那些比較中缺少'=='。 – chris

+0

你還應該補充一點,使用rand()和modulo是不好的(它引入了一個偏差):http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx和http://www.azillionmonkeys.com/qed/random.html當然,如果你使用Jerry Coffin的答案,這是可以避免的。 –

1

首先你是/想要返回一個字符串*但你的函數定義是字符串gen(...)。如果將其更改爲string * gen(...),將會很好。其次,如果(entry2 [num2] = entry [num-12])你正在做一個assaignment。但我想你想做一個比較。你用if(entry2 [num2] == entry [num-12])來做到這一點。使用double =進行比較,單個=進行assasignments。

第三,你正在做大量的複製和粘貼,這可能被封裝爲循環。

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

第四,爲什麼你在檢查數值是否相等後創建一個新數字?也許你應該在比較之後保留你的num2而不是錯誤。

第五,如果我看到它的權利,你永遠不會增加計數。這將導致無限循環。

第六,你應該使用新的語句,而不是字符串[num],這將無法在c + +中工作。