diff --git a/.gitignore b/.gitignore index 9fd79bd..a49b595 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +*.txt +main +.gdb_history # Created by https://www.toptal.com/developers/gitignore/api/c++ # Edit at https://www.toptal.com/developers/gitignore?templates=c++ @@ -37,3 +40,26 @@ # End of https://www.toptal.com/developers/gitignore/api/c++ +# Created by https://www.toptal.com/developers/gitignore/api/codeblocks +# Edit at https://www.toptal.com/developers/gitignore?templates=codeblocks + +### CodeBlocks ### +# specific to CodeBlocks IDE +*.layout +*.depend +*.cbp +# generated directories +bin/ +obj/ + +# End of https://www.toptal.com/developers/gitignore/api/codeblocks + +# Created by https://www.toptal.com/developers/gitignore/api/kdevelop4 +# Edit at https://www.toptal.com/developers/gitignore?templates=kdevelop4 + +### KDevelop4 ### +*.kdev4 +.kdev4/ + +# End of https://www.toptal.com/developers/gitignore/api/kdevelop4 + diff --git a/Makefile b/Makefile index e711a0f..897527c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc CXX=g++ RM=rm -f -CPPFLAGS=-ggdb -Og -Wall +CPPFLAGS=-ggdb -O0 -Wall LDFLAGS=-g LDLIBS=-lm diff --git a/Spigot.cpp b/Spigot.cpp index 43a4e1f..8192ba4 100644 --- a/Spigot.cpp +++ b/Spigot.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "Spigot.hpp" Spigot::Spigot(int requestedNumDigits) { @@ -11,13 +13,14 @@ Spigot::Spigot(int requestedNumDigits) { void Spigot::pump(void) { int tempPreDigit = 0; - int i = this->spigotListLength - 1; + int j = this->spigotListLength - 1; + int i = j; while(i >= 0) { this->spigotList[i] *= 10; i--; } this->carry = 0; - i = this->spigotListLength - 1; + i = j; // note this does *not* handle the i=0 case while(i > 0) { this->spigotList[i] += this->carry; @@ -27,28 +30,36 @@ void Spigot::pump(void) { } // special case of i = 0 this->spigotList[0] += this->carry; - tempPreDigit = this->spigotList[0] // 10; + tempPreDigit = this->spigotList[0] / 10; this->spigotList[0] -= (tempPreDigit * 10); - // handle the cases depending on the tempPreDigit - if(tempPreDigit >= 0 && tempPreDigit <= 8) { - // output all predigits - for(long unsigned int j = 0; j < this->preDigits.size(); j++) { - std::cout << this->preDigits.back(); - this->preDigits.pop_back(); + try { + // handle the cases depending on the tempPreDigit + if(tempPreDigit >= 0 && tempPreDigit <= 8) { + // output all predigits + long unsigned pdLen = this->preDigits.size(); + for(long unsigned int j = 0; j < pdLen; j++) { + std::cout << this->preDigits.back() << std::flush; + this->preDigits.pop_back(); + } + assert(this->preDigits.size() == 0); + this->preDigits.insert(this->preDigits.begin(), tempPreDigit); + + } else if(tempPreDigit == 9) { + this->preDigits.insert(this->preDigits.begin(), tempPreDigit); + } else if(tempPreDigit == 10) { + tempPreDigit = 0; + long unsigned pdLen = this->preDigits.size(); + for(long unsigned int j = 0; j < pdLen; j++) { + std::cout << (this->preDigits.back() + 1) % 10 << std::flush; + this->preDigits.pop_back(); + } + this->preDigits.insert(this->preDigits.begin(), tempPreDigit); + } else { + std::cout << "We should never have gotten here -- shit has hit the fan!" << std::endl; } - this->preDigits.insert(this->preDigits.begin(), tempPreDigit); - } else if(tempPreDigit == 9) { - this->preDigits.insert(this->preDigits.begin(), tempPreDigit); - } else if(tempPreDigit == 10) { - tempPreDigit = 0; - for(long unsigned int j = 0; j < this->preDigits.size(); j++) { - std::cout << (this->preDigits.back() + 1) % 10; - this->preDigits.pop_back(); - } - this->preDigits.insert(this->preDigits.begin(), tempPreDigit); - } else { - std::cout << "We should never have gotten here -- shit has hit the fan!" << std::endl; + } catch (int e) { + std::cout << "An exception " << e << " occurred." << std::endl; } return; } diff --git a/Spigot.hpp b/Spigot.hpp index f8d7e56..88908f1 100644 --- a/Spigot.hpp +++ b/Spigot.hpp @@ -11,13 +11,11 @@ public: void pump(void); private: - int numDigits; std::vector spigotList; int spigotListLength; int carry; std::vector preDigits; - }; #endif diff --git a/main.cpp b/main.cpp index 2c79099..e1b6a05 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,17 @@ #include +#include #include "Spigot.hpp" -int main(void) { - Spigot *piSpigot = new Spigot(1000); - for(int i = 0; i < 1000; i++) { +int main(int argc, char **argv) { + int numDigits = 0; + if(argc < 2) { + std::cout << "Usage: spigot " << std::endl; + return -1; + } else { + numDigits = std::atoi((const char *)argv[1]); + } + Spigot *piSpigot = new Spigot(numDigits); + for(int i = 0; i < numDigits; i++) { piSpigot->pump(); } return 0;