提交 8b23cdd8 编写于 作者: 小代码2016's avatar 小代码2016

feat(calc_lib): 优化大数加法, 只实现了整数

上级 0b7e3822
{
"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
#ifndef _KHL_CALC_DECIMAL_H_
#define _KHL_CALC_DECIMAL_H_
#ifndef _KHL_CALC_DECIMAL_H_
#define _KHL_CALC_DECIMAL_H_
#include <string>
......
#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 < lx; i++)
for (int i = 0; i < len; i++)
{
a[lx - i] = sx[i] - '0';
a[len - 1 - i] = sx[i] - '0';
}
for (int i = 0; i < ly; i++)
for (int i = 0; i < len; i++)
{
b[ly - i] = sy[i] - '0';
b[len - 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 < lc; i++)
for (int i = 0; i < len; 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;
}
......
......@@ -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
......@@ -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.
先完成此消息的编辑!
想要评论请 注册