Added math library that was created by hoangtuansu
This commit is contained in:
parent
51d97fa831
commit
f0d870c52d
7
Makefile
7
Makefile
|
@ -20,6 +20,7 @@ OBJECTS = src/checkautovariables.o \
|
|||
src/errorlogger.o \
|
||||
src/filelister.o \
|
||||
src/main.o \
|
||||
src/mathlib.o \
|
||||
src/preprocessor.o \
|
||||
src/settings.o \
|
||||
src/threadexecutor.o \
|
||||
|
@ -65,6 +66,7 @@ TESTOBJ = test/testautovariables.o \
|
|||
src/cppcheckexecutor.o \
|
||||
src/errorlogger.o \
|
||||
src/filelister.o \
|
||||
src/mathlib.o \
|
||||
src/preprocessor.o \
|
||||
src/settings.o \
|
||||
src/threadexecutor.o \
|
||||
|
@ -148,7 +150,10 @@ src/filelister.o: src/filelister.cpp src/filelister.h
|
|||
src/main.o: src/main.cpp src/cppcheckexecutor.h src/errorlogger.h src/settings.h
|
||||
$(CXX) $(CXXFLAGS) -c -o src/main.o src/main.cpp
|
||||
|
||||
src/preprocessor.o: src/preprocessor.cpp src/preprocessor.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h
|
||||
src/mathlib.o: src/mathlib.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o src/mathlib.o src/mathlib.cpp
|
||||
|
||||
src/preprocessor.o: src/preprocessor.cpp src/preprocessor.h src/errorlogger.h src/settings.h src/tokenize.h src/token.h
|
||||
$(CXX) $(CXXFLAGS) -c -o src/preprocessor.o src/preprocessor.cpp
|
||||
|
||||
src/settings.o: src/settings.cpp src/settings.h
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
#include "mathLib.h"
|
||||
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
|
||||
double MathLib::ToNumber(const std::string &str)
|
||||
{
|
||||
return atof(str.c_str());
|
||||
}
|
||||
|
||||
std::string MathLib::ToString(double d)
|
||||
{
|
||||
std::ostringstream result;
|
||||
result<<d;
|
||||
return result.str();
|
||||
}
|
||||
|
||||
std::string MathLib::add(const std::string & first, const std::string & second)
|
||||
{
|
||||
return ToString(ToNumber(first) + ToNumber(second));
|
||||
}
|
||||
|
||||
std::string MathLib::substract(const std::string &first, const std::string &second)
|
||||
{
|
||||
return ToString(ToNumber(first) - ToNumber(second));
|
||||
}
|
||||
|
||||
std::string MathLib::divide(const std::string &first, const std::string &second)
|
||||
{
|
||||
return ToString(ToNumber(first) / ToNumber(second));
|
||||
}
|
||||
|
||||
std::string MathLib::multiply(const std::string &first, const std::string &second)
|
||||
{
|
||||
return ToString(ToNumber(first) * ToNumber(second));
|
||||
}
|
||||
|
||||
|
||||
std::string MathLib::sin(const std::string &tok)
|
||||
{
|
||||
return ToString(::sin(ToNumber(tok)));
|
||||
}
|
||||
|
||||
|
||||
std::string MathLib::cos(const std::string &tok)
|
||||
{
|
||||
return ToString(::cos(ToNumber(tok)));
|
||||
}
|
||||
|
||||
std::string MathLib::tan(const std::string &tok)
|
||||
{
|
||||
return ToString(::tan(ToNumber(tok)));
|
||||
}
|
||||
|
||||
|
||||
std::string MathLib::abs(const std::string &tok)
|
||||
{
|
||||
return ToString(::abs(ToNumber(tok)));
|
||||
}
|
||||
|
||||
bool MathLib::IsGreater(const std::string &first, const std::string &second)
|
||||
{
|
||||
return ToNumber(first) > ToNumber(second);
|
||||
}
|
||||
|
||||
bool MathLib::IsEqual(const std::string &first, const std::string &second)
|
||||
{
|
||||
return ToNumber(first) == ToNumber(second);
|
||||
}
|
||||
|
||||
bool MathLib::IsLess(const std::string &first, const std::string &second)
|
||||
{
|
||||
return ToNumber(first) < ToNumber(second);
|
||||
}
|
||||
|
||||
int MathLib::compare(const std::string &first, const std::string &second)
|
||||
{
|
||||
if(ToNumber(first) < ToNumber(second))
|
||||
return -1;
|
||||
if(ToNumber(first) == ToNumber(second))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
#include "token.h"
|
||||
class MathLib
|
||||
{
|
||||
private:
|
||||
static double ToNumber(const std::string & str);
|
||||
static std::string ToString(double d);
|
||||
public:
|
||||
static std::string add(const std::string & first, const std::string & second);
|
||||
static std::string substract(const std::string & first, const std::string & second);
|
||||
static std::string multiply(const std::string & first, const std::string & second);
|
||||
static std::string divide(const std::string & first, const std::string & second);
|
||||
static std::string sin(const std::string & tok);
|
||||
static std::string cos(const std::string & tok);
|
||||
static std::string tan(const std::string & tok);
|
||||
static std::string abs(const std::string & tok);
|
||||
static bool IsGreater (const std::string & first, const std::string & second);
|
||||
static bool IsEqual (const std::string & first, const std::string & second);
|
||||
static bool IsLess (const std::string & first, const std::string & second);
|
||||
static int compare(const std::string & first, const std::string & second);
|
||||
};
|
Loading…
Reference in New Issue