biginteger判断相等_BigInteger类型的解析_超详细解析

1 /*98765432109876542345223452 2147483647234534523234523453 21474836472345234523234523454 1817609114327449623452345235 2345234523434656346345634563测试数据*/

6 #include

7 #include

8 #include

9 #include

10 #include

11 #include

12 #include

13 #include

14 #include

16 using namespacestd;17

18 structBigInteger {19 static const int BASE = 100000000; //静态成员变量---属于BigInteger这个类型的,不属于他的结构体变量

20 static const int WIDTH = 8;21 vector s; //储存大整数

22

23 BigInteger(long long num = 0) { //构造函数(带默认值)

24 *this =num;25 }26 BigInteger operator = (long long num) {//面对不超过long long的整数

27 s.clear(); //清空本对象的vector容器内容--以免有上一次数据影响28 //将整数里面的数八位八位的 储存到 vector里面去

29 do{30 s.push_back(num % BASE); //假如整数是9位--得到了后面的八位

31 num /= BASE; //继续得到前面的位数

32 } while (num > 0);33 return *this; //返回本对象

34 }35 //long long类型不够的情况

36 BigInteger operator = (const string& str) { //赋值运算符

37 s.clear();38 int x, len = (str.length() - 1) / WIDTH + 1; //有几个八位一组(多出来的不足8位舍去)39 //从右向左8位8位的储存整数到vector中

40 for (int i = 0; i < len; i++) { //按一共有几组八位储存

41 int end = str.length() - i*WIDTH; //i个八位一组--字符串长度 - 几个八位 = 前面剩余的位数42 //取max(0,前面位数 - 8个长度),如果该剩余位数不为0,就取大的

43 int start = max(0,end-WIDTH);44 //成为查找一组数的开始位置,如果取的是end-WIDTH,,那么end-start = WIDTH得到了一个八位,当做查找字符串的长度45 //sscanf()是将字符以某种形式取出存放到相应的类型中(这里是int),substr(start--开始搜寻的位置(包括),46 //end-start----是代表搜寻的长度),c_str()返回一个指针指向字符串相当于一个'\0'结尾的字符数组->用来终止一次47 //8位的取出到x

48 sscanf(str.substr(start,end-start).c_str(), "%d", &x);49 //要注意在最高位前面的几组8位中,如果他们的第八位为0,那么组成整数的时候就不会有八位,输出时要注意

50 s.push_back(x); //将取出的整数push到vector的底部---大数是从右往左的一点点储存进去

51 }52 return *this;53 }54 //重载 + 运算符

55 BigInteger operator + (const BigInteger& b) const

56 {57 BigInteger c;58 c.s.clear(); //清空c类型中vector内的元素

59 for (unsigned int i = 0, g = 0; ; i++)60 {61 if (g == 0 && i >= s.size() && i >= b.s.size())//如果位数已经到了最高位,i大于本对象对象且大于b对象的大小

62 break; //跳出循环

63 int x =g;64 if (i < s.size()) x +=s[i];65 if (i < b.s.size()) x +=b.s[i];66 c.s.push_back(x % BASE); //八位八位的取出取出后(从右向左)

67 g = x/BASE; //得到最高位向后的位数(一直到从右向左出现的第一次8位前面停止)

68 }69 returnc;70 }71

72 BigInteger operator += (const BigInteger &b)73 {74 *this = *this +b;75 return *this;76 }77

78

79 //比较运算符

80 bool operator < (const BigInteger &b) const

81 {82 if (s.size() !=b.s.size())83 return s.size() = 0; i--) //从后往前面比较,因为(低位在vector的前面)注:这样做的前提是两个

85 { //都没有前导0,否则不能比较

86 if (s[i] !=b.s[i])87 return s[i]

90 }91 bool operator > (const BigInteger &b) const

92 {93 return b < *this;94 }95 bool operator <= (const BigInteger &b) const

96 {97 return !(b < *this); //相当于返回 本对象 <= b对象

98 }99 bool operator >= (const BigInteger &b) const

100 {101 return !(*this

104 {105 return b < *this || *this

108 {109 return !(b < *this) || !(*this

113 ostream& operator << (ostream &out, const BigInteger &x)114 {115 out << x.s.back(); //从右向左输出(因为最高位是最后储存进去的)116 //八位八位的写

117 for (int i = x.s.size()-2; i >= 0; i--) { //从倒数第二位开始输出

118 char buf[20];119 sprintf(buf, "%08d", x.s[i]); //将整数当成字符写到buf中输出,不足八位的补零(因为在前面的几组八位中120 //如果不足八位说明在形成整数的过程中最高位为0)

121 for (unsigned int j = 0; j < strlen(buf); j++) //输出八位

122 out <

127 istream& operator >> (istream &in, BigInteger &x) {128 strings;129 if (! (in >>s))130 return in;131 x = s; //输入正确的流

132 return in;133 }134

135 structcmp {136 bool operator () (const BigInteger &a, const BigInteger &b) const { //升序比较

137 return a

141 structcmpL{142 bool operator () (const BigInteger &a, const BigInteger &b) const { //降序比较

143 return a >b;144 }145 };146 int main(void)147 {148 BigInteger bi, sum1, mus, b2;149

150 sets1;151

152 vectors2;153

154 maps3;155

156 pairs4;157

158 stringss;159 int i = 1;160

161 /*while (cin >> b2) //利用了重载 >> 提取运算符162 {163 mus += b2;164 }165 cout << mus << endl;*/

166

167 while (cin >>ss)168 {169 bi =ss;170 sum1 +=bi;171 s1.insert(bi);172 s2.push_back(bi);173 s4.first =bi;174 s4.second = 'a'+i; //用来检验map里面元素也是可以自动排序的175 i++;176 s3.insert(s4);177

178 }179

180 for (set::iterator it = s1.begin(); it != s1.end(); ++it) //set升序

181 cout << *it <

183 cout <

185 sort(s2.begin(),s2.end(),cmpL()); //sort 降序

186 for (vector::iterator it = s2.begin(); it != s2.end(); ++it)187 cout << *it <

189 for (map::iterator it = s3.begin(); it != s3.end(); ++it) //map降序

190 cout << (*it).second <

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值