高精度运算类bign
来源:互联网 发布:花生壳教学软件 编辑:程序博客网 时间:2024/05/17 08:43
本例选自<算法竞赛入门经典>。
#include<cstdio>#include<iostream>using namespace std;class bign{private:enum{maxn = 200};int len, s[maxn];public:bign();bign(int num);bign(const char * str);bign& operator =(int num);bign& operator =(const char * str);string str() const;bign operator +(const bign & b) const;bign operator -(const bign & b);bign& operator +=(const bign & b);bign& operator -=(const bign & b);void clean();bign operator *(const bign & b);bool operator <(const bign & b) const;bool operator <=(const bign & b) const;bool operator >(const bign & b) const;bool operator >=(const bign & b) const;bool operator ==(const bign & b) const;bool operator !=(const bign & b) const;};bign::bign(){memset(s,0,sizeof(s));len = 0;}bign::bign(int num){*this = num;}bign::bign(const char * str){*this = str;}bign& bign::operator=(int num){bign();char s[maxn];sprintf(s,"%d",num);*this = s;return *this;}bign& bign::operator=(const char *str){bign();len = strlen(str);for(int i = 0; i < len; ++i)s[i] = str[len -i - 1] - '0';return *this;}string bign::str() const{string res = "";for(int i = 0; i < len; ++i)res = (char)(s[i] + '0') + res;if(res == "")res = "0";return res;}bign bign::operator +(const bign & b) const{bign c;for(int i = 0, g = 0; g || i < max(len,b.len); ++i ){int x = g;if(i < len) x += s[i];if(i < b.len) x += b.s[i];g = x / 10;c.s[c.len++] = x % 10;}return c;}bign bign::operator -(const bign & b){if(*this < b){cout << "the result is nagive/n";return bign();}bign c;c.len = 0;for(int i = 0, g = 0; i < len; ++i){int x = s[i] - g;if(i < b.len)x -= b.s[i];if(x < 0){x += 10;g = 1;}elseg = 0;c.s[c.len++] = x;}c.clean();return c;}bign& bign::operator +=(const bign & b){*this = *this + b;return *this;}bign& bign::operator -=(const bign & b){*this = *this - b;return *this;}void bign::clean(){while(len > 0 && s[len - 1] == 0) --len;}bign bign::operator *(const bign & b){bign c;c.len = len + b.len;for(int i = 0; i < len; ++i)for(int j = 0; j < b.len; ++j)c.s[i+j] = s[i] * b.s[j];for(int k = 0; k < c.len; ++k){c.s[k+1] += c.s[k] / 10;c.s[k] %= 10;}c.clean();return c;}bool bign::operator <(const bign & b) const{if(len != b.len)return len < b.len;for(int i = 0; i < len; ++i)if(s[i] != b.s[i])return s[i] < b.s[i];return false;}bool bign::operator <=(const bign & b) const{return !(*this > b); }bool bign::operator >(const bign & b) const{if(len != b.len)return len > b.len;for(int i = 0; i < len; ++i)if(s[i] != b.s[i])return s[i] > b.s[i];return false;}bool bign::operator >=(const bign & b) const{return !(*this < b);}bool bign::operator ==(const bign & b) const{return !(b < *this) && !(*this < b);}bool bign::operator !=(const bign & b) const{return (b < *this) || (*this < b);}istream & operator >> (istream & in, bign & x){string res;in >> res;x = res.c_str();return in;}ostream & operator << (ostream & out, const bign & x){out << x.str();return out;}
- 高精度运算类bign
- 高精度运算类 bign
- 高精度运算类bign
- 高精度运算类bign
- 高精度运算类bign
- 高精度运算类bign
- bign类-高精度运算类
- AOAPC_I 高精度运算类 bign
- c++ 高精度运算类 bign
- 关于高精度运算bign类的实现
- 高精度模板 bign类
- 高精度计算bign类
- Bign类 高精度模版
- 高精度bign类
- 高精度运算类bign(非负整数)模板
- 高精度运算类bign(支持前导零输入)
- C++高精度运算类bign (重载操作符)
- C++高精度运算类bign (重载操作符)
- 【转载内容】产品经理专业术语-BRD PRD等
- Java多线程之线程池(四)
- 国标645协议.....2007---项目所需四(应用层)
- Linux系统虚拟机安装VMware Tools
- ID CodesFrom:UVa, 146
- 高精度运算类bign
- 阅读文献的三大问题:坐不住,记不住,想不开
- android客户端从服务器端获取json数据并解析
- 一个非计算机专业的的毕业生在互联网行业的辉煌的求职之路,值得学习
- Java多线程之信号量(一)
- 编程之美二 : 中国象棋将帅问题
- 从12306说起,如何构建高并发高性能网站
- 黑马程序员 java-test
- 安装 VMware Tools 详解