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

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

上级 4e67b632
...@@ -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
...@@ -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
...@@ -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
...@@ -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.
先完成此消息的编辑!
想要评论请 注册