C++ 重载运算符-,运算符<;,和操作员>;
因此,我对运算符重载非常陌生,无法获得运算符-、运算符>、运算符C++ 重载运算符-,运算符<;,和操作员>;,c++,list,stl,operator-overloading,biginteger,C++,List,Stl,Operator Overloading,Biginteger,因此,我对运算符重载非常陌生,无法获得运算符-、运算符>、运算符0) 结果。编号。向前推(进位); 返回结果; } void BigInt::显示(ostream&out) { for(list::iterator it=number.begin();it!=number.end();it++) { 不能输入; 对于(int i=0;irit1) { count2++; rit1++; rit2++; } 其他的 { rit1++; rit2++; } } 如果(计数1(BigInt操作数) {
#include <iostream>
#include <string>
#include "BigInt.h"
using namespace std;
BigInt BigInt::operator+(BigInt operand)
{
int carry = 0;
int sum;
BigInt result;
list<int>::reverse_iterator rit1 = number.rbegin();
list<int>::reverse_iterator rit2 = operand.number.rbegin();
while ( (rit1 != number.rend()) || (rit2 != operand.number.rend()) )
{
sum = 0;
if (rit1 != number.rend())
{
sum += *rit1;
rit1++;
}
if (rit2 != operand.number.rend())
{
sum += *rit2;
rit2++;
}
sum += carry;
result.number.push_front(sum % 10);
carry = sum / 10;
}
if (carry > 0)
result.number.push_front(carry);
return result;
}
void BigInt::display(ostream & out)
{
for (list<int>::iterator it = number.begin(); it != number.end(); it++)
{
cout << *it;
}
cout << endl;
}
void BigInt::read(istream & in)
{
string input;
cin >> input;
for (int i = 0; i < input.length(); ++i)
{
int x = input.at(i);
int y = char(x) - char('0');
number.push_back(y);
}
}
bool BigInt::operator<(BigInt operand)
{
list<int>::reverse_iterator rit1 = number.rbegin();
list<int>::reverse_iterator rit2 = operand.number.rbegin();
int count1 = 0, count2 = 0;
bool check;
while ( (rit1 != number.rend()) || (rit2 != operand.number.rend()) )
{
if (rit1 > rit2)
{
count1++;
rit1++;
rit2++;
}
else if (rit2 > rit1)
{
count2++;
rit1++;
rit2++;
}
else
{
rit1++;
rit2++;
}
}
if(count1 < count2)
{
check = true;
}
else
{
check = false;
}
return check;
}
bool BigInt::operator>(BigInt operand)
{
list<int>::reverse_iterator rit1 = number.rbegin();
list<int>::reverse_iterator rit2 = operand.number.rbegin();
int count1 = 0, count2 = 0;
while ( (rit1 != number.rend()) || (rit2 != operand.number.rend()) )
{
if (rit1 > rit2)
{
count1++;
rit1++;
rit2++;
}
else if (rit2 > rit1)
{
count2++;
rit1++;
rit2++;
}
else
{
rit1++;
rit2++;
}
}
if( count1 > count2)
{
return true;
}
else
{
return false;
}
}
bool BigInt::operator==(BigInt operand)
{
list<int>::reverse_iterator rit1 = number.rbegin();
list<int>::reverse_iterator rit2 = operand.number.rbegin();
do
{
rit1++;
rit2++;
}
while(rit1 == rit2);
return false;
}
BigInt BigInt::operator-(BigInt operand)
{
int carry = 0;
int sum;
BigInt result;
list<int>::reverse_iterator rit1 = number.rbegin();
list<int>::reverse_iterator rit2 = operand.number.rbegin();
while ( (rit1 != number.rend()) || (rit2 != operand.number.rend()) )
{
sum = 0;
if (rit1 != number.rend())
{
sum += *rit1;
rit1++;
}
if (rit2 != operand.number.rend())
{
sum += *rit2;
rit2++;
}
sum += carry;
result.number.push_front(sum % 10);
carry = sum / 10;
}
if (carry > 0)
result.number.push_front(carry);
return result;
}
#包括
#包括
#包括“BigInt.h”
使用名称空间std;
BigInt BigInt::运算符+(BigInt操作数)
{
整数进位=0;
整数和;
BigInt结果;
list::reverse_迭代器rit1=number.rbegin();
list::reverse_迭代器rit2=operand.number.rbegin();
while((rit1!=number.rend())| |(rit2!=operand.number.rend())
{
总和=0;
if(rit1!=number.rend())
{
sum+=*rit1;
rit1++;
}
if(rit2!=操作数.number.rend())
{
总和+=*rit2;
rit2++;
}
总和+=进位;
结果。数字。向前推(总和%10);
进位=总和/10;
}
如果(进位>0)
结果。编号。向前推(进位);
返回结果;
}
void BigInt::显示(ostream&out)
{
for(list::iterator it=number.begin();it!=number.end();it++)
{
不能输入;
对于(int i=0;irit1)
{
count2++;
rit1++;
rit2++;
}
其他的
{
rit1++;
rit2++;
}
}
如果(计数1<计数2)
{
检查=正确;
}
其他的
{
检查=错误;
}
退货检查;
}
布尔BigInt::运算符>(BigInt操作数)
{
list::reverse_迭代器rit1=number.rbegin();
list::reverse_迭代器rit2=operand.number.rbegin();
int count1=0,count2=0;
while((rit1!=number.rend())| |(rit2!=operand.number.rend())
{
如果(rit1>rit2)
{
count1++;
rit1++;
rit2++;
}
else if(rit2>rit1)
{
count2++;
rit1++;
rit2++;
}
其他的
{
rit1++;
rit2++;
}
}
如果(count1>count2)
{
返回true;
}
其他的
{
返回false;
}
}
bool BigInt::operator==(BigInt操作数)
{
list::reverse_迭代器rit1=number.rbegin();
list::reverse_迭代器rit2=operand.number.rbegin();
做
{
rit1++;
rit2++;
}
而(rit1==rit2);
返回false;
}
BigInt BigInt::运算符-(BigInt操作数)
{
整数进位=0;
整数和;
BigInt结果;
list::reverse_迭代器rit1=number.rbegin();
list::reverse_迭代器rit2=operand.number.rbegin();
while((rit1!=number.rend())| |(rit2!=operand.number.rend())
{
总和=0;
if(rit1!=number.rend())
{
sum+=*rit1;
rit1++;
}
if(rit2!=操作数.number.rend())
{
总和+=*rit2;
rit2++;
}
总和+=进位;
结果。数字。向前推(总和%10);
进位=总和/10;
}
如果(进位>0)
结果。编号。向前推(进位);
返回结果;
}
BigInt.h
#include <list>
using namespace std;
class BigInt
{
public:
BigInt() {};
void read(istream & in);
void display(ostream & out);
BigInt operator+(BigInt operand); //overloading operator
BigInt operator-(BigInt operand); //subtraction
bool operator<(BigInt operand);
bool operator>(BigInt operand);
bool operator==(BigInt operand);
list<int> number; //using STL list
};
#包括
使用名称空间std;
类BigInt
{
公众:
BigInt(){};
无效读取(istream&in);
无效显示(ostream&out);
BigInt运算符+(BigInt操作数);//重载运算符
BigInt运算符-(BigInt操作数);//减法
布尔运算符(BigInt操作数);
布尔运算符==(BigInt操作数);
列表编号;//使用STL列表
};
我的main.cpp中还没有任何内容
希望你能帮助我!谢谢
编辑!!!:
我忘记列出运算符<函数的错误:
Error 10 error C2676: binary '<' : 'std::_List_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 4 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 8 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 3 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 5 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 2 error C2784: 'bool std::operator <(const std::list<_Ty,_Alloc> &,const std::list<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::list<_Ty,_Alloc> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 6 error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 9 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 7 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<_Mylist>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 1014
Error 10 Error C2676:binary'if(rit1>rit2)
应该是if(*rit1>*rit2)
,因为您想比较迭代器的内容,而不是迭代器本身。不清楚这里有什么问题。如果您知道如何实现操作符+
,那么实现操作符-
的困难是什么?我首先将中的|
更改为&
。如果这和我想的一样,那么这个逻辑运算符是不正确的。我不知道如何修改运算符-。从运算符+到运算符-,有很少的修改吗?@WhozCraig,这是我教授给我的代码的一部分。我想这是对的。你的问题解决了我最大的错误。非常感谢。