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
No known key found for this signature in database
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++
# 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

View File

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

View File

@ -1,4 +1,6 @@
#include <iostream>
#include <cmath>
#include <cassert>
#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;
}

View File

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

View File

@ -1,9 +1,17 @@
#include <iostream>
#include <cstdlib>
#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 <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();
}
return 0;