diff --git a/src/main.cpp b/src/main.cpp index c0bc19b..92dee8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,12 @@ #include #include -#include "calculator.cpp" #include "commands.cpp" #include "cursor.cpp" #include "debug.cpp" #include "getch.h" #include "terminal.cpp" +#include "utils.cpp" +#include "calculator.cpp" //doesn't compile when sorted alphabetically, why?? /** * @brief Prints entered keys to the console. @@ -76,9 +77,7 @@ void writeResultLine( * @param inputSize the current input size (used to where display missing paranthesis) */ void writeResultLine(Calculator::parseAndCalculateResult result, const unsigned int inputSize) { - std::stringstream result_string; - result_string << result.value; - writeResultLine(result_string.str(), result.unmatchedParanthesis, result.valid, inputSize); + writeResultLine(doubleToString(result.value, 10), result.unmatchedParanthesis, result.valid, inputSize); } int main() { @@ -93,6 +92,8 @@ int main() { } #endif std::setlocale(LC_ALL, ""); + std::cout << "type your equation to calculate,\n"; + std::cout << "press enter to enter a new one\n"; std::cout << "type :q to exit, type :h for help\n"; bool exit = false; //should we exit? @@ -129,7 +130,7 @@ int main() { terminal::key_backspace(keys); break; case 13: // ENTER - std::cout << "\n\033[2K" << Calculator::parseAndCaluclate(keys.keys, true).value << "\n"; + std::cout << "\n\033[2K" << doubleToString(Calculator::parseAndCaluclate(keys.keys, true).value, -1) << "\n"; keys.cursorPos = 0; keys.keys.clear(); break; diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..7b4a001 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,23 @@ +#ifndef __UTILS_CPP +#define __UTILS_CPP + +#include +#include +#include + +/** + * @brief converts double to string + * + * @param from the double to convert + * @param precision what precision the stringed version should be + * @return std::string the converted number + */ +std::string doubleToString(const double& from, const int& precision = 10) { + std::stringstream theStream; + if (precision < 0) theStream << std::setprecision(std::numeric_limits::digits10+1); + else theStream << std::setprecision(precision); + theStream << from; + return theStream.str(); +} + +#endif