高精度之重载运算符

  • 这篇文章就纯属自娱自乐啦,受时间以及技术原因的限制,这里面的重载,减法支持的功能仅限于减一次(因为没有同是负号的判断),除法仅限于高精除单精(高精除高精的一个个的减或者二分实在是不想写了)……
  • 结构体片段以及输出操作如下:
struct bignum
{
     int len,s[maxn];
     char flag;
     bignum()
     {
            len=1;
            flag=‘+‘;
            memset(s,0,sizeof(s));
     }
     bignum (int num)
     {
            *this=num;
     }
     bignum (const char *num)
     {
            *this=num;
     }
     bignum operator = (const char *a)
     {
            len=strlen(a);
            for (int i=1;i<=len;++i)
              s[i]=a[len-i]-‘0‘;
            return *this;
     }
     bignum operator = (const int num)
     {
            char a[maxn];
            sprintf(a,"%d",num);
            *this=a;
            return *this;
     }
     bignum operator + (const bignum &a)
     {
            bignum c;
            c.len=max(len,a.len)+1;
            for (int i=1;i<c.len;++i)
            {
                c.s[i]+=(s[i]+a.s[i]);
                c.s[i+1]+=c.s[i]/10;
                c.s[i]%=10;
            }
            if (c.s[c.len]==0)
              c.len--;
            return c;
     }
     bignum operator += (const bignum &a)
     {
            *this=*this+a;
            return *this;
     }
     bignum operator * (const bignum &a)
     {
            bignum c;
            c.len+=(len+a.len);
            for (int i=1;i<=len;++i)
              for (int j=1;j<=a.len;++j)
              {
                  c.s[i+j-1]+=(s[i]*a.s[j]);
                  c.s[i+j]+=(c.s[i+j-1]/10);
                  c.s[i+j-1]%=10;
              }
            while (c.s[c.len]==0)
               c.len--;
            return c;
     }
     bignum operator *= (const bignum &a)
     {
            *this=(*this) * a;
            return *this;
     }
     bool operator < (const bignum &a) const
     {
          if (len!=a.len)
            return len<a.len;
          for (int i=len;i>=1;--i)
            if (s[i]!=a.s[i])
              return s[i]<a.s[i];
          return false;
     }
     bool operator > (const bignum &a) const
     {
          return a<*this;
     }
     bool operator <= (const bignum &a) const
     {
          return !(*this>a);
     }
     bool operator >= (const bignum &a) const
     {
          return !(*this<a);
     }
     bool operator == (const bignum &a) const
     {
          return !((*this<a) || (*this>a));
     }
     bool operator != (const bignum &a) const
     {
          return !(*this==a);
     }
     void change (bignum &a,bignum &b)
     {
            bignum tmp=a;
            a=b;
            b=tmp;
     }
     bignum operator - (const bignum &a) const
     {
             bignum b=*this,c;
             if (b<a)
             {
                 c.flag=‘-‘;
                 c.len=a.len;
                 for (int i=1;i<=c.len;++i)
                 {
                     c.s[i]+=(a.s[i]-b.s[i]);
                     if (c.s[i]<0)
                     {
                         c.s[i]+=10;
                         c.s[i+1]-=1;
                     }
                 }
                 while (c.len==0)
                    c.len--;
                 return c;
             }
             c.len=b.len;
             for (int i=1;i<=c.len;++i)
             {
                 c.s[i]+=(b.s[i]-a.s[i]);
                 if (c.s[i]<0)
                 {
                      c.s[i]+=10;
                      c.s[i+1]-=1;
                 }
             }
             while (c.len==0)
               c.len--;
             return c;
     }
     bignum operator -= (const bignum &a)
     {
            *this=(*this)-a;
            return *this;
     }
     bignum operator / (const int n)
     {
            bignum c,b=*this;
            c.len=b.len;
            int x=0;
            for (int i=1;i<=n;++i)
            {
                c.s[i]=(x*10+b.s[i])/n;
                x=(x*10+b.s[i])%n;
            }
            while (c.s[c.len]==0)
               c.len--;
            return c;
     }
     bignum operator /= (const int a)
     {
            *this=*this/a;
            return *this;
     }
};

ostream& operator << (ostream &out,const bignum &x)
{
         for (int i=x.len;i>=1;--i)
           printf("%d",x.s[i]);
         return out;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-17 23:26:44

高精度之重载运算符的相关文章

[C++]高精度 bign (重载运算符版本)

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define maxn 2000 #define base 10000 struct Bign { int c[maxn],len,sign; //初始化 Bign(){memset(c,0,sizeof(c)),len = 1,sign = 0;} //高位清

高精度重载运算符

高精度重载运算符模板 struct bign{ //存在性定义 int len, s[2010]; //初始化 bign () {memset(s, 0, sizeof(s)), len = 1;} bign (int num){ *this = num;} bign (char *num) {*this = num;} bign (const char *num){ *this = num;} //重载 = bign operator = (int num) { char s[2010]; s

C++学习之重载运算符1

C++除可重载函数之后,还允许定义已有的运算符,这样通过运算符重载可像处理数据使用它们. 先来个代码 1 #include<iostream> 2 using namespace std; 3 4 class num 5 { 6 public: 7 num(){n=1;} 8 ~num(){} 9 int get() const{return n;} 10 void set(int x){n=x;} 11 private: 12 int n; 13 }; 14 15 int main() 16

矩阵求和--重载运算符

C++习题 矩阵求和--重载运算符 [Submit][Status][Web Board] Description 有两个矩阵a和b,均为2行3列.求两个矩阵之和.重载运算符"+",使之能用于矩阵相加(如c=a+b). 重载流插入运算符"<<"和流提取运算符">>",使之能用于该矩阵的输入和输出. Input 两个2行3列矩阵 Output 矩阵之和 Sample Input 1 2 34 5 67 8 91 2 3 Sa

重载()运算符和重载强制类型转换

// 研究了半宿.最终弄清楚了 // 写了这段測试代码能够非常好的演示效果 class CConvert { public: CConvert(){m_nValue = 10;} // 重载()运算符 int operator ()(); // 重载int强制类型转换 operator int(); protected: private: int m_nValue; }; int CConvert::operator ()() { return m_nValue; } CConvert::ope

C++自学笔记_重载运算符_《C++ Primer》

#include <iostream> #include <stdexcept> using namespace std; class CheckedPtr{ public: CheckedPtr(int *b,int *e,int *c): beg(b),end(e),curr(c){ } CheckedPtr(const CheckedPtr &obj): //复制构造函数 beg(obj.beg),end(obj.end),curr(obj.curr){ } Chec

作为类的成员函数,重载运算符只能有一个参数

1 overload a operator of a class, you can only use one para., this pointer is automatically used. class Rational { public: //not correct since this ponit would be used automatically. //Rational operator+ (const Rational& lhs, const Rational& rhs);

C++习题 复数类--重载运算符+,-,*,/

Description 定义一个复数类Complex,重载运算符"+","-","*","/",使之能用于复数的加.减.乘.除.运算符重载函数作为Complex类的成员函数.编写程序,分别求两个复数之和.差.积和商. Input 两个复数 Output 两个复数之和.差.积和商 Sample Input 3 4 5 -10 Sample Output c1+c2=(8.00,-6.00i) c1-c2=(-2.00,14.00

operator重载运算符

1.重载运算符的函数一般格式如下 函数类型    operator  运算符名称    (形参表列) {对运算符的重载处理} 例如,想将“+”用于Complex(复数)的加法运算,函数的原型可以是这样的: Complex operator + (Complex & c1,Complex &c2); operator+函数表示对运算符+重载.其中,operator是关键字,专门用于定义重载运算符的函数的,运算符名称就是C++提供给用户的预定运算符. 注意:函数名是由operator和运算符组