2015-06-24 105 views
-1

我被困在我的代碼的最後一位。我知道我必須從每個試驗中添加所有配對的百分比,但是我怎樣才能從每個試驗中添加所有配對的百分比?另外我怎樣才能讓它在我的輸出中最後顯示爲「平均配對百分比=」?如何平均配對百分比

#include <iostream> 
#include <iomanip> 
#include "card.h" 
#include "deck.h" 
#include "stdlib.h" 
#include "time.h" 

using namespace std; 


int main(){ 

srand((unsigned)time(NULL)); //randomize function 




for (int y = 1; y <= 10; y++){//this gives me 10 trials 

    int numberOfPairs = 0; 
    int numberOfFlushes = 0; 
    int numberOfHands = 10000; 

    for (int i = 0; i < numberOfHands; i++){// this gives me 10,000 of whatever I put inside this loop 


     Deck gameDeck;//(calls the constructor) this creates the deck 
     gameDeck.shuffle(100);//this shuffles the deck 100 times 

     Card hand[5];//defining five cards 
     hand[0] = gameDeck.getCard(); //listing the cards 1-5 
     hand[1] = gameDeck.getCard(); 
     hand[2] = gameDeck.getCard(); 
     hand[3] = gameDeck.getCard(); 
     hand[4] = gameDeck.getCard(); 

     Card::Suit checkFlush = hand[0].getSuit(); //get the suit of the first card starting at zero 
     bool hasFlush = true; 
     bool hasPair = false; 

     for (int j = 0; j < 5; j++){ //runs through the deck (checking for pairs) 
      if (hand[j].getSuit() != checkFlush){//this is comparing the j series with the suites 
       hasFlush = false; //do not redefine 
      } 
      for (int k = 0; k < 5; k++){//runs through the deck and compares j and k 

       if (j != k && !hasPair){// dont want them to equal because its comparing the same card && search for a pair if you havent found one 
        if (hand[j].getValue() == hand[k].getValue()){//this tells us we have a pair 
         numberOfPairs++;//keeping track of my pairs 
         hasPair = true; 
         break;//breaks after finding a pair 
        } 

       } 

      } 
     } 
     if (hasFlush == true){ 
      numberOfFlushes++;//keeps track of my flushes 
     } 


    } 
    float percentagePair = ((float)numberOfPairs/numberOfHands) * 100.0; //finds percentage with a least a pair for each trial 
    float percentageFlush = ((float)numberOfFlushes/numberOfHands) * 100.0;//finds percentage with a least a flush for each trial 
    float avgPercentagePair = (percentagePair/ 10); // takes the average percent of pairs of all 10 trials 



    std::cout << "Trial number = " << y << endl; 
    std::cout << "Hands dealt = " << numberOfHands << endl; 
    std::cout << "Number of hands with at least a pair = " << numberOfPairs << endl; 
    std::cout << "Number of hands with a flush = " << numberOfFlushes << endl; 
    std::cout << "Percentage of hands with at least a pair = " << percentagePair << "%" << endl; 
    std::cout << "Percentage of hands with a flush = " << percentageFlush << "%" << endl << endl; 

    std::cout << "Average Percentage of pairs = " << avgPercentagePair << "%" << endl; 

回答

0

在每次試用後保持percentagePair的運行總和。在最後一次審判後,將審判總數除以審判次數。

由於您的代碼現在,avgPercentagePair僅爲percentagePair的10​​%。