#include #include #include #include #include #include inline float DtR(float d) { return d*(3.14159265/180.0); } class Req_exception { public: Req_exception(const char* msg) { std::cerr< coefficients; if(eqfile.is_open()) { int num = 0; while(eqfile.good()) { eqfile>>num; coefficients.push_back(num); } } if(coefficients.size()!=6) throw Req_exception("Error: Loaded file incorrect number of coefficients!"); a1 = coefficients[0]; b1 = coefficients[1]; a2 = coefficients[2]; b2 = coefficients[3]; a3 = coefficients[4]; b3 = coefficients[5]; } public: inline float fnClubAngle(int clubnumber) { return (a1+b1*0.85*clubnumber); } inline float fnClubLength(int clubnumber) { return (a2+b2*1.05*clubnumber); } inline float fnClubSpeed(int swingnumber, int clubnumber) { return (1.1*(a3+b3*swingnumber)*(fnClubLength(clubnumber)/40)*2); } float fnDistance(int swingnumber, int clubnumber) { float speed_in_yards_per_s = fnClubSpeed(swingnumber, clubnumber); float club_angle_in_radians = DtR(fnClubAngle(clubnumber)); return ((speed_in_yards_per_s/3)*2)*(::sin(2*club_angle_in_radians)/32.2); } Requation(const char* filename = "golf.txt") { eqfile.open(filename); if(!eqfile.is_open()) throw Req_exception("Error: Couldn't open file for reading!"); ReadandParseFile(); } ~Requation() { eqfile.close(); } }; typedef struct { float distance, depth; short clubnumber; short swingtype; } InputData; float abs( float val ) { if(val<0) return val*-1; return val; } bool CheckHit(float d1, float d2) { const float error = 0.2; if(::abs(d1-d2) <= error) return true; return false; } void PrintTable(Requation* eq, const InputData& input) { const char* szHeader = "%6s %8s %12s %8s %4s\n"; const char* szRowFmt = "%6d %8d %12.2f %8.2f %4s\n"; float distance = eq->fnDistance(input.swingtype, input.clubnumber); ::printf(szHeader, "Club #", "Swing #", "Distance(y)", "Error(y)", "Hit?"); printf("----------------------------------------------\n"); ::printf(szRowFmt, input.clubnumber, input.swingtype, distance, input.depth - distance, CheckHit(distance, input.depth) ? "Yes":"No"); } int main(int argc, char* argv[]) { Requation eq; InputData tempinput; bool quit = false; while(1) { memset(&tempinput, 0, sizeof(InputData)); std::cout<<"Distance (in yards): "; std::cin>>tempinput.distance; std::cout<<"Depth (in yards): "; std::cin>>tempinput.depth; do { std::cout<<"Club number (2-10): "; std::cin>>tempinput.clubnumber; if(tempinput.clubnumber == 99) { quit = true; break; } else if(tempinput.clubnumber < 2 || tempinput.clubnumber > 10) std::cout<<"Please enter a number between 2 and 10.\n"; else break; } while(1); if(quit) break; do { std::cout<<"Swing type (1-4): "; std::cin>>tempinput.swingtype; if(tempinput.swingtype < 1 || tempinput.swingtype > 4) std::cout<<"Please enter a number between 1 and 4.\n"; else break; } while(1); PrintTable(&eq, tempinput); } return 0; }