Advertisement
hfxdesign

CIS1202 Midterm

Mar 7th, 2015
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.06 KB | None | 0 0
  1. /*
  2. * Name: Nickelis Jarvis
  3. * Date: 3.7.15
  4. * Program Name: CS Stat Tracker
  5. * Description: Calculate total and average kills and deaths per match.
  6. */
  7.  
  8. #include <iomanip>
  9. #include <iostream>
  10. #include <string>
  11.  
  12. //Our max rounds per match, this could change with future updates.
  13. #define MAXROUNDS 30
  14.  
  15. //Array indexes for k / d
  16. typedef enum SortType {ROUND, KILLS, DEATHS, KDR};
  17.  
  18. //Function to display menu
  19. int showMenu() {
  20.     int selection;
  21.  
  22.     do {
  23.         std::cout
  24.             << "1) Record Match Stats\n"
  25.             << "2) Sort By Kills (Descending)\n"
  26.             << "3) Sort By Deaths (Descending)\n"
  27.             << "4) Sort By K/D (Descending)\n"
  28.             << "5) Look Up Stats by Round\n"
  29.             << "6) Display Match Stats\n"
  30.             << "\n0) Exit Program\n" << std::endl
  31.             << "What would you like to do? ";
  32.  
  33.         //This will return only if the selection is of
  34.         //valid type and in range, otherwise continue looping.
  35.         if (std::cin >> selection)
  36.             if (selection >= 0 && selection < 7)
  37.                 return selection;
  38.  
  39.         std::cout << "\nError: Invalid selection, please enter a number between 0 and 6.\n" << std::endl;
  40.         std::cin.clear();
  41.         std::cin.ignore(10000, '\n');
  42.     } while (true);
  43. }
  44.  
  45. //Function to pull stat from standard i/o, will be obsolete in future versions.
  46. int getStat(SortType StatType, int& rnd_) {
  47.     int rVal;
  48.    
  49.     std::string sVar = { "kills//deaths" };
  50.  
  51.     if (StatType == KILLS)
  52.         sVar = sVar.substr(0, sVar.find("//"));
  53.     else
  54.         sVar = sVar.substr(sVar.find("//") + 2); //Increment index 2 chars to scan over "//"
  55.  
  56.     while (true) {
  57.         std::cout << "Enter number of " << sVar << " for round " << rnd_ + 1 << ": ";
  58.         if (std::cin >> rVal) {
  59.             return rVal;
  60.         }
  61.         else {
  62.             std::cout << "\nError: Please enter a number.\n" << std::endl;
  63.             std::cin.clear();
  64.             std::cin.ignore(10000, '\n');
  65.         }
  66.     }
  67. }
  68.  
  69. //Function to calculate an average value
  70. double calcAvg(double* arrName, size_t& arrSize) {
  71.     double rVal = 0.0;
  72.  
  73.     for (size_t i = 0; i < arrSize; i++) {
  74.         rVal += *(arrName + i) / arrSize;
  75.     }
  76.     return rVal;
  77. }
  78.  
  79. //Function to calculate ratio of kills to deaths
  80. double getKDR(int& kills, int& deaths) {
  81.     return ((double)kills / (double)deaths);
  82. }
  83.  
  84. //Sort arrays by kills, deaths, or kdr, into a pointer array.
  85. void ptrSort(int Src[][3], int* Dest[], const SortType& SortBy) {
  86.     bool swap;
  87.    
  88.     for (size_t i = 0; i < MAXROUNDS; i++)
  89.         Dest[i] = Src[i];
  90.  
  91.     if (SortBy == KDR){
  92.         do {
  93.             swap = false;
  94.             for (size_t i = 0; i < (MAXROUNDS - 1); i++) {
  95.                 if ((getKDR(Dest[i + 1][KILLS], Dest[i + 1][DEATHS])) > (getKDR(Dest[i][KILLS], Dest[i][DEATHS]))) {
  96.                     int* temp = Dest[i];
  97.                     Dest[i] = Dest[i + 1];
  98.                     Dest[i + 1] = temp;
  99.                     swap = true;
  100.                 }
  101.             }
  102.         } while (swap);
  103.     }
  104.     else {
  105.         do {
  106.             swap = false;
  107.             for (size_t i = 0; i < (MAXROUNDS - 1); i++) {
  108.                 if (Dest[i + 1][SortBy] > Dest[i][SortBy]) {
  109.                     int* temp = Dest[i];
  110.                     Dest[i] = Dest[i + 1];
  111.                     Dest[i + 1] = temp;
  112.                     swap = true;
  113.                 }
  114.             }
  115.         } while (swap);
  116.     }
  117.     return;
  118. }
  119.  
  120. //Main function
  121. int main() {
  122.     int kd_arr[MAXROUNDS][3] = { 0 };   //Our actual array, initialized to 0 (empty)
  123.     int* p_kd_arr[MAXROUNDS];       //Our pointer array (for sorting)
  124.  
  125.     //This time i skipped the sentinel and just set 0 to return from main.
  126.     while (true) {
  127.         int choice = showMenu();
  128.         switch (choice) {
  129.         case 0:
  130.             system("pause");
  131.             return 0;
  132.         case 1: //Record Match Stats
  133.             for (int i = 0; i < MAXROUNDS; i++) {
  134.                 kd_arr[i][ROUND] = i + 1;           //Assign round number
  135.                 kd_arr[i][KILLS] = getStat(KILLS, i);       //Assign kills for round
  136.                 kd_arr[i][DEATHS] = getStat(DEATHS, i);     //Assign deaths for round
  137.             }
  138.             break;
  139.         case 2: //Sort by kills (Descending)
  140.  
  141.             //Fall-Through to avoid duplicate code. Only difference is SortBy passed to ptrSort
  142.  
  143.         case 3: //Sort by deaths (Descending)
  144.  
  145.             //Fall-Through to avoid duplicate code.
  146.  
  147.         case 4: //Sort by K/D ratio (Descending)
  148.             if (**kd_arr) {
  149.                 //This handles which menu choice they made and uses the appropriate sort call.
  150.                 if (choice == 2) {
  151.                     ptrSort(kd_arr, p_kd_arr, KILLS);
  152.                     std::cout << "5 rounds with the most kills: \n" << std::endl;
  153.                 }
  154.                 else if (choice == 3) {
  155.                     ptrSort(kd_arr, p_kd_arr, DEATHS);
  156.                     std::cout << "5 rounds with the most deaths: \n" << std::endl;
  157.                 }
  158.                 else {
  159.                     ptrSort(kd_arr, p_kd_arr, KDR);
  160.                     std::cout << "5 rounds with highest Kill / Death: \n" << std::endl;
  161.                 }
  162.  
  163.                 //Print top 5 for whatever sort we used.
  164.                 for (int i = 0; i < 5; i++) {
  165.                     std::cout << std::fixed << std::setprecision(3)
  166.                         << "ROUND " << p_kd_arr[i][ROUND] << ": \n"
  167.                         << std::setw(5) << p_kd_arr[i][KILLS] << " Kills"
  168.                         << std::setw(5) << p_kd_arr[i][DEATHS] << " Deaths"
  169.                         << std::setw(8) << "K/D: " << getKDR(p_kd_arr[i][KILLS], p_kd_arr[i][DEATHS]) << '\n' << std::endl;
  170.                 }
  171.             }
  172.             else
  173.                 std::cout << "\nError: No data to sort\n" << std::endl;
  174.             break;
  175.         case 5: //Look up stats by round
  176.             int round;
  177.  
  178.             if (**kd_arr) {
  179.                 do {
  180.                     std::cout << "Which round would you like to look up?" << std::endl;
  181.  
  182.                     //Check for valid data.
  183.                     if (std::cin >> round) {
  184.                         //If round is over constant MAXROUNDS, throw an error. Otherwise continue.
  185.                         if (round <= MAXROUNDS) {
  186.                             //Loop through to find our round we searched for.
  187.                             for (int i = 0; i < MAXROUNDS; i++) {
  188.                                 //FOUND IT! Go ahead and print the results (no need to use sorting here)
  189.                                 if (round == kd_arr[i][ROUND]) {
  190.                                     std::cout << std::fixed << std::setprecision(3)
  191.                                         << "\nROUND " << kd_arr[i][ROUND] << ": \n"
  192.                                         << std::setw(5) << kd_arr[i][KILLS] << " Kills"
  193.                                         << std::setw(5) << kd_arr[i][DEATHS] << " Deaths"
  194.                                         << std::setw(8) << "K/D: " << getKDR(kd_arr[i][KILLS], kd_arr[i][DEATHS]) << '\n' << std::endl;
  195.                                     break;
  196.                                 }
  197.                                 //Aww, nothing to be found :(
  198.                                 else if (i + 1 >= MAXROUNDS)
  199.                                     std::cout << "Data for round " << round << " could not be found.\n" << std::endl;
  200.                             }
  201.                             //Break the for loop and return to the menu.
  202.                             break;
  203.                         }
  204.                         //Display an error for too high a value.
  205.                         else
  206.                             std::cout << "Sorry, there are only " << MAXROUNDS << " rounds in a match, please try again.\n" << std::endl;
  207.                     }
  208.                     //Display an error for invalid data type.
  209.                     else
  210.                         std::cout << "Error: Invalid data type, please enter a number.\n" << std::endl;
  211.  
  212.                     std::cin.clear();
  213.                     std::cin.ignore(10000, '\n');
  214.                 } while (true);
  215.             }
  216.             //This will display if our first element has no data (empty array)
  217.             else
  218.                 std::cout << "Error: No data to look up.\n" << std::endl;
  219.             break;
  220.         case 6: //Display match stats
  221.             if (**kd_arr) {
  222.                 for (int i = 0; i < MAXROUNDS; i++) {
  223.                     std::cout << std::fixed << std::setprecision(3)
  224.                         << "\nROUND " << kd_arr[i][ROUND] << ": \n"
  225.                         << std::setw(5) << kd_arr[i][KILLS] << " Kills"
  226.                         << std::setw(5) << kd_arr[i][DEATHS] << " Deaths"
  227.                         << std::setw(8) << "K/D: " << getKDR(kd_arr[i][KILLS], kd_arr[i][DEATHS]) << '\n' << std::endl;
  228.                 }
  229.             }
  230.             else
  231.                 std::cout << "Error: No data to display.\n" << std::endl;
  232.             break;
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement