Added basic integer prototype

This commit is contained in:
Robert 2020-07-18 21:06:17 +02:00
parent 0061f9692f
commit 95282f7d1a
7 changed files with 162 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.vs
out
3rdparty
*.json

View file

@ -1,9 +1,31 @@
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
cmake_minimum_required (VERSION 3.8)
project ("GMP++")
project ("GM3p")
set(GMP_INCLUDE_DIR "" CACHE PATH "Path to the directory containing 'gmp.h'")
if(CMAKE_BUILD_TYPE EQUAL "DEBUG")
set(GMP_LIBRARY "" CACHE FILEPATH "Path to the gmp library")
else(CMAKE_BUILD_TYPE EQUAL "RELEASE")
set(GMP_LIBRARY "" CACHE FILEPATH "Path to the gmpDebug library")
endif()
set(ENABLE_TEST_PROJ FALSE CACHE BOOL "Enable the testing project (usually not needed)")
add_library(gm3p STATIC
"include/gm3p_int.hpp" "src/gm3p_int.cpp"
)
target_include_directories(gm3p PUBLIC
include
${GMP_INCLUDE_DIR}
)
target_link_libraries(gm3p PUBLIC
${GMP_LIBRARY}
)
# Include sub-projects.
add_subdirectory ("GMP++")
if(ENABLE_TEST_PROJ)
add_subdirectory(testapp)
endif()

4
include/gm3p.hpp Normal file
View file

@ -0,0 +1,4 @@
#pragma once
#include <iostream>
#include "gm3p_int.hpp"

28
include/gm3p_int.hpp Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include "gmp.h"
namespace gm3p
{
class gInt
{
public:
gInt();
gInt(mpz_t init);
gInt(long int init);
gInt(const char* init, int base);
~gInt();
gInt& operator=(const gInt& other);
friend gInt operator+(const gInt& left, const gInt& right);
friend gInt operator-(const gInt& left, const gInt& right);
friend gInt operator*(const gInt& left, const gInt& right);
friend gInt operator/(const gInt& left, const gInt& right);
friend std::ostream& operator<<(std::ostream& os, const gInt& value);
private:
mpz_t value;
};
}

71
src/gm3p_int.cpp Normal file
View file

@ -0,0 +1,71 @@
#include "gm3p.hpp"
namespace gm3p
{
gInt::gInt()
{
mpz_init(value);
}
gInt::gInt(mpz_t init)
: gInt::gInt()
{
mpz_set(value, init);
}
gInt::gInt(long int init) :
gInt::gInt()
{
mpz_set_si(value, init);
}
gInt::gInt(const char* init, int base) :
gInt::gInt()
{
mpz_set_str(value, init, base);
}
gInt::~gInt()
{
mpz_clear(value);
}
gInt& gInt::operator=(const gInt& other)
{
mpz_set(this->value, other.value);
}
gInt operator+(const gInt& left, const gInt& right)
{
gInt ret;
mpz_add(ret.value, left.value, right.value);
return ret;
}
gInt operator-(const gInt& left, const gInt& right)
{
gInt ret;
mpz_sub(ret.value, left.value, right.value);
return ret;
}
gInt operator*(const gInt& left, const gInt& right)
{
gInt ret;
mpz_mul(ret.value, left.value, right.value);
return ret;
}
gInt operator/(const gInt& left, const gInt& right)
{
gInt ret;
mpz_div(ret.value, left.value, right.value);
return ret;
}
std::ostream& operator<<(std::ostream& os, const gInt& value)
{
static char* buf;
gmp_asprintf(&buf, "%Zd", value);
os << buf;
}
}

13
testapp/CMakeLists.txt Normal file
View file

@ -0,0 +1,13 @@
add_executable(testapp
"main.cpp"
)
target_include_directories(testapp PUBLIC
${GMP_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/include
)
target_link_libraries(testapp PUBLIC
${GMP_LIBRARY}
gm3p
)

17
testapp/main.cpp Normal file
View file

@ -0,0 +1,17 @@
#include "gm3p.hpp"
#include <iostream>
using namespace gm3p;
int main(int argc, char** argv)
{
gInt a("12234234234095780349859034869034635235", 10);
gInt b("123534622305820985092875207398734234753", 10);
gInt c = a * b;
std::cout << a << " * " << b << " = " << c << std::endl;
return 0;
}