被学校oj的高精度恶心到了,所以我觉得以后把它当板子
- //??CBigInteger?????
- /********** Write your code here! **********/
- #include<bits/stdc++.h>
- using namespace std;
- class CBigInteger
- {
- public:
- vector<int> a;
- int si;
- CBigInteger()
- {
- si=1;
- }
- CBigInteger(vector<int> t,int s)
- {
- si=s;
- a=t;
- }
- CBigInteger(int x)
- {
- a.clear();
- si=x;
- }
- void sets(int x)
- {
- si=x;
- }
- CBigInteger add(CBigInteger x,CBigInteger y,int s)
- {
- int n=max(x.a.size(),y.a.size());
- vector<int> p(n);
- for(int i=0; i<n; i++)
- {
- if(i<x.a.size()) p[i]+=x.a[i];
- if(i<y.a.size()) p[i]+=y.a[i];
- }
- for(int i=0; i<n-1; i++)
- {
- p[i+1]+=p[i]/10;
- p[i]%=10;
- }
- if(p.size()==0) return CBigInteger(p,s);
- while(p[p.size()-1]>9)
- {
- p.push_back(p[p.size()-1]/10);
- p[p.size()-2]%=10;
- }
- return CBigInteger(p,s);
- }
- CBigInteger minus(CBigInteger x,CBigInteger y,int s)
- {
- int n=max(x.a.size(),y.a.size());
- vector<int> p(n);
- for(int i=0; i<n; i++)
- {
- if(i<x.a.size()) p[i]+=x.a[i];
- if(i<y.a.size()) p[i]-=y.a[i];
- }
- for(int i=0; i<n-1; i++)
- {
- if(p[i]<0)
- {
- p[i+1]--;
- p[i]+=10;
- }
- }
- if(n==0) return CBigInteger(p,s);
- if(p[n-1]<0)
- {
- return minus(y,x,-s);
- // vector<int> q(n);
- // q[n-1]=-p[n-1];
- // p.pop_back();
- // return minus(CBigInteger(q,1),CBigInteger(p,1),-s);
- }
- return CBigInteger(p,s);
- }
- CBigInteger operator + (CBigInteger t)
- {
- if(si==1)
- {
- if(t.si==1) return add(*this,t,1);
- else return minus(*this,t,1);
- }
- else
- {
- if(t.si==-1) return add(*this,t,-1);
- else return minus(*this,t,-1);
- }
- }
- CBigInteger operator - (CBigInteger t)
- {
- if(si==1)
- {
- if(t.si==1) return minus(*this,t,1);
- else return add(*this,t,1);
- }
- else
- {
- if(t.si==-1) return minus(*this,t,-1);
- else return add(*this,t,-1);
- }
- }
- CBigInteger mul(CBigInteger x,int y,int num)
- {
-
- vector<int> p;
- for(int i=1; i<=num; i++) p.push_back(0);
- for(int i=0; i<x.a.size(); i++)
- {
- p.push_back(x.a[i]*y);
- }
- return CBigInteger(p,1);
- }
- CBigInteger operator * (CBigInteger t)
- {
- CBigInteger p(si*t.si);
- for(int i=0; i<t.a.size(); i++)
- {
- p=p+mul(*this,t.a[i],i);
- }
- p.si=si*t.si;
- return p;
- }
- friend istream& operator >> (istream &in,CBigInteger &c);
- friend ostream& operator << (ostream &out,const CBigInteger &c);
- };
- istream& operator >> (istream &in,CBigInteger &c)
- {
- string s;
- cin>>s;
- int now=0;
- if(s[now]=='-')
- {
- c.sets(-1);
- now++;
- }
- else c.sets(1);
- c.a.clear();
- while(now<s.size())
- {
- c.a.push_back(s[now]-'0');
- now++;
- }
- reverse(c.a.begin(),c.a.end());
- while((!c.a.empty())&&c.a.back()==0) c.a.pop_back();
- return in;
- }
- ostream& operator << (ostream &out,const CBigInteger &c)
- {
- bool bz=0;
- vector<int> p=c.a;
- while((!p.empty())&&p.back()==0) p.pop_back();
- if(p.size()==0)
- {
- cout<<0;
- return out;
- }
- if(c.si==-1) bz=1;
- if(bz) cout<<"(-";
-
- for(int i=p.size()-1; i>=0; i--) cout<<p[i];
- if(bz) cout<<")";
- return out;
- }
- /*******************************************/
- int main()
- {
- int t;
- char op;
- CBigInteger bigNum1;
- CBigInteger bigNum2;
-
- cin >> t;
- while(t--)
- {
- cin >> bigNum1 >> op >> bigNum2;
- cout << bigNum1 << " " << op << " " << bigNum2 << " = ";
- if(op == '+')
- cout << bigNum1 + bigNum2 << endl;
- else if(op == '-')
- cout << bigNum1 - bigNum2 << endl;
- else if(op == '*')
- cout << bigNum1 * bigNum2 << endl;
- }
- return 0;
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |