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:
parent
aac5409a72
commit
218d5b7da8
5 changed files with 70 additions and 27 deletions
53
Spigot.cpp
53
Spigot.cpp
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue