first commit of version ported line by line from python

Not working correctly yet, errors when handling
certain predigits
This commit is contained in:
A.M. Rowsell 2023-06-07 02:08:12 -04:00
commit aac5409a72
6 changed files with 156 additions and 0 deletions

2
.depend Normal file
View File

@ -0,0 +1,2 @@
main.o: main.cpp Spigot.hpp
Spigot.o: Spigot.cpp Spigot.hpp

39
.gitignore vendored Normal file
View File

@ -0,0 +1,39 @@
# Created by https://www.toptal.com/developers/gitignore/api/c++
# Edit at https://www.toptal.com/developers/gitignore?templates=c++
### C++ ###
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# End of https://www.toptal.com/developers/gitignore/api/c++

28
Makefile Normal file
View File

@ -0,0 +1,28 @@
CC=gcc
CXX=g++
RM=rm -f
CPPFLAGS=-ggdb -Og -Wall
LDFLAGS=-g
LDLIBS=-lm
SRCS=main.cpp Spigot.cpp
OBJS=$(subst .cpp,.o,$(SRCS))
all: main
main: $(OBJS)
$(CXX) $(LDFLAGS) -o main $(OBJS) $(LDLIBS)
depend: .depend
.depend: $(SRCS)
$(RM) ./.depend
$(CXX) $(CPPFLAGS) -MM $^>>./.depend
clean:
$(RM) $(OBJS)
distclean: clean
$(RM) *~ .depend
include .depend

54
Spigot.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <iostream>
#include "Spigot.hpp"
Spigot::Spigot(int requestedNumDigits) {
/* create list for number of digits times 10 / 3 */
this->spigotListLength = (requestedNumDigits * 10) / 3;
this->spigotList.resize(this->spigotListLength, 2); // list is initialized with all 2
this->carry = 0;
return;
}
void Spigot::pump(void) {
int tempPreDigit = 0;
int i = this->spigotListLength - 1;
while(i >= 0) {
this->spigotList[i] *= 10;
i--;
}
this->carry = 0;
i = this->spigotListLength - 1;
// note this does *not* handle the i=0 case
while(i > 0) {
this->spigotList[i] += this->carry;
this->carry = (this->spigotList[i] / (i * 2 + 1)) * i;
this->spigotList[i] = this->spigotList[i] % (i * 2 + 1);
i--;
}
// special case of i = 0
this->spigotList[0] += this->carry;
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();
}
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;
}
return;
}

23
Spigot.hpp Normal file
View File

@ -0,0 +1,23 @@
#ifndef Spigot_h
#define Spigot_h
#include <iostream>
#include <vector>
class Spigot {
public:
/* functions */
Spigot(int requestedNumDigits);
void pump(void);
private:
int numDigits;
std::vector<int> spigotList;
int spigotListLength;
int carry;
std::vector<int> preDigits;
};
#endif

10
main.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <iostream>
#include "Spigot.hpp"
int main(void) {
Spigot *piSpigot = new Spigot(1000);
for(int i = 0; i < 1000; i++) {
piSpigot->pump();
}
return 0;
}