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

feat(calc_lib): 大数减法, 只实现了整数

上级 ac67953d
......@@ -9,7 +9,9 @@ KHL_CALC_NAMESPACE_BEGIN
class DllExport Decimal
{
public:
std::string add(std::string num1, std::string num2);
std::string add(std::string x, std::string y);
std::string substract(std::string x, std::string y);
};
KHL_CALC_NAMESPACE_END
#endif // _KHL_CALC_DECIMAL_H_
\ No newline at end of file
......@@ -5,29 +5,29 @@
KHL_CALC_NAMESPACE_BEGIN
std::string Decimal::add(std::string num1, std::string num2)
std::string Decimal::add(std::string x, std::string y)
{
/**
* 对齐数字
* x = 123 , y = 4567
* x = 0123 , y = 4567
*/
if (num1.size() != num2.size())
if (x.size() != y.size())
{
if (num1.size() > num2.size())
if (x.size() > y.size())
{
auto len = num1.size() - num2.size();
auto len = x.size() - y.size();
for (int i = 0; i < len; i++)
{
num2.insert(0, "0");
y.insert(0, "0");
}
}
else
{
auto len = num2.size() - num1.size();
auto len = y.size() - x.size();
for (int i = 0; i < len; i++)
{
num1.insert(0, "0");
x.insert(0, "0");
}
}
}
......@@ -37,9 +37,9 @@ std::string Decimal::add(std::string num1, std::string num2)
* x = 0123 , y = 4567
* x = 3210 , y = 7654
*/
char *sx = const_cast<char *>(num1.c_str());
char *sy = const_cast<char *>(num2.c_str());
const size_t len = num1.size();
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);
......@@ -71,11 +71,109 @@ std::string Decimal::add(std::string num1, std::string num2)
/** 翻转计算结果 */
std::string str;
int i = size - 1;
if(0 == c[i])
if (0 == c[i])
{
i--;
}
while( i >= 0 )
while (i >= 0)
{
str.push_back(c[i] + '0');
i--;
}
return str;
}
std::string Decimal::substract(std::string x, std::string y)
{
// 结果是否为负数
bool isNegativeNumber = false;
/**
* 对齐数字
* 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");
}
}
}
/**
* 数字排序
* s1 为较大数字
* s2 为较小数字
*/
std::string s1 = x;
std::string s2 = y;
if (s1.compare(s2) < 0)
{
s1 = y;
s2 = x;
isNegativeNumber = true;
}
/**
* 对齐后的字符串转换为倒置的 int 数组
* x = 0123 , y = 4567
* x = 3210 , y = 7654
*/
char *sx = const_cast<char *>(s1.c_str());
char *sy = const_cast<char *>(s2.c_str());
const size_t len = s1.size();
auto a = std::make_unique<int[]>(len);
auto b = std::make_unique<int[]>(len);
for (int i = 0; i < len; i++)
{
a[len - 1 - i] = sx[i] - '0';
}
for (int i = 0; i < len; i++)
{
b[len - 1 - i] = sy[i] - '0';
}
/** 计算 */
const size_t size = len;
auto c = std::make_unique<int[]>(size);
for (int i = 0; i < len; i++)
{
// 借位处理
if (a[i] < b[i])
{
a[i + 1]--;
a[i] += 10;
}
c[i] = a[i] - b[i];
}
/** 翻转计算结果 */
std::string str;
int i = len - 1;
while(0 == c[i] && i > 1)
{
i--;
}
if( isNegativeNumber){
str.push_back('-');
}
while(i >= 0 )
{
str.push_back(c[i] + '0');
i--;
......
......@@ -19,10 +19,18 @@ TEST_CASE("test_add")
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_substract")
{
spdlog::info("test substract");
auto decimal = std::make_unique<khl::calc::Decimal>();
std::cout << decimal->substract("0", "0") << std::endl;
std::cout << decimal->substract("0", "1") << std::endl;
std::cout << decimal->substract("1", "0") << std::endl;
std::cout << decimal->substract("123", "123") << std::endl;
std::cout << decimal->substract("123", "4567") << std::endl;
std::cout << decimal->substract("4567", "123") << std::endl;
std::cout << decimal->substract("684651316546", "46513516846165") << std::endl;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册