Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <iomanip>
  6. #include <locale>
  7. #include <sstream>
  8.  
  9. #define MAXB 1000
  10.  
  11. using namespace std;
  12.  
  13. string maleNames[1000];
  14. string femaleNames[1000];
  15. ofstream infoOutp("statistics.txt");
  16. ofstream eventOutp("events.txt");
  17.  
  18. void readNames()
  19. {
  20. ifstream male("male.txt");
  21. for(int i = 0; i < 1000; i++)
  22. male >> maleNames[i];
  23. male.close();
  24.  
  25. ifstream female("female.txt");
  26. for(int i = 0; i < 1000; i++)
  27. female >> femaleNames[i];
  28. female.close();
  29. }
  30.  
  31. int getRandomNumber(int min, int max)
  32. {
  33. static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0); // static used for efficiency, so we only calculate this value once
  34. // evenly distribute the random number across our range
  35. return static_cast<int>(rand() * fraction * (max - min + 1) + min);
  36. }
  37.  
  38. class Bunny {
  39. public:
  40. enum sex_type {
  41. SEX_MALE,
  42. SEX_FEMALE
  43. };
  44. enum color {
  45. COLOR_WHITE,
  46. COLOR_BROWN,
  47. COLOR_BLACK,
  48. COLOR_SPOTTED,
  49. ANY_COLOR
  50. };
  51.  
  52. private: //properties
  53. sex_type m_sex;
  54. color m_color;
  55. int m_age = 0;
  56. string m_name = "";
  57. bool m_radioactive;
  58. public:
  59. Bunny(sex_type sex, color col, string name, bool ractive) //Constructors
  60. : m_sex(sex), m_color(col), m_name(name), m_radioactive(ractive)
  61. {
  62. if(ractive == true)
  63. eventOutp << "Radioactive bunny ";
  64. else
  65. eventOutp << "Bunny ";
  66. eventOutp << name << " was born" << endl;
  67. }
  68. Bunny() {
  69. m_name = "";
  70. }
  71.  
  72. static Bunny createRandomBunny(Bunny bunnies[], color motherColor = ANY_COLOR)
  73. {
  74. cout << "CreateRandomBunny" << endl;
  75. sex_type sex = getRandomNumber(0, 1) > 0 ? SEX_MALE : SEX_FEMALE; //sex
  76. string name = sex == SEX_MALE ? maleNames[getRandomNumber(0, 999)] : femaleNames[getRandomNumber(0, 999)]; //name
  77. bool radioactive = getRandomNumber(1, 50) == 1 ? true : false; //radioactiveness
  78.  
  79. color randCol;
  80. if(motherColor == ANY_COLOR)
  81. randCol = static_cast<Bunny::color>(getRandomNumber(0, 3)); //color
  82. else
  83. randCol = motherColor;
  84.  
  85. return Bunny(sex, randCol, name, radioactive);
  86. }
  87.  
  88. void reset(int &alive)
  89. {
  90. alive--;
  91. eventOutp << "Bunny " << this->m_name << " died" << endl;
  92. this->m_name = ""; //set the chosen bunnie's name to blank (since everywhere we check the name to see if a spot in the list is filled)
  93. return;
  94. }
  95.  
  96. static void sortBunnies(Bunny bunnies[])
  97. {
  98. cout << "SortBunnies" << endl;
  99. for(int i = 0; i < MAXB; i++)
  100. for(int j = 0; j< MAXB; j++)
  101. if(bunnies[i].m_age > bunnies[j].m_age && bunnies[i].m_sex > bunnies[j].m_sex)
  102. swap(bunnies[i], bunnies[j]); //Sort bunnies by age (oldest first)
  103. return;
  104. }
  105.  
  106. static void printBunnies(Bunny bunnies[], int &turnNr)
  107. {
  108. cout << "PrintBunnies" << endl;
  109. string strTurn = "\nTurn nr. " + static_cast<ostringstream*>( &(ostringstream() << turnNr) )->str() + " ----------------------------------------------------\n";
  110. cout << strTurn << endl;
  111. infoOutp << strTurn << endl;
  112.  
  113. int totalBunnies = 0;
  114. int totalMales = 0;
  115. int totalFemales = 0;
  116. int totalRadioactive = 0;
  117.  
  118. sortBunnies(bunnies);
  119. for(int i = 0; i < MAXB; i++) {
  120. if(bunnies[i].m_name != "") {
  121. totalBunnies++;
  122. string info = "";
  123.  
  124. if(bunnies[i].m_radioactive == true) {
  125. info = "Radioactive bunny " + bunnies[i].m_name;
  126. totalRadioactive++;
  127. }
  128. else
  129. info = "Bunny " + bunnies[i].m_name;
  130.  
  131. if(bunnies[i].m_sex == SEX_MALE && bunnies[i].m_radioactive == false) {
  132. info += " (male)";
  133. totalMales++;
  134. }
  135. else if (bunnies[i].m_sex == SEX_FEMALE && bunnies[i].m_radioactive == false){
  136. info += " (female)";
  137. totalFemales++;
  138. }
  139.  
  140. info += ",\t " + static_cast<ostringstream*>( &(ostringstream() << bunnies[i].m_age) )->str() + " years old, color: ";
  141.  
  142. if(bunnies[i].m_color == 0)
  143. info += "white";
  144. else if(bunnies[i].m_color == 1)
  145. info += "brown";
  146. else if(bunnies[i].m_color == 2)
  147. info += "black";
  148. else if(bunnies[i].m_color == 3)
  149. info += "spotted";
  150.  
  151. cout << info << endl;
  152. infoOutp << info << endl;
  153. }
  154. }
  155. double malePercent = (static_cast<double>(totalMales) / static_cast<double>(totalBunnies)) *100;
  156. double femalePercent = (static_cast<double>(totalFemales) / static_cast<double>(totalBunnies)) *100;
  157. double radioactivePercent = (static_cast<double>(totalRadioactive) / static_cast<double>(totalBunnies)) *100;
  158.  
  159. string statistics = "\nTotal bunnies: " + static_cast<ostringstream*>( &(ostringstream() << totalBunnies) )->str()
  160. + ", male: " + static_cast<ostringstream*>( &(ostringstream() << totalMales) )->str()
  161. + " (" + static_cast<ostringstream*>( &(ostringstream() << malePercent) )->str()
  162. + "%), female: " + static_cast<ostringstream*>( &(ostringstream() << totalFemales) )->str()
  163. + " (" + static_cast<ostringstream*>( &(ostringstream() << femalePercent) )->str()
  164. + "%), radioactive: " + static_cast<ostringstream*>( &(ostringstream() << totalRadioactive) )->str()
  165. + " (" + static_cast<ostringstream*>( &(ostringstream() << radioactivePercent) )->str() + ")\n";
  166.  
  167. cout << statistics << endl;
  168. infoOutp << statistics << endl;
  169. return;
  170. }
  171.  
  172. static void killHalf(Bunny bunnies[], int &alive)
  173. {
  174. for(int i = 0; i < MAXB/2; i++) {
  175. int deathNr = getRandomNumber(0, 999);
  176. while(bunnies[i].m_name == "")
  177. deathNr = getRandomNumber(0, 999);
  178. bunnies[deathNr].reset(alive);
  179. }
  180. return;
  181. }
  182.  
  183. static void turn(Bunny bunnies[], int &turnNr, int &alive)
  184. {
  185. eventOutp << "\nTurn nr. " << static_cast<ostringstream*>( &(ostringstream() << turnNr) )->str() << " ----------------------------------------------------\n" << endl;
  186.  
  187. bool areMales = false;
  188. for(int y = 0; y < MAXB; y++)
  189. if(bunnies[y].m_age >= 2 && bunnies[y].m_sex == SEX_MALE) { //check if there are age 2 or older males
  190. areMales = true;
  191. break;
  192. }
  193.  
  194. for(int i = 0; i < MAXB; i++) { //FOR EACH BUNNY
  195. if(bunnies[i].m_name != "") { //if bunny exists (m_name not empty)
  196. if(bunnies[i].m_radioactive == false) { //if bunny is not radioactive
  197. if(areMales && bunnies[i].m_sex == SEX_FEMALE) {//if there are age 2 or older males and bunny is a female
  198. int emptyID = 9999;
  199. for(int j = 0; j < MAXB; j++)
  200. if(bunnies[j].m_name == "") { //search for a free spot in the bunny list
  201. emptyID = j;
  202. break;
  203. }
  204. if(emptyID != 9999) {//if there is a free spot (emptyID changed)
  205. bunnies[emptyID] = createRandomBunny(bunnies, bunnies[i].m_color); //create a new bunny with the color of his mother's
  206. alive++;
  207. }
  208. else {//if there aren't any free spots in the bunny list (emptyID unchanged)
  209. killHalf(bunnies, alive); //kill half of the bunnies
  210. alive = 500; //there should be 1000/2 = 500 bunnies left
  211. return; //finish the turn early
  212. }
  213. }
  214.  
  215. if (bunnies[i].m_age < 10) //if bunny is younger than 10 yrs.
  216. (bunnies[i].m_age += 1); //it grows older
  217. else
  218. bunnies[i].reset(alive); //or it dies
  219. }
  220. else {//if bunny is radioactive
  221. int nonRadioactive = 0; //check if there are any non-radioactive bunnies
  222. for(int y = 0; y < MAXB; y++)
  223. if(bunnies[y].m_radioactive == false)
  224. nonRadioactive++;
  225.  
  226. if(nonRadioactive != 0) { //if there are any non-radioactive bunnies
  227. int deathNr = getRandomNumber(0, MAXB); //generate a random bunny id in the list
  228. while(bunnies[deathNr].m_name == "" || bunnies[deathNr].m_radioactive == true)
  229. deathNr = getRandomNumber(0, MAXB); //change the ID until we find a non-empty (name not blank) and non-radioactive buny
  230. bunnies[deathNr].m_radioactive = true; //turn the chosen bunny into radioactive
  231. eventOutp << "Bunny " << bunnies[deathNr].m_name << " was turned radioactive" << endl;
  232. }
  233.  
  234. if(bunnies[i].m_age < 50) //if radioactive bunny is younger than 50 yrs.
  235. bunnies[i].m_age += 1; //it grows older
  236. else
  237. bunnies[i].reset(alive); //or it dies
  238. }
  239. }
  240. }
  241. if(alive > 0) {
  242. printBunnies(bunnies, turnNr);
  243. turnNr++;
  244. }
  245. return;
  246. }
  247. };
  248.  
  249. int main()
  250. {
  251. srand(static_cast<unsigned int>(time(0))); // set initial seed value to system clock
  252. rand();
  253. readNames();
  254.  
  255. Bunny bunnies [MAXB];
  256. int turnNr = 1;
  257.  
  258. for(int i = 0; i < 5; i++) {
  259. bunnies[i] = Bunny::createRandomBunny(bunnies); //create the 5 starting bunnies
  260. }
  261. int alive = 5;
  262. while(alive != 0) //while there is at least one bunny alive
  263. Bunny::turn(bunnies, turnNr, alive);
  264.  
  265. infoOutp.close();
  266. eventOutp.close();
  267. return 0;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement