重载运算符

简介: 重载运算符

当我们多次使用高精时,我们就会把它写成函数。或许你会疑问,我们难道不能向像int a=3,b=4,c; c=a+b;一样进行运算吗? 答案是肯定的。 接下来我们将介绍如何利用重载运算符进行高精计算。

例8.2  运用重载的例子:

给出n和m,求出1!+2!+3!+...+n!是否大于m.大于m的话输出”yes”,否则输出”no”.其中0<n<101,m<10^500.

【输入格式】

只有一行,包括一个n和一个m 【输出格式】

如果1!+2!+3!+...+n!大于m,则输出”yes”,否则输出”no”

【输入样例】

33 8954945705218228090637347680100940313

【输出样例】

no


1. #include<cstdio>
2. #include<cstring>
3. #include<iostream>
4. #include<cmath>
5. using namespace std;
6. const int MAXN=4000;//定义高精度数的长度 
7. struct BIGNUM{
8.  int len,s[MAXN];
9.  BIGNUM(){//创建构造函数(Constructor) 
10.     memset(s,0,sizeof(s));//初始化数组s 
11.     len=1;//初始化长度len
12.   } 
13.   //将字符串数字赋值给BIGNUM的变量 
14.   BIGNUM operator=(const char*num){// operator是关键字 
15.     len=strlen(num);
16.     for(int i=0;i<len;++i) s[i]=num[len-i-1]-'0';//num逆序赋值给s 
17.     return *this;
18.   }
19.   BIGNUM(const int num){*this=num; }
20.   //将较小的非字符串数字赋值给BIGNUM的变量 
21.   BIGNUM operator=(const int num){
22.     char a[MAXN];
23.     sprintf(a,"%d",num);
24.     *this=a;
25.     return *this;
26.   }
27.   BIGNUM(const char*num){*this=num;}
28.   //"+"的重载 
29.   BIGNUM operator+(const BIGNUM&a){
30.     BIGNUM c;
31.     c.len=max(len,a.len)+1;//默认两个数字相加有进位 
32.     for(int i=0,x=0;i<c.len;++i){
33.       c.s[i]=s[i]+a.s[i]+x;
34.       x=c.s[i]/10;
35.       c.s[i]=c.s[i]%10;
36.     }
37.     if(c.s[c.len-1]==0)--c.len;//如果没有进位长度就减一 
38.     return c;
39.   }
40.   BIGNUM operator+=(const BIGNUM&a){
41.     *this=*this+a;
42.     return *this;
43.   }
44.   //"*"的重载 
45.   BIGNUM operator*(const BIGNUM&x){
46.     BIGNUM c;
47.     c.len=len+x.len;
48.     for(int i=0;i<len;++i){
49.       for(int j=0;j<x.len;++j){
50.         c.s[i+j]+=s[i]*x.s[j];
51.         c.s[i+j+1]+=c.s[i+j]/10;
52.         c.s[i+j]%=10;
53.       } 
54.     }
55.     if(c.s[c.len-1]==0)--c.len;
56.     return c;
57.   }
58.   BIGNUM operator*=(const BIGNUM&a){
59.     *this=*this*a;
60.     return *this;
61.   }
62.   //先重载下 "<" 然后利用 "<"重载出其他关系运算符 
63.   bool operator<(const BIGNUM&x)const{
64.     if(len!=x.len) return len<x.len;//注意不能有前导零
65.     for(int i=len-1;i>=0;--i){
66.       if(s[i]!=x.s[i]) return s[i]<x.s[i];
67.     }
68.     return false;//全部都相等,说明小于不成立
69.   }
70.   //重载其他的关系运算符
71.   bool operator >(const BIGNUM&x)const{return x<*this;}
72.   bool operator <=(const BIGNUM&x)const{return !(x<*this);}
73.   bool operator >=(const BIGNUM&x)const{return !(*this<x);}
74.   bool operator ==(const BIGNUM&x)const{return !(x<*this||*this<x);}
75.   bool operator !=(const BIGNUM&x)const{return x<*this||*this<x;}
76. };
77. //使用重载"<<"运算符进行输出
78. ostream& operator<<(ostream &out,const BIGNUM& x){ 
79.   for(int i=x.len-1;i>=0;--i) cout<<x.s[i];
80.   return out;
81. }
82. //使用重载">>"运算符进行输入 
83. istream& operator>>(istream &in,BIGNUM &x){
84.   char num[MAXN];
85.   in>>num;
86.   x=num;
87.   return in;
88. }
89. BIGNUM f[5001];
90. int main()
91. {
92.   int n;
93.   BIGNUM m,sum=0,num=1;
94.   cin>>n>>m;
95.   for(int i=1;i<=n;++i){
96.     num*=i;
97.     sum+=num;
98.   }
99.   if(sum>m) cout<<"yes"<<endl;
100.  else cout<<"no";
101.  return 0;
102. }

 

相关文章
|
9月前
重载运算符
重载运算符
50 0
|
9月前
|
编译器 C++
c++重载函数和重载运算符
c++重载函数和重载运算符
43 0
|
9月前
|
C++ 开发者
43运算符重载函数作为类成员函数和友元函数
43运算符重载函数作为类成员函数和友元函数
39 0
|
8月前
|
安全 程序员 C++
C++一分钟之-重载运算符
【6月更文挑战第21天】C++的运算符重载让程序员能为自定义类型定制运算符行为,增强代码表达力。但要注意清晰性、优先级和返回类型。遵循运算符原有意义,充分测试,并用注释解释非直观设计。示例展示了如何为复数类重载`+`运算符。避免重载内置类型,注意结合性,且慎用隐式转换。重载应提升可读性而非复杂化代码。
62 2
|
9月前
|
C++ 编译器
|
9月前
|
程序员 编译器 C++
c++重载运算符和重载函数
c++重载运算符和重载函数
47 1
|
7月前
|
编译器 C++
【C++】详解运算符重载,赋值运算符重载,++运算符重载
【C++】详解运算符重载,赋值运算符重载,++运算符重载
|
9月前
|
程序员 C++
C++程序中的运算符重载
C++程序中的运算符重载
55 2
|
9月前
|
存储 编译器 C++
【C++类和对象】拷贝构造与赋值运算符重载(上)
【C++类和对象】拷贝构造与赋值运算符重载
|
9月前
|
编译器 C++ 索引
【C++类和对象】拷贝构造与赋值运算符重载(下)
【C++类和对象】拷贝构造与赋值运算符重载