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
SOURCES= src/cheapgmp.cpp src/accessories.cpp
MAINSRC= src/main.cpp
CMDSRC= src/cmd.cpp
TESTSOURCES= tests/test_cheapgmp.cpp
LDFLAGS=
TESTLDFLAGS= -laeryn_tests -laeryn_core
CFLAGS= -c -Wall -std=c++11
CPLUSPLUS= g++
BINARY=cheapgmp
CMD=gmpcmd
TESTBINARY=testrunner
OBJECTS=$(SOURCES:.cpp=.o)
TESTOBJECTS=$(TESTSOURCES:.cpp=.o)
MAINOBJECTS=$(MAINSRC:.cpp=.o)
CMDOBJECTS=$(CMDSRC:.cpp=.o)
all: $(SOURCES) $(BINARY)
@ -20,6 +23,9 @@ $(BINARY): $(OBJECTS) $(MAINOBJECTS)
app: $(BINARY)
gmpcmd: $(CMDOBJECTS) $(OBJECTS)
$(CPLUSPLUS) $(OBJECTS) $(CMDOBJECTS) $(LDFLAGS) -o $@
testrunner: $(OBJECTS) $(TESTOBJECTS)
$(CPLUSPLUS) $(OBJECTS) $(TESTOBJECTS) $(LDFLAGS) $(TESTLDFLAGS) -o $@
@ -30,7 +36,7 @@ test: $(BINARY)
./$(BINARY)
distclean: clean
rm -f $(BINARY)
rm -f $(BINARY) $(TESTBINARY) $(CMD)
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 */
void multiply(gmrep &multiplicand, ulong multiplier) {
gmrep res(new lmrep());
ulong rem = 0;
for_each(multiplicand->rbegin(), multiplicand->rend(), [&rem, multiplier](ulong &i) {
ulong t = (i * multiplier) + rem ;
@ -56,7 +55,7 @@ namespace cheapgmp {
return makerep(base == 0 ? 0 : 1);
}
gmrep res = makerep(base);
auto res = makerep(base);
ulong ct = power;
while(ct > 1) {
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 "cheapgmp.hpp"
#include "accessories.hpp"
using namespace cheapgmp;
using namespace std;