提交 6172deb5 编写于 作者: 小代码2016's avatar 小代码2016

feat(calc_lib): decimal 初始化

上级 c36cd37e
......@@ -6,7 +6,7 @@ project(khl_calc VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CONFIGURATION_TYPES Debug)
# set(CMAKE_CONFIGURATION_TYPES Debug)
# 设置 cmake 脚本目录
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
......
#ifndef _KHL_CALC_H_
#define _KHL_CALC_H_
#include "khl_calc/khl_calc_exception.hpp"
#include "khl_calc/khl_calc_decimal.h"
// C/C++ 跨平台时预处理判断平台环境
......
......@@ -60,6 +60,9 @@ public:
Decimal &operator=(Decimal &val);
~Decimal();
private:
void init(std::string val);
public:
/**
* @brief 返回 Decimal 的小数位数
......@@ -140,7 +143,7 @@ private:
/**
* @brief 字符串形式的 Decimal 值
*/
std::string val;
std::string _val;
};
KHL_CALC_NAMESPACE_END
#endif // _KHL_CALC_DECIMAL_H_
\ No newline at end of file
#endif // _KHL_CALC_DECIMAL_H_
/**
* @file khl_calc_exception.hpp
* @brief 异常体系
*/
#ifndef _KHL_CALC_EXCEPTION_H_
#define _KHL_CALC_EXCEPTION_H_
#include <string>
#include "khl_calc_common.h"
KHL_CALC_NAMESPACE_BEGIN
/**
* @brief Calc 库的异常基类
*/
class DllExport CalcException
{
public:
CalcException(std::string msg) : _msg(msg)
{
}
protected:
/**
* 异常信息. 由 msg() 返回
*/
std::string _msg;
public:
std::string msg()
{
return _msg;
}
};
/**
* @brief Decimal 异常基类
*/
class DllExport CalcDecimalException : public CalcException
{
public:
CalcDecimalException(std::string msg) : CalcException(msg)
{
}
};
/**
* @brief Decimal 初始化异常.
* 抛出次异常原因通常是 Decimal(std::string) 的入参非法, 例如空字符串、 或者字符串不能表示为数字, 例如: new Decimal("1.1.1")
*/
class DllExport CalcDecimalInitException : public CalcDecimalException
{
public:
CalcDecimalInitException(std::string msg) : CalcDecimalException(msg)
{
}
};
KHL_CALC_NAMESPACE_END
#endif // _KHL_CALC_EXCEPTION_H_
#include <iostream>
#include <memory>
#include <algorithm>
#include "khl_calc/khl_calc_exception.hpp"
#include "khl_calc/khl_calc_decimal.h"
KHL_CALC_NAMESPACE_BEGIN
Decimal::Decimal(int val)
{
init(std::to_string(val));
}
Decimal::Decimal(std::string val)
{
// 输入字符串不能为空
if( val.empty())
{
throw CalcDecimalInitException("Init error: input string can't be empty string.");
}
// 输入字符串必须为合法的数字
auto chars = val.c_str();
init(val);
}
Decimal::Decimal(long val)
{
init(std::to_string(val));
}
Decimal::Decimal(Decimal &val)
......@@ -28,9 +42,15 @@ Decimal::~Decimal()
{
}
void Decimal::init(std::string val)
{
_val = val;
_scale = 0;
}
int Decimal::scale()
{
return 0;
return _scale;
}
int Decimal::compareTo(Decimal val)
......
......@@ -10,47 +10,15 @@
#include "khl_calc/khl_calc.h"
TEST_CASE("test_add")
TEST_CASE("test_init")
{
spdlog::info("test add");
auto decimal = std::make_unique<khl::calc::Decimal>();
for (int i = 0; i < 10; i++)
spdlog::info("test init");
try
{
std::cout << decimal->add("465465465464", "1321654876135138468135135") << std::endl;
std::cout << "===================" << std::endl;
auto decimal = std::make_unique<khl::calc::Decimal>("");
}
catch(khl::calc::CalcDecimalInitException e)
{
std::cerr << e.msg() << std::endl;;
}
}
TEST_CASE("test_substract")
{
spdlog::info("test substract");
auto decimal = std::make_unique<khl::calc::Decimal>();
// std::cout << decimal->substract("0", "0") << std::endl;
// std::cout << decimal->substract("0", "1") << std::endl;
// std::cout << decimal->substract("1", "0") << std::endl;
std::cout << decimal->substract("123", "123") << std::endl;
// std::cout << decimal->substract("123", "4567") << std::endl;
// std::cout << decimal->substract("4567", "123") << std::endl;
// std::cout << decimal->substract("684651316546", "46513516846165") << std::endl;
}
TEST_CASE("test_multiply")
{
spdlog::info("test multiply");
auto decimal = std::make_unique<khl::calc::Decimal>();
std::cout << decimal->multiply("9133", "0") << std::endl;
}
TEST_CASE("test_divide")
{
spdlog::info("test divide");
auto decimal = std::make_unique<khl::calc::Decimal>();
// std::cout << decimal->divide("10", "5") << std::endl;
// std::cout << decimal->divide("531518", "123") << std::endl;
std::cout << decimal->divide("222", "2") << std::endl;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册