Squashed bug causing incorrect digits. Now fully working

I totally brainfarted and put this->preDigits.size() in the for
loop comparison clause, which gets re-evaluated every loop.
So it was leaving digits on the stack that should have been
printed, causing errors both immediately and down the line.
This commit is contained in:
A.M. Rowsell 2023-06-09 12:15:53 -04:00
parent aac5409a72
commit 218d5b7da8
Signed by: amrowsell
GPG Key ID: 0B6E2D8375CF79A9
5 changed files with 70 additions and 27 deletions

26
.gitignore vendored
View File

@ -1,3 +1,6 @@
*.txt
main
.gdb_history
# Created by https://www.toptal.com/developers/gitignore/api/c++ # Created by https://www.toptal.com/developers/gitignore/api/c++
# Edit at https://www.toptal.com/developers/gitignore?templates=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++ # 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

View File

@ -1,7 +1,7 @@
CC=gcc CC=gcc
CXX=g++ CXX=g++
RM=rm -f RM=rm -f
CPPFLAGS=-ggdb -Og -Wall CPPFLAGS=-ggdb -O0 -Wall
LDFLAGS=-g LDFLAGS=-g
LDLIBS=-lm LDLIBS=-lm

View File

@ -1,4 +1,6 @@
#include <iostream> #include <iostream>
#include <cmath>
#include <cassert>
#include "Spigot.hpp" #include "Spigot.hpp"
Spigot::Spigot(int requestedNumDigits) { Spigot::Spigot(int requestedNumDigits) {
@ -11,13 +13,14 @@ Spigot::Spigot(int requestedNumDigits) {
void Spigot::pump(void) { void Spigot::pump(void) {
int tempPreDigit = 0; int tempPreDigit = 0;
int i = this->spigotListLength - 1; int j = this->spigotListLength - 1;
int i = j;
while(i >= 0) { while(i >= 0) {
this->spigotList[i] *= 10; this->spigotList[i] *= 10;
i--; i--;
} }
this->carry = 0; this->carry = 0;
i = this->spigotListLength - 1; i = j;
// note this does *not* handle the i=0 case // note this does *not* handle the i=0 case
while(i > 0) { while(i > 0) {
this->spigotList[i] += this->carry; this->spigotList[i] += this->carry;
@ -27,28 +30,36 @@ void Spigot::pump(void) {
} }
// special case of i = 0 // special case of i = 0
this->spigotList[0] += this->carry; this->spigotList[0] += this->carry;
tempPreDigit = this->spigotList[0] // 10; tempPreDigit = this->spigotList[0] / 10;
this->spigotList[0] -= (tempPreDigit * 10); this->spigotList[0] -= (tempPreDigit * 10);
try {
// handle the cases depending on the tempPreDigit // handle the cases depending on the tempPreDigit
if(tempPreDigit >= 0 && tempPreDigit <= 8) { if(tempPreDigit >= 0 && tempPreDigit <= 8) {
// output all predigits // output all predigits
for(long unsigned int j = 0; j < this->preDigits.size(); j++) { long unsigned pdLen = this->preDigits.size();
std::cout << this->preDigits.back(); for(long unsigned int j = 0; j < pdLen; j++) {
std::cout << this->preDigits.back() << std::flush;
this->preDigits.pop_back(); this->preDigits.pop_back();
} }
assert(this->preDigits.size() == 0);
this->preDigits.insert(this->preDigits.begin(), tempPreDigit); this->preDigits.insert(this->preDigits.begin(), tempPreDigit);
} else if(tempPreDigit == 9) { } else if(tempPreDigit == 9) {
this->preDigits.insert(this->preDigits.begin(), tempPreDigit); this->preDigits.insert(this->preDigits.begin(), tempPreDigit);
} else if(tempPreDigit == 10) { } else if(tempPreDigit == 10) {
tempPreDigit = 0; tempPreDigit = 0;
for(long unsigned int j = 0; j < this->preDigits.size(); j++) { long unsigned pdLen = this->preDigits.size();
std::cout << (this->preDigits.back() + 1) % 10; for(long unsigned int j = 0; j < pdLen; j++) {
std::cout << (this->preDigits.back() + 1) % 10 << std::flush;
this->preDigits.pop_back(); this->preDigits.pop_back();
} }
this->preDigits.insert(this->preDigits.begin(), tempPreDigit); this->preDigits.insert(this->preDigits.begin(), tempPreDigit);
} else { } else {
std::cout << "We should never have gotten here -- shit has hit the fan!" << std::endl; 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; return;
} }

View File

@ -11,13 +11,11 @@ public:
void pump(void); void pump(void);
private: private:
int numDigits;
std::vector<int> spigotList; std::vector<int> spigotList;
int spigotListLength; int spigotListLength;
int carry; int carry;
std::vector<int> preDigits; std::vector<int> preDigits;
}; };
#endif #endif

View File

@ -1,9 +1,17 @@
#include <iostream> #include <iostream>
#include <cstdlib>
#include "Spigot.hpp" #include "Spigot.hpp"
int main(void) { int main(int argc, char **argv) {
Spigot *piSpigot = new Spigot(1000); int numDigits = 0;
for(int i = 0; i < 1000; i++) { if(argc < 2) {
std::cout << "Usage: spigot <numDigits>" << 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(); piSpigot->pump();
} }
return 0; return 0;