重载运算符
来源:互联网 发布:c语言界面设计 编辑:程序博客网 时间:2024/05/17 22:26
重载运算符
–运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
此处有高精度+ - * / % > < >= <= ==
不要在意头文件。。
本篇的高精度自带正负
用动态数组存储
不用考虑位数的问题
只要空间不超限就行
#include<bits/stdc++.h>using namespace std;struct BigInt{ bool symbol; vector<int>num; BigInt(string sNum = "0"){*this=sNum;} BigInt(int iNum){*this = iNum;} BigInt operator=(const int iNum) { num.erase(num.begin(), num.end()); int tmp(iNum); if(iNum < 0)symbol=true,tmp=-tmp; else symbol=false; while(tmp >= 10){num.push_back(tmp % 10);tmp /= 10;} if(tmp!=0)num.push_back(tmp); else if(num.empty())num.push_back(0); return *this; } BigInt operator = (const string sNum) { num.erase(num.begin(), num.end()); for(string::size_type i(sNum.size() - 1); i > 0; --i)num.push_back(sNum[i] - '0'); if(sNum[0] == '-')symbol=true; else{symbol=false;num.push_back(sNum[0] - '0');} for(vector<int>::size_type i(num.size() - 1); num[i] == 0 && i > 0; --i) num.pop_back(); return *this; } BigInt operator = (const BigInt bInt) { num.erase(num.begin(), num.end()); for(vector<int>::size_type i(0); i < bInt.num.size(); ++i)num.push_back(bInt.num[i]); symbol=bInt.symbol; return *this; } BigInt operator+(const BigInt bInt)const { BigInt rslt; if(bInt.symbol == (*this).symbol) if(bInt.symbol)rslt.symbol=true; else if((*this).symbol){rslt=bInt-(-*this);rslt.symbol=true;return rslt;} else if(bInt.symbol){rslt = *this - (-bInt);return rslt;} vector<int>::size_type lenA(num.size()), lenB(bInt.num.size()), lng; lng = (lenA > lenB ? lenA : lenB); for(vector<int>::size_type i(0); i < lng; ++i) { int tmp(rslt.num[rslt.num.size() - 1]); if(i<lenA)tmp+=num[i]; if(i<lenB)tmp+=bInt.num[i]; rslt.num[rslt.num.size() - 1] = tmp % 10; rslt.num.push_back(tmp / 10); } if(rslt.num[lng]==0)rslt.num.pop_back(); return rslt; } BigInt operator - (BigInt bInt) const { BigInt rslt, myInt(*this), tmp; vector<int>::size_type lenA(num.size()), lenB(bInt.num.size()), lng, shrt, len; shrt=(lenA > lenB ? lenB : lenA); lng=(lenA > lenB ? lenA : lenB); if(bInt.symbol == myInt.symbol) { if(!bInt.symbol) { if(myInt < bInt) { tmp = myInt; myInt = bInt; bInt = tmp; rslt.symbol = true; } } else { if(myInt > bInt) { tmp = myInt; myInt = bInt; bInt = tmp; rslt.symbol = false; } myInt=-myInt;bInt=-bInt; rslt.symbol = true; } } else if(myInt.symbol) return -(-myInt + bInt); else if(bInt.symbol) return myInt + (-bInt); for(vector<int>::size_type i(0); i < lng; ++i) { if(i < shrt) { if(myInt.num[i] < bInt.num[i]) { myInt.num[i + 1] -= 1; rslt.num[i] = myInt.num[i] + 10 - bInt.num[i]; } else rslt.num[i] = myInt.num[i] - bInt.num[i]; } else rslt.num[i] = myInt.num[i]; rslt.num.push_back(0); } for(vector<int>::size_type i(rslt.num.size() - 1); rslt.num[i] == 0 && i > 0; --i) rslt.num.pop_back(); return rslt; } BigInt operator - () const { BigInt rslt(*this); if(rslt != 0) rslt.symbol = !rslt.symbol; return rslt; } BigInt operator * (const BigInt bInt) const { BigInt rslt; vector<int>::size_type lenA(num.size()), lenB(bInt.num.size()), len(lenA + lenB); rslt.num.insert(rslt.num.begin(), len - 1, 0); for(vector<int>::size_type i(0); i < lenA; ++i) for(vector<int>::size_type j(0); j < lenB; ++j) { rslt.num[i + j] += num[i] * bInt.num[j]; rslt.num[i + j + 1] += rslt.num[i + j] / 10; rslt.num[i + j] %= 10; } if(rslt.num[len - 1] == 0) rslt.num.pop_back(); if(rslt != 0) rslt.symbol = symbol ^ bInt.symbol; return rslt; } BigInt operator / (const BigInt bInt) const { BigInt tmp, tmp2; string rsltS((*this).num.size(), '0'); if(bInt == 0) return BigInt(0); vector<int>::size_type i(num.size()); string::size_type j(0); while(i--) { if(tmp == 0) tmp.num.pop_back(); tmp.num.insert(tmp.num.begin(), num[i]); int i(10); while(i--) { tmp2 = tmp - (bInt < 0 ? -bInt : bInt) * i; if(tmp2 >= 0) { rsltS[j] += i; tmp = tmp2; break; } } ++j; } BigInt rslt(rsltS); if(rslt != 0) rslt.symbol = symbol ^ bInt.symbol; return rslt; } BigInt operator % (const BigInt bInt) const { BigInt rslt, tmp; if(*this == 0) return BigInt(0); vector<int>::size_type i(num.size()); while(i--) { if(rslt == 0) rslt.num.pop_back(); rslt.num.insert(rslt.num.begin(), num[i]); int i(10); while(i--) { tmp = rslt - (bInt < 0 ? -bInt : bInt) * i; if(tmp >= 0) { rslt = tmp; break; } } } rslt.symbol = symbol; return rslt; } BigInt operator += (const BigInt bInt) { *this = *this + bInt; return *this; } BigInt operator -= (const BigInt bInt) { *this = *this - bInt; return *this; } BigInt operator *= (const BigInt bInt) { *this = *this * bInt; return *this; } BigInt operator /= (const BigInt bInt) { *this = *this / bInt; return *this; } BigInt operator %= (const BigInt bInt) { *this = *this % bInt; return *this; } bool operator < (const BigInt bInt) const { vector<int>::size_type bLen(bInt.num.size()); if(!bInt.symbol && symbol) return true; else if(bInt.symbol && !symbol) return false; else if(num.size() != bLen) { if(!symbol) return num.size() < bLen; else return num.size() > bLen; } for(vector<int>::size_type i(bLen - 1); i > 0; --i) { if(num[i] != bInt.num[i]) { if(!bInt.symbol && !(*this).symbol) return num[i] < bInt.num[i]; else return bInt.num[i] < num[i]; } } if(!bInt.symbol && !symbol) return num[0] < bInt.num[0]; else return bInt.num[0] < num[0]; } bool operator>(const BigInt bInt)const{return bInt<*this;} bool operator <= (const BigInt bInt)const{return !(*this > bInt);} bool operator >= (const BigInt bInt)const{return !(*this < bInt);} bool operator != (const BigInt bInt)const{return (*this<bInt)||(*this>bInt);} bool operator == (const BigInt bInt)const{return !(*this != bInt);}}; istream& operator >> (istream &in, BigInt& x){ string tmp; in >> tmp; x = tmp; return in;} ostream& operator << (ostream &out, BigInt& x){ if(x.symbol)cout<<'-'; for(vector<int>::size_type i(x.num.size() - 1); i > 0; --i)out<<x.num[i]; out<<x.num[0]; return out;}
0 0
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- C#运算符重载
- C++-运算符重载
- 重载赋值运算符
- 运算符重载
- 有关运算符重载
- mysql--查看版本
- Effective Java系列读后感(三)-类和接口
- C++ 文件操作
- eclipse 常用快捷键整理
- WiFi、ZigBee、BLE用哪个?小米内部是这样选的
- 重载运算符
- 堆栈是什么意思?
- memcached-2-理解memcached的内存存储
- 字符串判断空
- 自定义字符分割函数
- less应用
- 谢谢我的兄弟们
- 快速沃尔什变换(FWT)讲解 解决集合卷积的方法
- 图论-最小生成树算法