高精度重载运算符

高精度重载运算符模板

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];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    string str() const {
        string res = "";
        for (int i = 0; i < len; i++)
            res = (char) (s[i] + ‘0‘) + res;
            if (res == "") res = "0";
            return res;
    }
    void clear(){ while(len > 1 && !s[len - 1]) len --;}
    bign operator = (const char *num) {
        len = strlen(num);
        for (int i = 0; i < len; i++)
            s[i] = num[len - i - 1] - ‘0‘;
            return *this;
    }
        //四则运算的重载
    bign operator + (const bign &b) const {
        bign c = *this;
        int i = 0;
        for (int i = 0; i < b.len; i++) {
            c.s[i] += b.s[i];
            if (c.s[i] > 9) c.s[i] %= 10, c.s[i + 1] ++;
        }
        while (c.s[i] > 9) c.s[i++] %= 10, c.s[i]++;
        c.len = max(len, b.len);
        if (c.s[i] && c.len <= i) c.len = i + 1;
        return c;
    }
    bign operator - (const bign &b) const {
        bign c = *this;
        int i = 0;
        for (int i = 0; i < b.len; ++i) {
            c.s[i] -= b.s[i];
            if (c.s[i] < 0) c.s[i] += 10, c.s[i + 1] --;
        }
        while (c.s[i] < 0) c.s[i++] += 10, c.s[i] --;
        c.clear();
        return c;
    }
    bign operator * (const bign &b) const {
        int i, j;
        bign c;
        c.len = len + b.len;
        for (int j = 0; j < b.len; j++)
            for (int i = 0; i < len; i++)
                c.s[i + j] += s[i] * b.s[j];
        for (int i = 0; i < c.len - 1; i++){
            c.s[i + 1] += c.s[i] / 10;
            c.s[i] %= 10;
        }
        c.clear();
        return c;
    }
    bign operator / (const bign &b) const {
        bign c = *this, a = 0;
        int i, j;
        for (i = len - 1; i >= 0; i --) {
            a = a * 10 + s[i];
            for (j = 0; j < 10; j++)
                if (a < b * (j + 1)) break;
            c.s[i] = j;
            a = a - b * j;
        }
        c.clear();
        return c;
    }
    bign operator % (const bign &b) const {
        int i, j;
        bign c = 0;
        for (int i = len - 1; i >= 0; i--) {
            c = c * 10 + s[i];
            for (int i = len - 1; i >= 0; --i){
                if (c < b * (j + 1)) break;
                c = c - b * j;
            }
        }
        return c;
    }
    bign operator += (const bign &b){
        *this = *this + b;
        return *this;
    }
    bool operator < (const bign &b) const{
        if (len != b.len) return len < b.len;
        for (int i = len - 1; i >= 0; i--)
            if (s[i] != b.s[i]) return s[i] < b.s[i];
        return false;
    }
        //比较符重载
    bool operator > (const bign &b) const {return b < *this;}
    bool operator <= (const bign &b) const {return !(b < *this);}
    bool operator >= (const bign &b) const {return !(*this < b);}
    bool operator == (const bign &b) const {return !(b < *this) && !(*this < b);}
    bool operator != (const bign &b) const {return (b < *this) || (*this < b);}
};
        //流输入输出重载
istream& operator >> (istream &in, bign &x) {
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream& operator << (ostream &out, bign &x){
    out << x.str();
    return out;
}

原文地址:https://www.cnblogs.com/sun915/p/9501509.html

时间: 2024-10-05 05:50:12

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

[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;} //高位清

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和运算符组

C++学习27 用全局函数重载运算符

运算符重载函数既可以声明为类的成员函数,也可以声明为所有类之外的全局函数. 运算符重载函数作为类的成员函数 将运算符重载函数声明为类的成员函数时,二元运算符的参数只有一个,一元运算符不需要参数.之所以少一个参数,是因为这个参数是隐含的. 例如,上节的 complex 类中重载了加法运算符: complex operator+(const complex & A)const; 当执行: c3 = c1 + c2; 会被转换为: c3 = c1.operator+(c2); 通过 this 指针隐式