提交 12903bc0 编写于 作者: 小代码2016's avatar 小代码2016

feat(calc): add

上级 3ab29ea9
......@@ -63,6 +63,14 @@ public:
private:
void init(std::string val);
/**
* @brief 前置填充 0
* @param s 需要填充的字符串
* @param len 填充后的长度
* @return 填充后的字符串
*/
std::string fillZeroPrefix(std::string s, int len);
public:
/**
* @brief 返回 Decimal 的小数位数
......@@ -90,7 +98,7 @@ public:
* @param augend 被加数
* @return 计算结果, 一个新的 Decimal
*/
Decimal add(Decimal augend);
Decimal add(Decimal *augend);
/**
* @brief 返回一个新的 Decimal , 其值是 (this-augend), 其小数位数是 max(this.sacle(),augend.scale())
......
......@@ -57,5 +57,18 @@ public:
{
}
};
/**
* @brief Decimal 参数异常.
* 抛出次异常原因通常是 Decimal::[add,substract,plus,divide]的入参非法, 例如null, 0 等
*/
class DllExport CalcDecimalIllegalParameterException : public CalcDecimalException
{
public:
CalcDecimalIllegalParameterException(std::string msg) : CalcDecimalException(msg)
{
}
};
KHL_CALC_NAMESPACE_END
#endif // _KHL_CALC_EXCEPTION_H_
/**
* @file khl_calc_decimal.cpp
* @brief Decimal 实现
*/
*/
#include <iostream>
#include <memory>
#include <algorithm>
......@@ -18,7 +18,7 @@ Decimal::Decimal(int val)
Decimal::Decimal(std::string val)
{
// 输入字符串不能为空
if( !CalcUtil::isNumber(val))
if (!CalcUtil::isNumber(val))
{
throw CalcDecimalInitException("Init error: the input is not a number.");
}
......@@ -64,8 +64,37 @@ std::string Decimal::format(std::string pattern)
return nullptr;
}
Decimal Decimal::add(Decimal augend)
std::string Decimal::fillZeroPrefix(std::string s, int len)
{
if (len == s.size())
{
return s;
}
int count = len - s.size();
std::string str;
for (int i = 0; i < count; i++)
{
str.push_back('0');
}
str.append(s);
return str;
}
Decimal Decimal::add(Decimal *augend)
{
if (nullptr == augend)
{
throw CalcDecimalIllegalParameterException("Illegal parameter: the augend cannot be null.");
}
std::string x = _val;
std::string y = augend->_val;
const int len = std::max(x.size(), y.size());
x = fillZeroPrefix(x, len);
y = fillZeroPrefix(y, len);
std::cout << x << std::endl;
std::cout << y << std::endl;
return nullptr;
}
......
......@@ -5,5 +5,8 @@
int main()
{
auto d1 = std::make_unique<khl::calc::Decimal>("123");
auto d2 = std::make_unique<khl::calc::Decimal>("4567");
d1->add(d2.get());
return 0;
}
\ No newline at end of file
#include <memory>
#include <string>
#include <iostream>
#include "doctest/doctest.h"
#include "khl_calc/khl_calc.h"
void add(khl::calc::Decimal *d1, khl::calc::Decimal *d2)
{
// d1->add(d2);
}
TEST_CASE("test_add")
{
SUBCASE("test_add_001")
{
// auto d = std::make_unique<khl::calc::Decimal>("123");
// auto d2 = std::make_unique<khl::calc::Decimal>("4567");
// khl::calc::Decimal *dd = d2.get();
// d->add(dd);
khl::calc::Decimal *d1 = new khl::calc::Decimal("123");
khl::calc::Decimal *d2 = new khl::calc::Decimal("4567");
add(d1, d2);
delete d1;
delete d2;
std::cout << "test add" << std::endl;
}
}
\ No newline at end of file
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <string>
#include <memory>
#include <cstring>
#include <sstream>
#include <cctype>
#include <vector>
#include "doctest/doctest.h"
#include "spdlog/spdlog.h"
#include "khl_calc/khl_calc.h"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册