Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
ada3da79
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 搜索 >>
提交
ada3da79
编写于
2月 11, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc_lib): 大数减法, 只实现了整数
上级
ac67953d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
127 addition
and
19 deletion
+127
-19
calc_lib/include/khl_calc/khl_calc_decimal.h
calc_lib/include/khl_calc/khl_calc_decimal.h
+3
-1
calc_lib/src/khl_calc/khl_calc_decimal.cpp
calc_lib/src/khl_calc/khl_calc_decimal.cpp
+110
-12
calc_lib/test/testmain.cpp
calc_lib/test/testmain.cpp
+14
-6
未找到文件。
calc_lib/include/khl_calc/khl_calc_decimal.h
浏览文件 @
ada3da79
...
...
@@ -9,7 +9,9 @@ KHL_CALC_NAMESPACE_BEGIN
class
DllExport
Decimal
{
public:
std
::
string
add
(
std
::
string
num1
,
std
::
string
num2
);
std
::
string
add
(
std
::
string
x
,
std
::
string
y
);
std
::
string
substract
(
std
::
string
x
,
std
::
string
y
);
};
KHL_CALC_NAMESPACE_END
#endif // _KHL_CALC_DECIMAL_H_
\ No newline at end of file
calc_lib/src/khl_calc/khl_calc_decimal.cpp
浏览文件 @
ada3da79
...
...
@@ -5,29 +5,29 @@
KHL_CALC_NAMESPACE_BEGIN
std
::
string
Decimal
::
add
(
std
::
string
num1
,
std
::
string
num2
)
std
::
string
Decimal
::
add
(
std
::
string
x
,
std
::
string
y
)
{
/**
* 对齐数字
* x = 123 , y = 4567
* x = 0123 , y = 4567
*/
if
(
num1
.
size
()
!=
num2
.
size
())
if
(
x
.
size
()
!=
y
.
size
())
{
if
(
num1
.
size
()
>
num2
.
size
())
if
(
x
.
size
()
>
y
.
size
())
{
auto
len
=
num1
.
size
()
-
num2
.
size
();
auto
len
=
x
.
size
()
-
y
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
num2
.
insert
(
0
,
"0"
);
y
.
insert
(
0
,
"0"
);
}
}
else
{
auto
len
=
num2
.
size
()
-
num1
.
size
();
auto
len
=
y
.
size
()
-
x
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
num1
.
insert
(
0
,
"0"
);
x
.
insert
(
0
,
"0"
);
}
}
}
...
...
@@ -37,9 +37,9 @@ std::string Decimal::add(std::string num1, std::string num2)
* x = 0123 , y = 4567
* x = 3210 , y = 7654
*/
char
*
sx
=
const_cast
<
char
*>
(
num1
.
c_str
());
char
*
sy
=
const_cast
<
char
*>
(
num2
.
c_str
());
const
size_t
len
=
num1
.
size
();
char
*
sx
=
const_cast
<
char
*>
(
x
.
c_str
());
char
*
sy
=
const_cast
<
char
*>
(
y
.
c_str
());
const
size_t
len
=
x
.
size
();
auto
a
=
std
::
make_unique
<
int
[]
>
(
len
);
auto
b
=
std
::
make_unique
<
int
[]
>
(
len
);
...
...
@@ -71,11 +71,109 @@ std::string Decimal::add(std::string num1, std::string num2)
/** 翻转计算结果 */
std
::
string
str
;
int
i
=
size
-
1
;
if
(
0
==
c
[
i
])
if
(
0
==
c
[
i
])
{
i
--
;
}
while
(
i
>=
0
)
while
(
i
>=
0
)
{
str
.
push_back
(
c
[
i
]
+
'0'
);
i
--
;
}
return
str
;
}
std
::
string
Decimal
::
substract
(
std
::
string
x
,
std
::
string
y
)
{
// 结果是否为负数
bool
isNegativeNumber
=
false
;
/**
* 对齐数字
* x = 123 , y = 4567
* x = 0123 , y = 4567
*/
if
(
x
.
size
()
!=
y
.
size
())
{
if
(
x
.
size
()
>
y
.
size
())
{
auto
len
=
x
.
size
()
-
y
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
y
.
insert
(
0
,
"0"
);
}
}
else
{
auto
len
=
y
.
size
()
-
x
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
x
.
insert
(
0
,
"0"
);
}
}
}
/**
* 数字排序
* s1 为较大数字
* s2 为较小数字
*/
std
::
string
s1
=
x
;
std
::
string
s2
=
y
;
if
(
s1
.
compare
(
s2
)
<
0
)
{
s1
=
y
;
s2
=
x
;
isNegativeNumber
=
true
;
}
/**
* 对齐后的字符串转换为倒置的 int 数组
* x = 0123 , y = 4567
* x = 3210 , y = 7654
*/
char
*
sx
=
const_cast
<
char
*>
(
s1
.
c_str
());
char
*
sy
=
const_cast
<
char
*>
(
s2
.
c_str
());
const
size_t
len
=
s1
.
size
();
auto
a
=
std
::
make_unique
<
int
[]
>
(
len
);
auto
b
=
std
::
make_unique
<
int
[]
>
(
len
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
a
[
len
-
1
-
i
]
=
sx
[
i
]
-
'0'
;
}
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
b
[
len
-
1
-
i
]
=
sy
[
i
]
-
'0'
;
}
/** 计算 */
const
size_t
size
=
len
;
auto
c
=
std
::
make_unique
<
int
[]
>
(
size
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
// 借位处理
if
(
a
[
i
]
<
b
[
i
])
{
a
[
i
+
1
]
--
;
a
[
i
]
+=
10
;
}
c
[
i
]
=
a
[
i
]
-
b
[
i
];
}
/** 翻转计算结果 */
std
::
string
str
;
int
i
=
len
-
1
;
while
(
0
==
c
[
i
]
&&
i
>
1
)
{
i
--
;
}
if
(
isNegativeNumber
){
str
.
push_back
(
'-'
);
}
while
(
i
>=
0
)
{
str
.
push_back
(
c
[
i
]
+
'0'
);
i
--
;
...
...
calc_lib/test/testmain.cpp
浏览文件 @
ada3da79
...
...
@@ -19,10 +19,18 @@ TEST_CASE("test_add")
std
::
cout
<<
decimal
->
add
(
"465465465464"
,
"1321654876135138468135135"
)
<<
std
::
endl
;
std
::
cout
<<
"==================="
<<
std
::
endl
;
}
// std::cout << decimal->add("1", "12") << std::endl;
// std::cout << decimal->add("1", "10") << std::endl;
// std::cout << decimal->add("12", "1") << std::endl;
// std::cout << decimal->add("10", "1") << 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
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录