Skip to content

Commit 2e06162

Browse files
committed
add demonstration
1 parent 2e7f1bb commit 2e06162

10 files changed

+265
-47
lines changed

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,42 @@ reused on other projects (on other languages too).
1919

2020
The project is very flexible, but still very simple to use.
2121

22+
### Demonstration
23+
24+
See examples in `demo/` folder. GNU `make` with build three versions:
25+
- demo_hand: using [HandBigInt.hpp](./src/HandBigInt.hpp) (slow and the simplest to use)
26+
- demo_cshand: using [BigInteger.h](./src/BigInteger.h) + [BigIntegerHand.cpp](./src/BigIntegerHand.cpp) (slow)
27+
- demo_csgmp: using [BigInteger.h](./src/BigInteger.h) + [BigIntegerGMP.cpp](./src/BigIntegerGMP.cpp) (fast)
28+
29+
Example from `./demo_cshand` (print `-1`, `128` and `2^128`):
30+
31+
```
32+
demonstration for csBigInteger (Engine: 'HandBigInt')
33+
usage: <operation> <enter> <number> <enter> <number <enter>
34+
avaliable operations are: + - / * % ^ > < 0x 0b (0x and 0b are conversions to hex big endian and binary)
35+
36+
please type the operation: 0x
37+
expects 1 number(s)
38+
please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):-1
39+
number is (decimal) '-1'
40+
conversion: 0xff <----- Negative values have MSb set to one
41+
42+
please type the operation: 0x
43+
expects 1 number(s)
44+
please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):128
45+
number is (decimal) '128'
46+
conversion: 0x0080 <----- This zero on the left is correct and necessary for csBigInteger
47+
48+
please type the operation: ^
49+
expects 2 number(s)
50+
please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):2
51+
number is (decimal) '2'
52+
please type second number (in decimal):128
53+
number is '128'
54+
result: 340282366920938463463374607431768211456
55+
```
56+
57+
2258
### Using Hand Implementation
2359

2460
Just include the Single Header solution [src/HandBigInt.hpp](src/HandBigInt.hpp).

demo/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
demo_*

demo/cs_demo.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
#include <assert.h>
3+
#include <iostream>
4+
5+
#include "BigInteger.h"
6+
7+
using namespace csbiginteger;
8+
9+
int
10+
main()
11+
{
12+
std::cout << "demonstration for csBigInteger (Engine: '" << BigInteger::getEngine() << "')" << std::endl;
13+
std::cout << "usage: <operation> <enter> <number> <enter> <number <enter>" << std::endl;
14+
std::cout << "avaliable operations are: + - / * % ^ > < 0x 0b (0x and 0b are conversions to hex big endian and binary)" << std::endl;
15+
while (true) {
16+
std::cout << std::endl << "please type the operation: ";
17+
18+
std::string op;
19+
std::cin >> op;
20+
21+
int num = 0;
22+
if ((op == "+") || (op == "-") || (op == "/") || (op == "*") || (op == "%") || (op == "^") || (op == ">") || (op == "<"))
23+
num = 2;
24+
if ((op == "0x") || (op == "0b"))
25+
num = 1;
26+
assert(num > 0);
27+
std::cout << "expects " << num << " number(s)" << std::endl;
28+
29+
std::cout << "please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):";
30+
std::string sbig;
31+
std::cin >> sbig;
32+
33+
BigInteger big1(sbig);
34+
std::cout << "number is (decimal) '" << big1.ToString(10) << "'" << std::endl;
35+
36+
if (num > 1) {
37+
std::cout << "please type second number (in decimal):";
38+
std::string sbig2;
39+
std::cin >> sbig2;
40+
41+
BigInteger big2(sbig2);
42+
std::cout << "number is '" << big2.ToString(10) << "'" << std::endl;
43+
44+
std::cout << "result: ";
45+
46+
if (op == "+")
47+
std::cout << (big1 + big2).ToString(10) << std::endl;
48+
if (op == "-")
49+
std::cout << (big1 - big2).ToString(10) << std::endl;
50+
if (op == "/")
51+
std::cout << (big1 / big2).ToString(10) << std::endl;
52+
if (op == "*")
53+
std::cout << (big1 * big2).ToString(10) << std::endl;
54+
if (op == "%")
55+
std::cout << (big1 % big2).ToString(10) << std::endl;
56+
if (op == "^")
57+
std::cout << BigInteger::Pow(big1, big2.toInt()).ToString(10) << std::endl;
58+
if (op == ">")
59+
std::cout << (big1 > big2) << std::endl;
60+
if (op == "<")
61+
std::cout << (big1 < big2) << std::endl;
62+
63+
continue;
64+
}
65+
66+
std::cout << "conversion: ";
67+
if (op == "0x")
68+
std::cout << big1.ToString(16) << std::endl;
69+
if (op == "0b")
70+
std::cout << big1.ToString(2) << std::endl;
71+
}
72+
73+
return 0;
74+
}

demo/hand_demo.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
#include <assert.h>
3+
#include <iostream>
4+
5+
#include "HandBigInt.hpp"
6+
7+
int
8+
main()
9+
{
10+
std::cout << "demonstration for HandBigInt" << std::endl;
11+
std::cout << "usage: <operation> <enter> <number> <enter> <number <enter>" << std::endl;
12+
std::cout << "avaliable operations are: + - / * % ^ > < 0x 0b (0x and 0b are conversions to hex big endian and binary)" << std::endl;
13+
while (true) {
14+
std::cout << "please type the operation: ";
15+
16+
std::string op;
17+
std::cin >> op;
18+
19+
int num = 0;
20+
if ((op == "+") || (op == "-") || (op == "/") || (op == "*") || (op == "%") || (op == "^") || (op == ">") || (op == "<"))
21+
num = 2;
22+
if ((op == "0x") || (op == "0b"))
23+
num = 1;
24+
assert(num > 0);
25+
std::cout << "expects " << num << " number(s)" << std::endl;
26+
27+
std::cout << "please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):";
28+
std::string sbig;
29+
std::cin >> sbig;
30+
31+
HandBigInt big1;
32+
if ((sbig.length() >= 2) && (sbig[0] == '0') && (sbig[1] == 'x'))
33+
big1 = HandBigInt::fromUnsignedHex(sbig);
34+
else if ((sbig.length() >= 2) && (sbig[0] == '0') && (sbig[1] == 'b'))
35+
big1 = HandBigInt::fromUnsignedBin(sbig);
36+
else
37+
big1 = HandBigInt(sbig);
38+
std::cout << "number is (decimal) '" << big1.toString() << "'" << std::endl;
39+
40+
if (num > 1) {
41+
std::cout << "please type second number (in decimal):";
42+
std::string sbig2;
43+
std::cin >> sbig2;
44+
45+
HandBigInt big2(sbig2);
46+
std::cout << "number is '" << big2.toString() << "'" << std::endl;
47+
48+
std::cout << "result: ";
49+
50+
if (op == "+")
51+
std::cout << (big1 + big2).toString() << std::endl;
52+
if (op == "-")
53+
std::cout << (big1 - big2).toString() << std::endl;
54+
if (op == "/")
55+
std::cout << (big1 / big2).toString() << std::endl;
56+
if (op == "*")
57+
std::cout << (big1 * big2).toString() << std::endl;
58+
if (op == "%")
59+
std::cout << (big1 % big2).toString() << std::endl;
60+
if (op == "^")
61+
std::cout << HandBigInt::pow(big1, big2.get_ui()).toString() << std::endl;
62+
if (op == ">")
63+
std::cout << (big1 > big2) << std::endl;
64+
if (op == "<")
65+
std::cout << (big1 < big2) << std::endl;
66+
67+
continue;
68+
}
69+
70+
std::cout << "conversion: ";
71+
if (op == "0x")
72+
std::cout << big1.get_str(16) << std::endl;
73+
if (op == "0b")
74+
std::cout << big1.get_str(2) << std::endl;
75+
}
76+
77+
return 0;
78+
}

demo/makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: demo_hand demo_cshand demo_csgmp
2+
3+
demo_hand: hand_demo.cpp ../src/HandBigInt.hpp
4+
g++ -fsanitize=address -Wfatal-errors -pedantic --std=c++17 -I../src hand_demo.cpp -o demo_hand
5+
6+
demo_cshand: cs_demo.cpp
7+
g++ -fsanitize=address -Wfatal-errors -pedantic --std=c++17 -I../src cs_demo.cpp ../src/BigIntegerHand.cpp -o demo_cshand
8+
9+
demo_csgmp: cs_demo.cpp
10+
g++ -fsanitize=address -Wfatal-errors -pedantic --std=c++17 -I../src cs_demo.cpp ../src/BigIntegerGMP.cpp -o demo_csgmp -lgmp -lgmpxx
11+
12+
clean:
13+
rm -f demo_*

src/BigInteger.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class BigInteger final
2929
cs_vbyte _data;
3030

3131
public:
32+
33+
static std::string getEngine();
34+
3235
// size in bytes
3336
int Length() const
3437
{

src/BigIntegerGMP.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const BigInteger BigInteger::One = BigInteger(1);
4141
const BigInteger BigInteger::MinusOne = BigInteger(-1);
4242
const BigInteger BigInteger::Error = error();
4343

44+
std::string
45+
BigInteger::getEngine()
46+
{
47+
return "GMP";
48+
}
49+
50+
4451
const BigInteger
4552
BigInteger::error()
4653
{

src/BigIntegerHand.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ const BigInteger BigInteger::One = BigInteger(1);
3434
const BigInteger BigInteger::MinusOne = BigInteger(-1);
3535
const BigInteger BigInteger::Error = error();
3636

37+
std::string
38+
BigInteger::getEngine()
39+
{
40+
return "HandBigInt";
41+
}
42+
43+
3744
const BigInteger
3845
BigInteger::error()
3946
{

src/BigIntegerMono.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ const BigInteger BigInteger::One = BigInteger(1);
9494
const BigInteger BigInteger::MinusOne = BigInteger(-1);
9595
const BigInteger BigInteger::Error = error();
9696

97+
std::string
98+
BigInteger::getEngine()
99+
{
100+
return "Mono";
101+
}
102+
103+
97104
const BigInteger
98105
BigInteger::error()
99106
{

0 commit comments

Comments
 (0)