Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
6172deb5
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 搜索 >>
提交
6172deb5
编写于
2月 15, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc_lib): decimal 初始化
上级
c36cd37e
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
97 addition
and
44 deletion
+97
-44
calc_lib/CMakeLists.txt
calc_lib/CMakeLists.txt
+1
-1
calc_lib/include/khl_calc/khl_calc.h
calc_lib/include/khl_calc/khl_calc.h
+1
-0
calc_lib/include/khl_calc/khl_calc_decimal.h
calc_lib/include/khl_calc/khl_calc_decimal.h
+5
-2
calc_lib/include/khl_calc/khl_calc_exception.hpp
calc_lib/include/khl_calc/khl_calc_exception.hpp
+61
-0
calc_lib/src/khl_calc/khl_calc_decimal.cpp
calc_lib/src/khl_calc/khl_calc_decimal.cpp
+21
-1
calc_lib/test/testmain.cpp
calc_lib/test/testmain.cpp
+8
-40
未找到文件。
calc_lib/CMakeLists.txt
浏览文件 @
6172deb5
...
...
@@ -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
)
...
...
calc_lib/include/khl_calc/khl_calc.h
浏览文件 @
6172deb5
#ifndef _KHL_CALC_H_
#define _KHL_CALC_H_
#include "khl_calc/khl_calc_exception.hpp"
#include "khl_calc/khl_calc_decimal.h"
// C/C++ 跨平台时预处理判断平台环境
...
...
calc_lib/include/khl_calc/khl_calc_decimal.h
浏览文件 @
6172deb5
...
...
@@ -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_
calc_lib/include/khl_calc/khl_calc_exception.hpp
0 → 100644
浏览文件 @
6172deb5
/**
* @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_
calc_lib/src/khl_calc/khl_calc_decimal.cpp
浏览文件 @
6172deb5
#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
)
...
...
calc_lib/test/testmain.cpp
浏览文件 @
6172deb5
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录