This is a speed distance and time calculator - the comments should explain most of the odd looking code if there is any. I am looking for suggestions to improve the efficiency of the program and how better to structure it - is my approach the best from a 'best practices', efficiency and OOP point of view.
So, specifically:
- How might I improve to fit in with common best practices?
- I this the most efficient method and structure?
- Could I add or improve functions so I could more easily add features like unit conversions?
I don't want to lose any functionality. The current program calculates speed distance and time as well as showing all working which adds quite a lot of strings to store information. I am still looking to improve these parts as well.
The main reason I am asking, is to avoid bad practices and find better ways of doing what I'm doing. I am a C++ beginner but I know quite a lot.
#include <iostream> #include <sstream> using std::cout; using std::cin; using std::string; using std::endl; using std::ostringstream; class Math { // Math Class public: ostringstream s; // SPEED DISTANCE AND TIME CALCULATION double calcSpeed(double distance, double time, string& formula) // SPEED ~ Variable formula passed to the ufnction will be redefined { s << distance << " / " << time; // Adding numbers to string formula = s.str(); // Converting s to formula of type string return distance / time; // Returns result of function } double calcDistance(double speed, double time, string& formula) // DISTANCE { s << speed << " * " << time; formula = s.str(); return speed * time; } double calcTime(double speed, double distance, string& formula) // TIME { s << distance << " / " << speed; formula = s.str(); return distance / speed; } }; int main(int argc, const char * argv[]) { Math math; // Creates object of Math class to be used to access members double firstvalue, secondvalue, result; // Numbers used for conditions and working int chosenCalc = 1; string chosenCalcStr = "speed"; // Strings used for working string firstParam, secondParam; string formula; string intform; bool quit = false; typedef double (Math::*FuncChosen)(double first, double second, string& third); // Typedef gives access to a function which can change if it has same parameter and return types FuncChosen p = &Math::calcSpeed; // p's default value /* Welcome Message */ printf("If you have both other variables in the triangle you can calculate any element. Speed, distance or time.\n"); printf("The answer given will be in the same units that you entered - if you entered m/s for speed, time will be in seconds etc.\n"); /* While user enters something other than 4*/ while (chosenCalc != 4 && !quit) // While user does not want to quit { printf("Enter 1 if you want to calculate speed, 2 for distance, 3 for time and 4 to exit.\n> "); cin >> chosenCalc; if (chosenCalc == 4) { quit = true; return 0; } /* Conditions */ switch (chosenCalc) { case 1: p = &Math::calcSpeed; // Redefines p to explicit function at runtime according to conditions firstParam = "distance"; // Explanation strings secondParam = "time"; formula = "Speed = distance / time"; chosenCalcStr = "Speed"; // Type in formula calculated break; case 2: p = &Math::calcDistance; firstParam = "speed"; secondParam = "time"; formula = "Distance = Speed * time"; chosenCalcStr = "Distance"; break; case 3: p = &Math::calcTime; firstParam = "speed"; secondParam = "distance"; formula = "Time = distance / speed"; chosenCalcStr = "Time"; break; } /* User Input */ cout << "\nPlease enter the value for " << firstParam << ".\n> "; cin >> firstvalue; cout << "Please enter the value for " << secondParam << ".\n> "; cin >> secondvalue; cout << "\n"; result = (math.*p)(firstvalue, secondvalue, intform); // Double result = return value of chosen function **NON EXPLICIT (changes at runtime)** math is used as object to access public members if (chosenCalc != 1) { cout << "Speed = distance / time\n";} // Print formula - We don't want to print out sum twice unless we are not calculating speed and wnat general formula cout << formula << "\n"; // Print adjusted formula cout << chosenCalcStr << " = " << intform << "\n"; // Print numbers in formula cout << chosenCalcStr << " = " << result << "\n\n"; // Print result /* END */ } }