提交 86bb4a05 编写于 作者: 小代码2016's avatar 小代码2016

feat(calc_lib): 基本的大数加法, 只实现了整数

上级 0fae45f8
{
"files.associations": {
"xstring": "cpp"
}
}
\ No newline at end of file
...@@ -6,6 +6,8 @@ project(khl_calc VERSION 1.0.0 LANGUAGES CXX) ...@@ -6,6 +6,8 @@ project(khl_calc VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
# set(CMAKE_CONFIGURATION_TYPES Debug)
# 设置 cmake 脚本目录 # 设置 cmake 脚本目录
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
# 通用函数 # 通用函数
......
#ifndef _KHL_CALC_DECIMAL_H_ #ifndef _KHL_CALC_DECIMAL_H_
#define _KHL_CALC_DECIMAL_H_ #define _KHL_CALC_DECIMAL_H_
#include <string>
#include "khl_calc_common.h" #include "khl_calc_common.h"
KHL_CALC_NAMESPACE_BEGIN KHL_CALC_NAMESPACE_BEGIN
class DllExport Decimal class DllExport Decimal
{ {
public: public:
int add(int x, int y); std::string add(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
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
#include "khl_calc/khl_calc_decimal.h" #include "khl_calc/khl_calc_decimal.h"
KHL_CALC_NAMESPACE_BEGIN 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 KHL_CALC_NAMESPACE_END
#include <iostream> #include <iostream>
#include "khl_calc/khl_calc.h"
int main() int main()
{ {
std::cout << "hello khl_calc" << std::endl; 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; return 0;
} }
\ No newline at end of file
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <string>
#include <memory> #include <memory>
#include <cstring>
#include <sstream>
#include "doctest/doctest.h" #include "doctest/doctest.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
...@@ -11,6 +14,14 @@ TEST_CASE("test_001") ...@@ -11,6 +14,14 @@ TEST_CASE("test_001")
{ {
spdlog::info("test 001"); spdlog::info("test 001");
auto decimal = std::make_unique<khl::calc::Decimal>(); 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.
先完成此消息的编辑!
想要评论请 注册