1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <iomanip>
#include <iostream>
#include <string>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <string.h>
struct GradeToPoints {
const char* grade;
double points;
};
template< typename T, size_t N >
T* abegin( T (&a)[ N ] )
{ return a; }
template< typename T, size_t N >
T* aend( T (&a)[ N ] )
{ return a + N; }
template< typename T, size_t N >
size_t asize( T (&)[ N ] )
{ return N; }
double CalcGradePoints( const std::string& grade ) {
GradeToPoints grades[] = {
{ "a", 4.00 }, { "am", 3.67 }, { "a-", 3.67 },
{ "b+", 3.33 }, { "bp", 3.33 }, { "b", 3.00 },
{ "b-", 2.67 }, { "bm", 2.67 }, { "cp", 2.33 },
{ "c+", 2.33 }, { "c", 2.00 }, { "c-", 1.67 },
{ "cm", 1.67 }, { "dp", 1.33 }, { "d+", 1.33 },
{ "d", 1.00 }, { "f", 0.00 }, { "i", 0.00 }
};
GradeToPoints* score = std::find_if(
abegin( grades ), aend( grades ), boost::lambda::bind(
strcasecmp, &boost::lambda::_1->*&GradeToPoints::grade,
grade.c_str() ) == 0 );
return score == aend( grades ) ? -1.0 : score->points;
}
double GetGradePoints( const char* class_name ) {
double grade_points;
do {
std::string grade;
std::cout << "Enter grade for " << class_name << ": " << std::flush;
getline( std::cin, grade );
grade_points = CalcGradePoints( grade );
} while( grade_points < 0 );
return grade_points;
}
int main() {
const char* classes[] = {
"Calculus", "Intro to Information Technology", "Physics",
"Communication Skills", "Religious Studies"
};
double grade_points = 0.0;
std::for_each( abegin( classes ), aend( classes ),
boost::lambda::var( grade_points ) += boost::lambda::bind( &GetGradePoints,
boost::lambda::_1 ) );
std::cout << "Final GPA = " << std::fixed << std::setprecision( 2 )
<< ( grade_points / asize( classes ) ) << std::endl;
}
|