Cleanup, added a command-line version, eg 'gmpcmd 123' will output 123^321

This commit is contained in:
Elf M. Sternberg 2015-08-09 10:51:44 -07:00
parent eb05624610
commit c51556a7ad
4 changed files with 40 additions and 4 deletions

View File

@ -1,17 +1,20 @@
PROJECT= resume PROJECT= resume
SOURCES= src/cheapgmp.cpp src/accessories.cpp SOURCES= src/cheapgmp.cpp src/accessories.cpp
MAINSRC= src/main.cpp MAINSRC= src/main.cpp
CMDSRC= src/cmd.cpp
TESTSOURCES= tests/test_cheapgmp.cpp TESTSOURCES= tests/test_cheapgmp.cpp
LDFLAGS= LDFLAGS=
TESTLDFLAGS= -laeryn_tests -laeryn_core TESTLDFLAGS= -laeryn_tests -laeryn_core
CFLAGS= -c -Wall -std=c++11 CFLAGS= -c -Wall -std=c++11
CPLUSPLUS= g++ CPLUSPLUS= g++
BINARY=cheapgmp BINARY=cheapgmp
CMD=gmpcmd
TESTBINARY=testrunner TESTBINARY=testrunner
OBJECTS=$(SOURCES:.cpp=.o) OBJECTS=$(SOURCES:.cpp=.o)
TESTOBJECTS=$(TESTSOURCES:.cpp=.o) TESTOBJECTS=$(TESTSOURCES:.cpp=.o)
MAINOBJECTS=$(MAINSRC:.cpp=.o) MAINOBJECTS=$(MAINSRC:.cpp=.o)
CMDOBJECTS=$(CMDSRC:.cpp=.o)
all: $(SOURCES) $(BINARY) all: $(SOURCES) $(BINARY)
@ -20,6 +23,9 @@ $(BINARY): $(OBJECTS) $(MAINOBJECTS)
app: $(BINARY) app: $(BINARY)
gmpcmd: $(CMDOBJECTS) $(OBJECTS)
$(CPLUSPLUS) $(OBJECTS) $(CMDOBJECTS) $(LDFLAGS) -o $@
testrunner: $(OBJECTS) $(TESTOBJECTS) testrunner: $(OBJECTS) $(TESTOBJECTS)
$(CPLUSPLUS) $(OBJECTS) $(TESTOBJECTS) $(LDFLAGS) $(TESTLDFLAGS) -o $@ $(CPLUSPLUS) $(OBJECTS) $(TESTOBJECTS) $(LDFLAGS) $(TESTLDFLAGS) -o $@
@ -30,7 +36,7 @@ test: $(BINARY)
./$(BINARY) ./$(BINARY)
distclean: clean distclean: clean
rm -f $(BINARY) rm -f $(BINARY) $(TESTBINARY) $(CMD)
clean: clean:
rm -f $(OBJECTS) rm -f $(OBJECTS) $(TESTOBJECTS) $(MAINOBJECTS) $(CMDOBJECTS)

View File

@ -11,7 +11,6 @@ namespace cheapgmp {
as a carryover to the 10^(n+1) slot */ as a carryover to the 10^(n+1) slot */
void multiply(gmrep &multiplicand, ulong multiplier) { void multiply(gmrep &multiplicand, ulong multiplier) {
gmrep res(new lmrep());
ulong rem = 0; ulong rem = 0;
for_each(multiplicand->rbegin(), multiplicand->rend(), [&rem, multiplier](ulong &i) { for_each(multiplicand->rbegin(), multiplicand->rend(), [&rem, multiplier](ulong &i) {
ulong t = (i * multiplier) + rem ; ulong t = (i * multiplier) + rem ;
@ -56,7 +55,7 @@ namespace cheapgmp {
return makerep(base == 0 ? 0 : 1); return makerep(base == 0 ? 0 : 1);
} }
gmrep res = makerep(base); auto res = makerep(base);
ulong ct = power; ulong ct = power;
while(ct > 1) { while(ct > 1) {
multiply(res, base); multiply(res, base);

30
src/cmd.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
#include <string>
#include <sstream>
#include "cheapgmp.hpp"
#include "accessories.hpp"
using namespace cheapgmp;
using namespace std;
ulong reverse(ulong in) {
ulong rev = 0;
for(; in != 0; ) {
rev = (rev * 10) + (in % 10);
in = in / 10;
}
return rev;
}
int main(int argc, char* argv[]) {
ulong in;
istringstream ss(argv[1]);
if (!(ss >> in)) {
cerr << "Invalid number " << argv[1] << '\n';
exit(1);
}
wstring s2 = tostring(makepower(in, (reverse(in))));
std::wcout << s2 << endl;
}

View File

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include "cheapgmp.hpp" #include "cheapgmp.hpp"
#include "accessories.hpp"
using namespace cheapgmp; using namespace cheapgmp;
using namespace std; using namespace std;