Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
1e3efe22
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 搜索 >>
提交
1e3efe22
编写于
2月 12, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc_lib): 优化大数除法, 只实现了整数
上级
d3d21c4a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
40 addition
and
26 deletion
+40
-26
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
+36
-22
calc_lib/src/main/main.cpp
calc_lib/src/main/main.cpp
+1
-1
calc_lib/test/testmain.cpp
calc_lib/test/testmain.cpp
+1
-1
未找到文件。
calc_lib/include/khl_calc/khl_calc_decimal.h
浏览文件 @
1e3efe22
...
...
@@ -17,13 +17,13 @@ public:
std
::
string
divide
(
std
::
string
x
,
std
::
string
y
);
const
int
compare
(
std
::
string
x
,
std
::
string
y
);
int
compare
(
std
::
string
x
,
std
::
string
y
);
private:
/**
* 比较两个数字的大小
*/
//
const
int compare(std::string x, std::string y);
// int compare(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
浏览文件 @
1e3efe22
...
...
@@ -16,7 +16,7 @@ std::string Decimal::add(std::string x, std::string y)
{
if
(
x
.
size
()
>
y
.
size
())
{
auto
len
=
x
.
size
()
-
y
.
size
();
int
len
=
x
.
size
()
-
y
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
y
.
insert
(
0
,
"0"
);
...
...
@@ -24,7 +24,7 @@ std::string Decimal::add(std::string x, std::string y)
}
else
{
auto
len
=
y
.
size
()
-
x
.
size
();
int
len
=
y
.
size
()
-
x
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
x
.
insert
(
0
,
"0"
);
...
...
@@ -39,7 +39,7 @@ std::string Decimal::add(std::string x, std::string y)
*/
char
*
sx
=
const_cast
<
char
*>
(
x
.
c_str
());
char
*
sy
=
const_cast
<
char
*>
(
y
.
c_str
());
const
size_
t
len
=
x
.
size
();
const
in
t
len
=
x
.
size
();
auto
a
=
std
::
make_unique
<
int
[]
>
(
len
);
auto
b
=
std
::
make_unique
<
int
[]
>
(
len
);
...
...
@@ -54,7 +54,7 @@ std::string Decimal::add(std::string x, std::string y)
}
/** 计算 */
const
size_
t
size
=
len
+
1
;
const
in
t
size
=
len
+
1
;
auto
c
=
std
::
make_unique
<
int
[]
>
(
size
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
...
...
@@ -100,7 +100,7 @@ std::string Decimal::substract(std::string x, std::string y)
{
if
(
x
.
size
()
>
y
.
size
())
{
auto
len
=
x
.
size
()
-
y
.
size
();
int
len
=
x
.
size
()
-
y
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
y
.
insert
(
0
,
"0"
);
...
...
@@ -108,7 +108,7 @@ std::string Decimal::substract(std::string x, std::string y)
}
else
{
auto
len
=
y
.
size
()
-
x
.
size
();
int
len
=
y
.
size
()
-
x
.
size
();
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
x
.
insert
(
0
,
"0"
);
...
...
@@ -137,7 +137,7 @@ std::string Decimal::substract(std::string x, std::string y)
*/
char
*
sx
=
const_cast
<
char
*>
(
s1
.
c_str
());
char
*
sy
=
const_cast
<
char
*>
(
s2
.
c_str
());
const
size_
t
len
=
s1
.
size
();
const
in
t
len
=
s1
.
size
();
auto
a
=
std
::
make_unique
<
int
[]
>
(
len
);
auto
b
=
std
::
make_unique
<
int
[]
>
(
len
);
...
...
@@ -152,7 +152,7 @@ std::string Decimal::substract(std::string x, std::string y)
}
/** 计算 */
const
size_
t
size
=
len
;
const
in
t
size
=
len
;
auto
c
=
std
::
make_unique
<
int
[]
>
(
size
);
for
(
int
i
=
0
;
i
<
len
;
i
++
)
{
...
...
@@ -197,8 +197,8 @@ std::string Decimal::multiply(std::string x, std::string y)
*/
char
*
sx
=
const_cast
<
char
*>
(
x
.
c_str
());
char
*
sy
=
const_cast
<
char
*>
(
y
.
c_str
());
const
size_
t
la
=
x
.
size
()
+
1
;
const
size_
t
lb
=
y
.
size
()
+
1
;
const
in
t
la
=
x
.
size
()
+
1
;
const
in
t
lb
=
y
.
size
()
+
1
;
auto
a
=
std
::
make_unique
<
int
[]
>
(
la
);
auto
b
=
std
::
make_unique
<
int
[]
>
(
lb
);
...
...
@@ -213,7 +213,7 @@ std::string Decimal::multiply(std::string x, std::string y)
}
/** 计算 */
const
size_
t
lc
=
x
.
size
()
+
y
.
size
()
+
1
;
const
in
t
lc
=
x
.
size
()
+
y
.
size
()
+
1
;
auto
c
=
std
::
make_unique
<
int
[]
>
(
lc
);
for
(
int
i
=
1
;
i
<
la
;
i
++
)
{
...
...
@@ -254,6 +254,12 @@ std::string Decimal::divide(std::string x, std::string y)
return
"1"
;
}
// 除数为 1 , 直接返回被除数
if
(
0
==
y
.
compare
(
"1"
))
{
return
x
;
}
std
::
string
str
=
"0"
;
std
::
string
remainder
;
// 被除数只比除数多一位, 则使用减法
...
...
@@ -271,22 +277,30 @@ std::string Decimal::divide(std::string x, std::string y)
{
// 被除数比除数多两位或以上, 先用加法再用减法
// 余数
str
=
"1"
;
str
=
"0"
;
std
::
string
times
=
"1"
;
std
::
string
num
=
y
;
while
(
compare
(
num
,
x
)
<=
0
)
std
::
string
t
=
x
;
while
(
t
.
size
()
-
y
.
size
()
>=
2
)
{
remainder
=
num
;
num
=
add
(
num
,
num
);
if
(
compare
(
num
,
x
)
>
0
)
while
(
compare
(
num
,
t
)
<=
0
)
{
break
;
remainder
=
num
;
num
=
add
(
num
,
num
);
if
(
compare
(
num
,
t
)
>=
0
)
{
num
=
y
;
str
=
add
(
str
,
times
);
times
=
"1"
;
t
=
substract
(
t
,
remainder
);
break
;
}
times
=
multiply
(
times
,
"2"
);
}
str
=
multiply
(
str
,
"2"
);
}
if
(
compare
(
num
,
x
)
>
0
)
remainder
=
t
;
if
(
compare
(
remainder
,
y
)
>=
0
)
{
remainder
=
substract
(
x
,
remainder
);
while
(
compare
(
remainder
,
y
)
>=
0
)
{
remainder
=
substract
(
remainder
,
y
);
...
...
@@ -298,7 +312,7 @@ std::string Decimal::divide(std::string x, std::string y)
return
str
;
}
const
int
Decimal
::
compare
(
std
::
string
x
,
std
::
string
y
)
int
Decimal
::
compare
(
std
::
string
x
,
std
::
string
y
)
{
if
(
x
.
size
()
<
y
.
size
())
{
...
...
calc_lib/src/main/main.cpp
浏览文件 @
1e3efe22
...
...
@@ -9,7 +9,7 @@ int main()
// {
// std::cout << decimal->divide("531518", "123") << std::endl;
std
::
cout
<<
decimal
->
divide
(
"
111"
,
"9
"
)
<<
std
::
endl
;
std
::
cout
<<
decimal
->
divide
(
"
222"
,
"2
"
)
<<
std
::
endl
;
std
::
cout
<<
"==================="
<<
std
::
endl
;
// }
return
0
;
...
...
calc_lib/test/testmain.cpp
浏览文件 @
1e3efe22
...
...
@@ -50,7 +50,7 @@ TEST_CASE("test_divide")
// std::cout << decimal->divide("10", "5") << std::endl;
// std::cout << decimal->divide("531518", "123") << std::endl;
std
::
cout
<<
decimal
->
divide
(
"
111"
,
"9
"
)
<<
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录