重载运算符

短命女 2022-07-11 12:05 296阅读 0赞
  1. #include<iostream>
  2. #include<string.h>
  3. #include<stdio.h>
  4. using namespace std;
  5. const int MAXN=4000;
  6. struct Bignum{
  7. int len,s[MAXN];
  8. string condition;
  9. inline int max(int a,int b){
  10. return a>b?a:b;
  11. }
  12. inline void swap(Bignum &a,Bignum &b)
  13. {
  14. Bignum t=a;
  15. a=b;
  16. b=t;
  17. }
  18. inline void BignumGet(string &s,Bignum *a,char *ch){
  19. bool flag=true;
  20. int k=0,k2=0;;
  21. int record[1001]={
  22. 0};
  23. for (auto i:s)
  24. {
  25. if (!(s[i]>='0'&&s[i]<=9)){
  26. ch[k]=s[i];
  27. k++;
  28. record[k2]=i;
  29. }
  30. }
  31. for (int i=1;i<=k2;i++)
  32. {
  33. }
  34. }
  35. Bignum()
  36. {
  37. len=1;
  38. memset(s,0,sizeof(s));
  39. }
  40. Bignum(const char *num){*this=num;}
  41. Bignum(int num){*this=num;}
  42. Bignum operator=(const char *num)
  43. {
  44. len=strlen(num);
  45. for (int i=0;i<len;i++)
  46. {
  47. s[i]=num[len-i-1]-'0';
  48. }
  49. return *this;
  50. }
  51. Bignum operator=(const string s1)
  52. {
  53. condition=s1;
  54. return *this;
  55. }
  56. Bignum operator=(const int num)//存储
  57. {
  58. char a[MAXN];
  59. sprintf(a,"%d",num);
  60. *this=a;
  61. return *this;
  62. }
  63. Bignum operator+(const Bignum &a)//重载加法
  64. {
  65. Bignum c;
  66. c.len=max(len,a.len)+1;
  67. for (int i=0,x=0;i<c.len;++i)
  68. {
  69. c.s[i]=a.s[i]+s[i]+x;
  70. x=c.s[i]/10;
  71. c.s[i]%=10;
  72. }
  73. if (c.s[c.len-1]==0)--c.len;
  74. return c;
  75. }
  76. Bignum operator+=(const Bignum &a)
  77. {
  78. return *this+a;
  79. }
  80. Bignum operator-(Bignum &num){
  81. //重载减法
  82. Bignum b=*this;
  83. Bignum a=num;
  84. bool flag=false;
  85. if (b<a){
  86. swap(b,a);
  87. flag=true;
  88. cout<<'-';
  89. }
  90. b.len=max(a.len,b.len);
  91. for (int i=0,x=0;i<b.len;i++){
  92. if (b.s[i]<a.s[i]){
  93. b.s[i]=b.s[i]+10-a.s[i];
  94. b.s[i+1]--;
  95. }else {
  96. b.s[i]=b.s[i]-a.s[i];
  97. }
  98. }
  99. while (b.s[b.len-1]==0)b.len--;
  100. return b;
  101. }
  102. Bignum operator*(const Bignum &a)//重载高精度乘法
  103. {
  104. Bignum c;
  105. c.len=len+a.len;
  106. for (int i=0;i<len;i++)
  107. {
  108. for (int j=0;j<a.len;j++)
  109. {
  110. c.s[i+j]+=a.s[j]*s[i];
  111. c.s[i+j+1]+=c.s[i+j]/10;
  112. c.s[i+j]%=10;
  113. }
  114. }
  115. while(c.s[c.len-1]==0)c.len--;
  116. return c;
  117. }
  118. Bignum operator*(const int a)//乘法
  119. {
  120. Bignum c=a;
  121. return *this*c;
  122. }
  123. Bignum operator*=(const Bignum &a)
  124. {
  125. *this=*this*a;
  126. return *this;
  127. }
  128. bool operator <(const Bignum&x)const{
  129. //重载<号
  130. if (len!=x.len) return len<x.len;
  131. for (int i=len-1;i>=0;i--)
  132. {
  133. if (s[i]!=x.s[i])return s[i]<x.s[i];
  134. }
  135. return false;
  136. }
  137. bool operator >(const Bignum&x)const{
  138. return x<*this;}//重载>号
  139. bool operator <=(const Bignum&x)const{
  140. return !(x<*this);}//重载<=号
  141. bool operator >=(const Bignum&x)const{
  142. return !(*this<x);}//重载>=号
  143. bool operator ==(const Bignum&x)const{
  144. return !(x<*this||x>*this);}//重载==号
  145. bool operator !=(const Bignum&x)const{
  146. return (x<*this||x>*this);}//重载!=号
  147. };
  148. ostream &operator<<(ostream &out,const Bignum&x){
  149. //输出流
  150. if (x.condition.empty()){
  151. cout<<x.condition;
  152. string a;
  153. return out;
  154. }
  155. for (int i=x.len-1;i>=0;i--)
  156. cout<<x.s[i];
  157. return out;
  158. }
  159. istream &operator>>(istream &in,Bignum&x){
  160. //输入流
  161. char num[MAXN];
  162. in>>num;
  163. x=num;
  164. return in;
  165. }
  166. int main()
  167. {
  168. Bignum a,b;
  169. cin>>a;
  170. cout<<a*99;
  171. }

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读

    相关 运算符重载

    不能重载的运算符有:. 和 :: 和 ?:和.\和sizeof 注意点1: 使用运算符重载的本质上是一次函数调用,所以这些关于运算对象的求值顺序无法应用到重载的运算符上。

    相关 运算符重载

    一、运算符重载。 什么是运算符重载? 运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。 二、加号运算符重载。 作用: 实现两个自定

    相关 运算符重载

    实现函数运算符的实质:编写一个函数,该函数以“operator运算符号”为函数名,其定义了重载的运算符将要执行的操作。(给自己的类进行运算) 运算符重载有两种形式:重载为类的

    相关 运算符重载

    c++的一大特性就是重载(overload),通过重载可以把功能相似的几个函数合为一个,使得程序更加简洁、高效。在c++中不止函数可以重载,运算符也可以重载。由于一般数据类型间