Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
0b7e3822
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 搜索 >>
提交
0b7e3822
编写于
2月 09, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc_lib): 基本的大数加法(使用 make_unique 替代 new/delete), 只实现了整数
上级
86bb4a05
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
10 addition
and
21 deletion
+10
-21
calc_lib/src/khl_calc/khl_calc_decimal.cpp
calc_lib/src/khl_calc/khl_calc_decimal.cpp
+10
-21
未找到文件。
calc_lib/src/khl_calc/khl_calc_decimal.cpp
浏览文件 @
0b7e3822
#include <iostream>
#include <memory>
#include <cstring>
#include <sstream>
#include <algorithm>
...
...
@@ -8,36 +9,29 @@ KHL_CALC_NAMESPACE_BEGIN
std
::
string
Decimal
::
add
(
std
::
string
x
,
std
::
string
y
)
{
/** 入参转换为 int 数组, 并倒置 */
char
*
sx
=
const_cast
<
char
*>
(
x
.
c_str
());
char
*
sy
=
const_cast
<
char
*>
(
y
.
c_str
());
std
::
cout
<<
"sx:"
<<
sx
<<
std
::
endl
;
std
::
cout
<<
"sy:"
<<
sy
<<
std
::
endl
;
const
size_t
lx
=
strlen
(
sx
);
const
size_t
ly
=
strlen
(
sy
);
int
*
a
=
new
int
[
lx
]
;
int
*
b
=
new
int
[
ly
]
;
auto
a
=
std
::
make_unique
<
int
[]
>
(
lx
)
;
auto
b
=
std
::
make_unique
<
int
[]
>
(
ly
)
;
std
::
cout
<<
"sx > a"
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
lx
;
i
++
)
{
a
[
lx
-
i
]
=
sx
[
i
]
-
'0'
;
std
::
cout
<<
sx
[
i
]
<<
" "
<<
a
[
lx
-
i
]
<<
std
::
endl
;
}
std
::
cout
<<
"sx > a"
<<
std
::
endl
;
std
::
cout
<<
"sy > b"
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
ly
;
i
++
)
{
b
[
ly
-
i
]
=
sy
[
i
]
-
'0'
;
std
::
cout
<<
sy
[
i
]
<<
" "
<<
b
[
ly
-
i
]
<<
std
::
endl
;
}
std
::
cout
<<
"sy > b"
<<
std
::
endl
;
/** 计算 */
const
size_t
lc
=
std
::
max
(
lx
,
ly
)
+
1
;
int
*
result
=
new
int
[
lc
]
;
auto
result
=
std
::
make_unique
<
int
[]
>
(
lc
)
;
for
(
int
i
=
0
;
i
<
lc
;
i
++
)
{
...
...
@@ -46,30 +40,25 @@ std::string Decimal::add(std::string x, std::string y)
result
[
i
]
=
result
[
i
]
%
10
;
}
/** 删除前导0 */
size_t
len
=
lc
;
if
(
0
==
result
[
lc
]
&&
lc
>
0
)
{
len
--
;
}
std
::
cout
<<
"---"
<<
std
::
endl
;
char
*
c
=
new
char
[
lc
];
/** 翻转倒置的计算结果 */
auto
c
=
std
::
make_unique
<
char
[]
>
(
lc
);
for
(
int
i
=
len
,
j
=
0
;
i
>
0
;
i
--
,
j
++
)
{
std
::
cout
<<
result
[
i
]
<<
std
::
endl
;
c
[
j
]
=
result
[
i
]
+
'0'
;
}
std
::
cout
<<
"---"
<<
std
::
endl
;
std
::
string
str
;
std
::
stringstream
ss
;
ss
<<
c
;
ss
>>
str
;
delete
[]
a
;
delete
[]
b
;
delete
[]
result
;
delete
[]
c
;
return
str
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录