Guest User

Untitled

a guest
Apr 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <cfloat>
  5. #include <sstream>
  6.  
  7.  
  8. typedef std::map< double, std::string > GradeTranslationMap;
  9.  
  10.  
  11. template < class T >
  12. T
  13. promptForValue
  14. (
  15.     const std::string & promptStr
  16. )
  17. {
  18.     T value = 0;
  19.     std::cout << promptStr << " : ";
  20.     std::cin >> value;
  21.     return value;
  22. };
  23.  
  24.  
  25. GradeTranslationMap
  26. generateRatioToGradeMap
  27. ()
  28. {
  29.     GradeTranslationMap xlator;
  30.     xlator[ 0.6 - DBL_EPSILON ] = "F";
  31.     xlator[ 0.7 - DBL_EPSILON ] = "D";
  32.     xlator[ 0.8 - DBL_EPSILON ] = "C";
  33.     xlator[ 0.9 - DBL_EPSILON ] = "B";
  34.     xlator[ 1.0 ] = "A";
  35.     xlator[ DBL_MAX ] = "A+";
  36.     return xlator;
  37. }
  38.  
  39.  
  40. /*
  41.     \brief  Returns the ratio of the mark.
  42. */
  43. double
  44. grader
  45. (
  46.     double grade,
  47.     double total
  48. )
  49. {
  50.     //Sanity checks
  51.     if ( grade < 0 ) throw std::string( "Negative grades are invalid." );
  52.     if ( total <=0 ) throw std::string( "Negative or null totals are invalid" );   
  53.  
  54.     //Calculate the grade as a percent, and print to cout
  55.     double ratio ( grade / total );
  56.     double percent ( ratio * 100.0 );
  57.     std::cout << percent << "%";
  58.  
  59.     //Determine the alphabetic grade too:
  60.     /*
  61.         Here is some magic: We build a lookup table to convert ratios to grades,
  62.         instead of using a messy if else if death-trap.
  63.  
  64.         Also, making it static const is like declaring a global variable. But without
  65.         poluting the global namespace.
  66.     */
  67.     static const GradeTranslationMap XLATE_RATIO_TO_GRADE( generateRatioToGradeMap() );
  68.  
  69.     /*
  70.         The STL is our friend. Make use of it. look up what std::map lower_bound does
  71.     */
  72.     GradeTranslationMap::const_iterator gradeIt = XLATE_RATIO_TO_GRADE.lower_bound( ratio );
  73.    
  74.     if ( gradeIt == XLATE_RATIO_TO_GRADE.end() ) throw std::string( "No string grade found." );
  75.     std::cout << ", or, also a " <<gradeIt->second << std::endl;
  76.  
  77.     //rerturn the grade ratio
  78.     return ratio;
  79. }
  80.  
  81.  
  82. int
  83. main()
  84. {
  85.     //How many people play?
  86.     int players ( promptForValue<int>( "Number of players" ) );
  87.     double outOf ( promptForValue<double>( "What was the test out of" ) );
  88.     std::cout << std::endl;
  89.     double totalRatio = 0;
  90.     double validRatios = 0;
  91.  
  92.     for ( int p = 1; p <= players; p++ )
  93.     {
  94.         try
  95.         {
  96.             //Fetch the garde info
  97.             std::stringstream promptMessage;
  98.             promptMessage << "Enter player " << p  << "'s grade";
  99.             std::string promptMsg( promptMessage.str() );
  100.            
  101.             //calculate the grade, out of 1.
  102.             double grade ( promptForValue<double>( promptMsg ) );          
  103.    
  104.             //Now issue a grade
  105.             double ratio ( grader( grade, outOf ) );
  106.  
  107.             //Also, keep track of the marks so far.
  108.             totalRatio += ratio;
  109.             validRatios++;
  110.         }
  111.         catch ( const std::string & error )
  112.         {
  113.             std::cout << "Error: " << error << std::endl;
  114.         }
  115.     }
  116.  
  117.     //Display infor about the overall performance too
  118.     if ( validRatios > 0 )
  119.     {
  120.         std::cout << std::endl << "Overall grade: ";
  121.         grader( totalRatio, validRatios );
  122.     }
  123.  
  124.     return 0;
  125. }
Add Comment
Please, Sign In to add comment