Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
12903bc0
C
calc
项目概览
小代码2016
/
calc
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
2
Wiki
分析
仓库
DevOps
项目成员
Pages
C
calc
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
2
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
12903bc0
编写于
2月 27, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc): add
上级
3ab29ea9
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
89 addition
and
13 deletion
+89
-13
calc_lib/include/khl_calc/khl_calc_decimal.h
calc_lib/include/khl_calc/khl_calc_decimal.h
+9
-1
calc_lib/include/khl_calc/khl_calc_exception.hpp
calc_lib/include/khl_calc/khl_calc_exception.hpp
+13
-0
calc_lib/src/khl_calc/khl_calc_decimal.cpp
calc_lib/src/khl_calc/khl_calc_decimal.cpp
+32
-3
calc_lib/src/main/main.cpp
calc_lib/src/main/main.cpp
+3
-0
calc_lib/test/test_decimal_add.cpp
calc_lib/test/test_decimal_add.cpp
+32
-0
calc_lib/test/testmain.cpp
calc_lib/test/testmain.cpp
+0
-9
未找到文件。
calc_lib/include/khl_calc/khl_calc_decimal.h
浏览文件 @
12903bc0
...
...
@@ -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())
...
...
calc_lib/include/khl_calc/khl_calc_exception.hpp
浏览文件 @
12903bc0
...
...
@@ -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_
calc_lib/src/khl_calc/khl_calc_decimal.cpp
浏览文件 @
12903bc0
/**
* @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
;
}
...
...
calc_lib/src/main/main.cpp
浏览文件 @
12903bc0
...
...
@@ -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
calc_lib/test/test_decimal_add.cpp
0 → 100644
浏览文件 @
12903bc0
#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
calc_lib/test/testmain.cpp
浏览文件 @
12903bc0
#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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录