Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
25280910
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 搜索 >>
提交
25280910
编写于
2月 12, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc_lib): 大数除法, 只实现了整数
上级
4e67b632
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
91 addition
and
8 deletion
+91
-8
calc_lib/include/khl_calc/khl_calc_decimal.h
calc_lib/include/khl_calc/khl_calc_decimal.h
+10
-0
calc_lib/src/khl_calc/khl_calc_decimal.cpp
calc_lib/src/khl_calc/khl_calc_decimal.cpp
+63
-3
calc_lib/src/main/main.cpp
calc_lib/src/main/main.cpp
+6
-4
calc_lib/test/testmain.cpp
calc_lib/test/testmain.cpp
+12
-1
未找到文件。
calc_lib/include/khl_calc/khl_calc_decimal.h
浏览文件 @
25280910
...
@@ -14,6 +14,16 @@ public:
...
@@ -14,6 +14,16 @@ public:
std
::
string
substract
(
std
::
string
x
,
std
::
string
y
);
std
::
string
substract
(
std
::
string
x
,
std
::
string
y
);
std
::
string
multiply
(
std
::
string
x
,
std
::
string
y
);
std
::
string
multiply
(
std
::
string
x
,
std
::
string
y
);
std
::
string
divide
(
std
::
string
x
,
std
::
string
y
);
const
int
compare
(
std
::
string
x
,
std
::
string
y
);
private:
/**
* 比较两个数字的大小
*/
// const int compare(std::string x, std::string y);
};
};
KHL_CALC_NAMESPACE_END
KHL_CALC_NAMESPACE_END
#endif // _KHL_CALC_DECIMAL_H_
#endif // _KHL_CALC_DECIMAL_H_
\ No newline at end of file
calc_lib/src/khl_calc/khl_calc_decimal.cpp
浏览文件 @
25280910
...
@@ -204,12 +204,12 @@ std::string Decimal::multiply(std::string x, std::string y)
...
@@ -204,12 +204,12 @@ std::string Decimal::multiply(std::string x, std::string y)
for
(
int
i
=
1
;
i
<
la
;
i
++
)
for
(
int
i
=
1
;
i
<
la
;
i
++
)
{
{
a
[
la
-
i
]
=
sx
[
i
-
1
]
-
'0'
;
a
[
la
-
i
]
=
sx
[
i
-
1
]
-
'0'
;
}
}
for
(
int
i
=
1
;
i
<
lb
;
i
++
)
for
(
int
i
=
1
;
i
<
lb
;
i
++
)
{
{
b
[
lb
-
i
]
=
sy
[
i
-
1
]
-
'0'
;
b
[
lb
-
i
]
=
sy
[
i
-
1
]
-
'0'
;
}
}
/** 计算 */
/** 计算 */
...
@@ -228,7 +228,7 @@ std::string Decimal::multiply(std::string x, std::string y)
...
@@ -228,7 +228,7 @@ std::string Decimal::multiply(std::string x, std::string y)
/** 翻转计算结果 */
/** 翻转计算结果 */
std
::
string
str
;
std
::
string
str
;
int
i
=
lc
-
1
;
int
i
=
lc
-
1
;
while
(
0
==
c
[
i
]
&&
i
>
1
)
while
(
0
==
c
[
i
]
&&
i
>
1
)
{
{
i
--
;
i
--
;
}
}
...
@@ -241,4 +241,64 @@ std::string Decimal::multiply(std::string x, std::string y)
...
@@ -241,4 +241,64 @@ std::string Decimal::multiply(std::string x, std::string y)
return
str
;
return
str
;
}
}
std
::
string
Decimal
::
divide
(
std
::
string
x
,
std
::
string
y
)
{
if
(
0
==
x
.
compare
(
"0"
)
||
compare
(
x
,
y
)
<
0
)
{
return
"0"
;
}
if
(
0
==
compare
(
x
,
y
)
)
{
return
"1"
;
}
/**
* 字符串转换为倒置的 int 数组
* x = 123 , y = 4567
* x = 321 , y = 7654
*/
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
;
auto
a
=
std
::
make_unique
<
int
[]
>
(
la
);
auto
b
=
std
::
make_unique
<
int
[]
>
(
lb
);
for
(
int
i
=
1
;
i
<
la
;
i
++
)
{
a
[
la
-
i
]
=
sx
[
i
-
1
]
-
'0'
;
}
for
(
int
i
=
1
;
i
<
lb
;
i
++
)
{
b
[
lb
-
i
]
=
sy
[
i
-
1
]
-
'0'
;
}
std
::
string
str
=
"0"
;
// 余数
std
::
string
remainder
=
x
;
while
(
compare
(
remainder
,
y
)
>=
0
)
{
remainder
=
substract
(
remainder
,
y
);
str
=
add
(
str
,
"1"
);
}
return
str
;
}
const
int
Decimal
::
compare
(
std
::
string
x
,
std
::
string
y
)
{
if
(
x
.
size
()
<
y
.
size
())
{
return
-
1
;
}
if
(
x
.
size
()
>
y
.
size
())
{
return
1
;
}
return
x
.
compare
(
y
);
}
KHL_CALC_NAMESPACE_END
KHL_CALC_NAMESPACE_END
calc_lib/src/main/main.cpp
浏览文件 @
25280910
...
@@ -5,10 +5,12 @@
...
@@ -5,10 +5,12 @@
int
main
()
int
main
()
{
{
auto
decimal
=
std
::
make_unique
<
khl
::
calc
::
Decimal
>
();
auto
decimal
=
std
::
make_unique
<
khl
::
calc
::
Decimal
>
();
for
(
int
i
=
0
;
i
<
10
;
i
++
)
// for (int i = 0; i < 10; i++)
{
// {
std
::
cout
<<
decimal
->
add
(
"465465465464"
,
"1321654876135138468135135"
)
<<
std
::
endl
;
// std::cout << decimal->divide("531518", "123") << std::endl;
std
::
cout
<<
decimal
->
divide
(
"9999"
,
"2"
)
<<
std
::
endl
;
std
::
cout
<<
"==================="
<<
std
::
endl
;
std
::
cout
<<
"==================="
<<
std
::
endl
;
}
//
}
return
0
;
return
0
;
}
}
\ No newline at end of file
calc_lib/test/testmain.cpp
浏览文件 @
25280910
...
@@ -41,8 +41,19 @@ TEST_CASE("test_multiply")
...
@@ -41,8 +41,19 @@ TEST_CASE("test_multiply")
auto
decimal
=
std
::
make_unique
<
khl
::
calc
::
Decimal
>
();
auto
decimal
=
std
::
make_unique
<
khl
::
calc
::
Decimal
>
();
std
::
cout
<<
decimal
->
multiply
(
"9133"
,
"0"
)
<<
std
::
endl
;
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
(
"9999"
,
"2"
)
<<
std
::
endl
;
std
::
cout
<<
decimal
->
divide
(
"99999"
,
"2"
)
<<
std
::
endl
;
std
::
cout
<<
decimal
->
divide
(
"999999"
,
"2"
)
<<
std
::
endl
;
std
::
cout
<<
decimal
->
divide
(
"9999999"
,
"2"
)
<<
std
::
endl
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录