大整数的除法在 C++ 中表示为字符串
Division of Big Integers represented like string in c++
编辑:现在下面的方法(除法函数)可以正常工作了!
我正在研究 C++ 中的大整数除法。我已经编写了加法和减法函数,但我遇到了除法问题。
这里是 Big_Integer class:
big_int.h
char toChar(int num)
class Big_Int
{
public:
Big_Int();
Big_Int(const Big_Int&);
Big_Int(string);
Big_Int& operator=(const Big_Int&);
Big_Int operator+(const Big_Int&);
friend Big_Int difference(const Big_Int&, const Big_Int&);
friend Big_Int divide(const Big_Int&, long long);
friend Big_Int operator - (const Big_Int&, const Big_Int&);
friend bool operator<(const Big_Int&, const Big_Int&);
friend bool operator>(const Big_Int&, const Big_Int&);
friend bool operator<=(const Big_Int& , const Big_Int&);
friend bool operator>=(const Big_Int& , const Big_Int&);
friend Big_Int operator/(const Big_Int&, long long);
friend bool less_than(const Big_Int&, const Big_Int&);
friend bool less_or_eq(const Big_Int&, const Big_Int&);
friend bool operator==(const Big_Int&, const Big_Int&);
friend ostream& operator<<(ostream& , const Big_Int&);
friend istream& operator>>(istream&, const Big_Int&);
private:
string number;
};
这是我的除法函数:
Big_Int divide(const Big_Int& in, long long den)
{
string w_dvt = in.number;
int carry = 0;
string dvt = 0;
string result;
while (!(w_dvt.empty()))
{
if ((w_dvt[0] == '0') && (carry == 0))
{
result += w_dvt[0];
w_dvt = w_dvt.substr(1);
}
else {
int i = 0;
for (; i < w_dvt.size(); ++i)
{
dvt += w_dvt[i];
if (stoi(dvt) >= den) break;
}
w_dvt = w_dvt.substr(i + 1);
long long i_dvt = stoi(dvt);
int res = i_dvt / den;
carry = i_dvt%den;
i_dvt = carry;
dvt = to_string(i_dvt);
result += toChar(res);
}
}
return Big_Int(result);
}
Big_Int operator/(const Big_Int& in, long long den)
{
Big_Int res = divide(in, den);
return res;
}
char toChar(int num)
{
char ch = '0'+ num ;
return ch;
}
这是主程序:
int main()
{
string num1, num2;
cin >> num1;
Big_Int i1(num1);
cin >> num2;
Big_Int i2(num2);
Big_Int divs = i1 / 8;
cout << divs << endl;
}
它编译得很好,但是当我尝试 运行 它会中止并显示一条消息:
"Expression: invalid null pointer"
我对我的程序做了一些修改以消除该问题,但它仍然中止并显示相同的消息。如果有人对我的代码的问题有任何想法,我将不胜感激。
抓挠:
string dvt = 0;
放:
string dvt /*= 0*/ ;
至少
int main()
{
Big_Int divs = Big_Int("42") / 8;
cout << divs << endl;
}
运行.
(我为 Big_Int (string s)
和 operator<<
添加了微不足道的暗示)
编辑
一点解释:指向以零结尾的空 C 字符串的 char* 是 不是 一个 - 无效 - (char*)(0);它是指向 (char)0 的有效指针。
编辑:现在下面的方法(除法函数)可以正常工作了!
我正在研究 C++ 中的大整数除法。我已经编写了加法和减法函数,但我遇到了除法问题。 这里是 Big_Integer class:
big_int.h
char toChar(int num)
class Big_Int
{
public:
Big_Int();
Big_Int(const Big_Int&);
Big_Int(string);
Big_Int& operator=(const Big_Int&);
Big_Int operator+(const Big_Int&);
friend Big_Int difference(const Big_Int&, const Big_Int&);
friend Big_Int divide(const Big_Int&, long long);
friend Big_Int operator - (const Big_Int&, const Big_Int&);
friend bool operator<(const Big_Int&, const Big_Int&);
friend bool operator>(const Big_Int&, const Big_Int&);
friend bool operator<=(const Big_Int& , const Big_Int&);
friend bool operator>=(const Big_Int& , const Big_Int&);
friend Big_Int operator/(const Big_Int&, long long);
friend bool less_than(const Big_Int&, const Big_Int&);
friend bool less_or_eq(const Big_Int&, const Big_Int&);
friend bool operator==(const Big_Int&, const Big_Int&);
friend ostream& operator<<(ostream& , const Big_Int&);
friend istream& operator>>(istream&, const Big_Int&);
private:
string number;
};
这是我的除法函数:
Big_Int divide(const Big_Int& in, long long den)
{
string w_dvt = in.number;
int carry = 0;
string dvt = 0;
string result;
while (!(w_dvt.empty()))
{
if ((w_dvt[0] == '0') && (carry == 0))
{
result += w_dvt[0];
w_dvt = w_dvt.substr(1);
}
else {
int i = 0;
for (; i < w_dvt.size(); ++i)
{
dvt += w_dvt[i];
if (stoi(dvt) >= den) break;
}
w_dvt = w_dvt.substr(i + 1);
long long i_dvt = stoi(dvt);
int res = i_dvt / den;
carry = i_dvt%den;
i_dvt = carry;
dvt = to_string(i_dvt);
result += toChar(res);
}
}
return Big_Int(result);
}
Big_Int operator/(const Big_Int& in, long long den)
{
Big_Int res = divide(in, den);
return res;
}
char toChar(int num)
{
char ch = '0'+ num ;
return ch;
}
这是主程序:
int main()
{
string num1, num2;
cin >> num1;
Big_Int i1(num1);
cin >> num2;
Big_Int i2(num2);
Big_Int divs = i1 / 8;
cout << divs << endl;
}
它编译得很好,但是当我尝试 运行 它会中止并显示一条消息:
"Expression: invalid null pointer"
我对我的程序做了一些修改以消除该问题,但它仍然中止并显示相同的消息。如果有人对我的代码的问题有任何想法,我将不胜感激。
抓挠:
string dvt = 0;
放:
string dvt /*= 0*/ ;
至少
int main()
{
Big_Int divs = Big_Int("42") / 8;
cout << divs << endl;
}
运行.
(我为 Big_Int (string s)
和 operator<<
添加了微不足道的暗示)
编辑
一点解释:指向以零结尾的空 C 字符串的 char* 是 不是 一个 - 无效 - (char*)(0);它是指向 (char)0 的有效指针。