From aac5409a7242cec5659174337d656bc22cfae96d Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Wed, 7 Jun 2023 02:08:12 -0400 Subject: [PATCH] first commit of version ported line by line from python Not working correctly yet, errors when handling certain predigits --- .depend | 2 ++ .gitignore | 39 +++++++++++++++++++++++++++++++++++++++ Makefile | 28 ++++++++++++++++++++++++++++ Spigot.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Spigot.hpp | 23 +++++++++++++++++++++++ main.cpp | 10 ++++++++++ 6 files changed, 156 insertions(+) create mode 100644 .depend create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 Spigot.cpp create mode 100644 Spigot.hpp create mode 100644 main.cpp diff --git a/.depend b/.depend new file mode 100644 index 0000000..9ca3c09 --- /dev/null +++ b/.depend @@ -0,0 +1,2 @@ +main.o: main.cpp Spigot.hpp +Spigot.o: Spigot.cpp Spigot.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9fd79bd --- /dev/null +++ b/.gitignore @@ -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++ + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e711a0f --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/Spigot.cpp b/Spigot.cpp new file mode 100644 index 0000000..43a4e1f --- /dev/null +++ b/Spigot.cpp @@ -0,0 +1,54 @@ +#include +#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; +} diff --git a/Spigot.hpp b/Spigot.hpp new file mode 100644 index 0000000..f8d7e56 --- /dev/null +++ b/Spigot.hpp @@ -0,0 +1,23 @@ +#ifndef Spigot_h +#define Spigot_h + +#include +#include + +class Spigot { +public: + /* functions */ + Spigot(int requestedNumDigits); + 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 new file mode 100644 index 0000000..2c79099 --- /dev/null +++ b/main.cpp @@ -0,0 +1,10 @@ +#include +#include "Spigot.hpp" + +int main(void) { + Spigot *piSpigot = new Spigot(1000); + for(int i = 0; i < 1000; i++) { + piSpigot->pump(); + } + return 0; +}