From 2bebbd2db910deee9261e769634d9f1140b61acd Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 9 Jul 2020 15:46:35 -0400 Subject: [PATCH] Moved all string functions to a single function A couple of bugs relating to time resolution squished --- Stopwatch.hpp | 50 ++++++-------------------------------------------- 1 file changed, 6 insertions(+), 44 deletions(-) diff --git a/Stopwatch.hpp b/Stopwatch.hpp index eec923f..035d1fb 100644 --- a/Stopwatch.hpp +++ b/Stopwatch.hpp @@ -1,10 +1,10 @@ //MyClasses/Stopwatch.hpp //Matthew Ellison -// Created: 10-30-2018 -//Modified: 02-07-2019 +// Created: 10-30-18 +//Modified: 07-09-20 //This file defines a class that can be used as a simple timer for programs /* - Copyright (C) 2019 Matthew Ellison + Copyright (C) 2020 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -147,44 +147,7 @@ public: //Returns a string with the time at best resolution std::string getStr(){ //Setup the variables - double tempTime = getTime(NANOSECOND); //Holds the time that we are manipulating - std::stringstream timeStr; - - //Decide what time resolution would be best. Looking for the format of XXX.XXX - int timeRes = NANOSECOND; - for(timeRes = MICROSECOND;(timeRes < DEFAULT) && (tempTime >= 1000);++timeRes){ - tempTime = getTime(static_cast(timeRes)); - } - --timeRes; //Take this variable back down to the right place. It has to go one too far to trigger the loop stop - - //Check if the resolution is seconds and if there are more than 120 seconds - if((timeRes == SECOND) && (tempTime >= 120)){ - ++timeRes; - tempTime = getTime(static_cast(timeRes)); - } - //Check if the resolution is minutes and if there are more than 120 minutes - else if((timeRes == MINUTE) && (tempTime >= 120)){ - ++timeRes; - tempTime = getTime(static_cast(timeRes)); - } - - //Put the number in the string - timeStr << std::fixed << std::setprecision(3) << tempTime << ' '; - - //From the timeRes variable decide what word should go on the end of the string - switch(timeRes){ - case HOUR: timeStr << "hours"; break; - case MINUTE: timeStr << "minutes"; break; - case SECOND: timeStr << "seconds"; break; - case MILLISECOND: timeStr << "milliseconds"; break; - case MICROSECOND: timeStr << "microseconds"; break; - case NANOSECOND: timeStr << "nanoseconds"; break; - case DEFAULT: timeStr << "time"; break; - default: throw invalidTimeResolution(); //This should never be hit with this code, but it's good to have all the bases covered - } - - //Return the string - return timeStr.str(); + return getStr(getTime(NANOSECOND)); //Holds the time that we are manipulating } //This function resets all the variables so that it can be run again void reset(){ @@ -200,10 +163,9 @@ public: //Decide what time resolution would be best. Looking for the format of XXX.XXX int timeRes = NANOSECOND; - for(timeRes = MICROSECOND;(timeRes < DEFAULT) && (tempTime >= 1000);++timeRes){ + for(timeRes = NANOSECOND;(timeRes < SECOND) && (tempTime >= 1000);++timeRes){ tempTime /= 1000; } - --timeRes; //Take this variable back down to the right place. It has to go one too far to trigger the loop stop //Check if the resolution is seconds and if there are more than 120 seconds if((timeRes == SECOND) && (tempTime >= 120)){ @@ -211,7 +173,7 @@ public: tempTime /= 60; } //Check if the resolution is minutes and if there are more than 120 minutes - else if((timeRes == MINUTE) && (tempTime >= 120)){ + if((timeRes == MINUTE) && (tempTime >= 120)){ ++timeRes; tempTime /= 60; }