Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小代码2016
calc
提交
86bb4a05
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,发现更多精彩内容 >>
提交
86bb4a05
编写于
2月 09, 2023
作者:
小代码2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(calc_lib): 基本的大数加法, 只实现了整数
上级
0fae45f8
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
96 addition
and
4 deletion
+96
-4
calc_lib/.vscode/settings.json
calc_lib/.vscode/settings.json
+5
-0
calc_lib/CMakeLists.txt
calc_lib/CMakeLists.txt
+2
-0
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
+68
-2
calc_lib/src/main/main.cpp
calc_lib/src/main/main.cpp
+6
-0
calc_lib/test/testmain.cpp
calc_lib/test/testmain.cpp
+12
-1
未找到文件。
calc_lib/.vscode/settings.json
0 → 100644
浏览文件 @
86bb4a05
{
"files.associations"
:
{
"xstring"
:
"cpp"
}
}
\ No newline at end of file
calc_lib/CMakeLists.txt
浏览文件 @
86bb4a05
...
...
@@ -6,6 +6,8 @@ project(khl_calc VERSION 1.0.0 LANGUAGES CXX)
set
(
CMAKE_CXX_STANDARD 14
)
set
(
CMAKE_CXX_STANDARD_REQUIRED True
)
# set(CMAKE_CONFIGURATION_TYPES Debug)
# 设置 cmake 脚本目录
set
(
CMAKE_MODULE_PATH
${
CMAKE_SOURCE_DIR
}
/cmake
)
# 通用函数
...
...
calc_lib/include/khl_calc/khl_calc_decimal.h
浏览文件 @
86bb4a05
#ifndef _KHL_CALC_DECIMAL_H_
#define _KHL_CALC_DECIMAL_H_
#include <string>
#include "khl_calc_common.h"
KHL_CALC_NAMESPACE_BEGIN
class
DllExport
Decimal
{
public:
int
add
(
int
x
,
int
y
);
std
::
string
add
(
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
浏览文件 @
86bb4a05
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
#include "khl_calc/khl_calc_decimal.h"
KHL_CALC_NAMESPACE_BEGIN
int
Decimal
::
add
(
int
x
,
int
y
)
std
::
string
Decimal
::
add
(
std
::
string
x
,
std
::
string
y
)
{
return
x
+
y
;
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
];
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
];
for
(
int
i
=
0
;
i
<
lc
;
i
++
)
{
result
[
i
]
=
a
[
i
]
+
b
[
i
];
result
[
i
+
1
]
=
result
[
i
]
/
10
;
result
[
i
]
=
result
[
i
]
%
10
;
}
size_t
len
=
lc
;
if
(
0
==
result
[
lc
]
&&
lc
>
0
)
{
len
--
;
}
std
::
cout
<<
"---"
<<
std
::
endl
;
char
*
c
=
new
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
;
}
KHL_CALC_NAMESPACE_END
calc_lib/src/main/main.cpp
浏览文件 @
86bb4a05
#include <iostream>
#include "khl_calc/khl_calc.h"
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
;
return
0
;
}
\ No newline at end of file
calc_lib/test/testmain.cpp
浏览文件 @
86bb4a05
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <string>
#include <memory>
#include <cstring>
#include <sstream>
#include "doctest/doctest.h"
#include "spdlog/spdlog.h"
...
...
@@ -11,6 +14,14 @@ TEST_CASE("test_001")
{
spdlog
::
info
(
"test 001"
);
auto
decimal
=
std
::
make_unique
<
khl
::
calc
::
Decimal
>
();
CHECK
(
3
==
decimal
->
add
(
1
,
2
));
std
::
string
str
=
decimal
->
add
(
"84964651354684615354684"
,
"498461351384864651684"
);
std
::
cout
<<
"result: "
<<
str
<<
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录