Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
8b23cdd8
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,发现更多精彩内容 >>
提交
8b23cdd8
编写于
2月 11, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc_lib): 优化大数加法, 只实现了整数
上级
0b7e3822
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
151 addition
and
52 deletion
+151
-52
calc_lib/.vscode/settings.json
calc_lib/.vscode/settings.json
+75
-1
calc_lib/include/khl_calc/khl_calc_decimal.h
calc_lib/include/khl_calc/khl_calc_decimal.h
+2
-2
calc_lib/src/khl_calc/khl_calc_decimal.cpp
calc_lib/src/khl_calc/khl_calc_decimal.cpp
+56
-34
calc_lib/src/main/main.cpp
calc_lib/src/main/main.cpp
+5
-3
calc_lib/test/testmain.cpp
calc_lib/test/testmain.cpp
+13
-12
未找到文件。
calc_lib/.vscode/settings.json
浏览文件 @
8b23cdd8
{
"files.associations"
:
{
"xstring"
:
"cpp"
"xstring"
:
"cpp"
,
"algorithm"
:
"cpp"
,
"array"
:
"cpp"
,
"atomic"
:
"cpp"
,
"bit"
:
"cpp"
,
"cctype"
:
"cpp"
,
"charconv"
:
"cpp"
,
"chrono"
:
"cpp"
,
"clocale"
:
"cpp"
,
"cmath"
:
"cpp"
,
"compare"
:
"cpp"
,
"concepts"
:
"cpp"
,
"condition_variable"
:
"cpp"
,
"csignal"
:
"cpp"
,
"cstdarg"
:
"cpp"
,
"cstddef"
:
"cpp"
,
"cstdint"
:
"cpp"
,
"cstdio"
:
"cpp"
,
"cstdlib"
:
"cpp"
,
"cstring"
:
"cpp"
,
"ctime"
:
"cpp"
,
"cwchar"
:
"cpp"
,
"exception"
:
"cpp"
,
"format"
:
"cpp"
,
"forward_list"
:
"cpp"
,
"fstream"
:
"cpp"
,
"functional"
:
"cpp"
,
"initializer_list"
:
"cpp"
,
"iomanip"
:
"cpp"
,
"ios"
:
"cpp"
,
"iosfwd"
:
"cpp"
,
"iostream"
:
"cpp"
,
"istream"
:
"cpp"
,
"iterator"
:
"cpp"
,
"limits"
:
"cpp"
,
"list"
:
"cpp"
,
"locale"
:
"cpp"
,
"map"
:
"cpp"
,
"memory"
:
"cpp"
,
"mutex"
:
"cpp"
,
"new"
:
"cpp"
,
"optional"
:
"cpp"
,
"ostream"
:
"cpp"
,
"ratio"
:
"cpp"
,
"set"
:
"cpp"
,
"span"
:
"cpp"
,
"sstream"
:
"cpp"
,
"stdexcept"
:
"cpp"
,
"stop_token"
:
"cpp"
,
"streambuf"
:
"cpp"
,
"string"
:
"cpp"
,
"system_error"
:
"cpp"
,
"thread"
:
"cpp"
,
"tuple"
:
"cpp"
,
"type_traits"
:
"cpp"
,
"typeinfo"
:
"cpp"
,
"unordered_map"
:
"cpp"
,
"unordered_set"
:
"cpp"
,
"utility"
:
"cpp"
,
"vector"
:
"cpp"
,
"xfacet"
:
"cpp"
,
"xhash"
:
"cpp"
,
"xiosbase"
:
"cpp"
,
"xlocale"
:
"cpp"
,
"xlocbuf"
:
"cpp"
,
"xlocinfo"
:
"cpp"
,
"xlocmes"
:
"cpp"
,
"xlocmon"
:
"cpp"
,
"xlocnum"
:
"cpp"
,
"xloctime"
:
"cpp"
,
"xmemory"
:
"cpp"
,
"xstddef"
:
"cpp"
,
"xtr1common"
:
"cpp"
,
"xtree"
:
"cpp"
,
"xutility"
:
"cpp"
}
}
\ No newline at end of file
calc_lib/include/khl_calc/khl_calc_decimal.h
浏览文件 @
8b23cdd8
#ifndef
_KHL_CALC_DECIMAL_H_
#define
_KHL_CALC_DECIMAL_H_
#ifndef _KHL_CALC_DECIMAL_H_
#define _KHL_CALC_DECIMAL_H_
#include <string>
...
...
calc_lib/src/khl_calc/khl_calc_decimal.cpp
浏览文件 @
8b23cdd8
#include <iostream>
#include <memory>
#include <cstring>
#include <sstream>
#include <algorithm>
#include "khl_calc/khl_calc_decimal.h"
...
...
@@ -9,56 +7,80 @@ 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
());
const
size_t
lx
=
strlen
(
sx
);
const
size_t
ly
=
strlen
(
sy
);
/**
* 对齐数字
* 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"
);
}
}
}
auto
a
=
std
::
make_unique
<
int
[]
>
(
lx
);
auto
b
=
std
::
make_unique
<
int
[]
>
(
ly
);
/**
* 对齐后的字符串转换为倒置的 int 数组
* x = 0123 , y = 4567
* x = 3210 , y = 7654
*/
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
);
for
(
int
i
=
0
;
i
<
l
x
;
i
++
)
for
(
int
i
=
0
;
i
<
l
en
;
i
++
)
{
a
[
l
x
-
i
]
=
sx
[
i
]
-
'0'
;
a
[
l
en
-
1
-
i
]
=
sx
[
i
]
-
'0'
;
}
for
(
int
i
=
0
;
i
<
l
y
;
i
++
)
for
(
int
i
=
0
;
i
<
l
en
;
i
++
)
{
b
[
l
y
-
i
]
=
sy
[
i
]
-
'0'
;
b
[
l
en
-
1
-
i
]
=
sy
[
i
]
-
'0'
;
}
/** 计算 */
const
size_t
lc
=
std
::
max
(
lx
,
ly
)
+
1
;
auto
result
=
std
::
make_unique
<
int
[]
>
(
lc
);
const
size_t
size
=
len
+
1
;
auto
c
=
std
::
make_unique
<
int
[]
>
(
size
);
for
(
int
i
=
0
;
i
<
l
c
;
i
++
)
for
(
int
i
=
0
;
i
<
l
en
;
i
++
)
{
result
[
i
]
=
a
[
i
]
+
b
[
i
];
result
[
i
+
1
]
=
result
[
i
]
/
10
;
result
[
i
]
=
result
[
i
]
%
10
;
c
[
i
]
+=
a
[
i
]
+
b
[
i
];
// 处理进位
if
(
c
[
i
]
>=
10
)
{
c
[
i
+
1
]
+=
c
[
i
]
/
10
;
c
[
i
]
=
c
[
i
]
%
10
;
}
}
/** 删除前导0 */
size_t
len
=
lc
;
if
(
0
==
result
[
lc
]
&&
lc
>
0
)
/** 翻转计算结果 */
std
::
string
str
;
int
i
=
size
-
1
;
if
(
0
==
c
[
i
])
{
len
--
;
i
--
;
}
/** 翻转倒置的计算结果 */
auto
c
=
std
::
make_unique
<
char
[]
>
(
lc
);
for
(
int
i
=
len
,
j
=
0
;
i
>
0
;
i
--
,
j
++
)
while
(
i
>=
0
)
{
c
[
j
]
=
result
[
i
]
+
'0'
;
str
.
push_back
(
c
[
i
]
+
'0'
);
i
--
;
}
std
::
string
str
;
std
::
stringstream
ss
;
ss
<<
c
;
ss
>>
str
;
return
str
;
}
...
...
calc_lib/src/main/main.cpp
浏览文件 @
8b23cdd8
...
...
@@ -4,9 +4,11 @@
int
main
()
{
std
::
cout
<<
"hello khl_calc"
<<
std
::
endl
;
auto
decimal
=
std
::
make_unique
<
khl
::
calc
::
Decimal
>
();
std
::
string
str
=
decimal
->
add
(
"1"
,
"2"
);
std
::
cout
<<
"result: "
<<
str
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
std
::
cout
<<
decimal
->
add
(
"465465465464"
,
"1321654876135138468135135"
)
<<
std
::
endl
;
std
::
cout
<<
"==================="
<<
std
::
endl
;
}
return
0
;
}
\ No newline at end of file
calc_lib/test/testmain.cpp
浏览文件 @
8b23cdd8
...
...
@@ -10,18 +10,19 @@
#include "khl_calc/khl_calc.h"
TEST_CASE
(
"test_
001
"
)
TEST_CASE
(
"test_
add
"
)
{
spdlog
::
info
(
"test
001
"
);
spdlog
::
info
(
"test
add
"
);
auto
decimal
=
std
::
make_unique
<
khl
::
calc
::
Decimal
>
();
std
::
string
str
=
decimal
->
add
(
"84964651354684615354684"
,
"498461351384864651684"
);
std
::
cout
<<
"result: "
<<
str
<<
std
::
endl
;
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
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_002"
)
{
char
a
=
'9'
;
int
b
=
a
-
'0'
;
std
::
cout
<<
b
<<
std
::
endl
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录