博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
大实数的输入与比较
阅读量:6247 次
发布时间:2019-06-22

本文共 3942 字,大约阅读时间需要 13 分钟。

需要重载输入,输出,构造函数,以及比较操作符

用两个双端队列分别存储整数与小数部分数据,输入输出过程均转成字符串。

Bignumber实现代码

using size_t =unsigned long;private:    const int POWER = 4;//压四位存储    std::string str_num,str_float_num;    std::deque
num; std::deque
float_num; bool mark = 0;public: Bignumber()=default; Bignumber(std::string& item){ this->to_Bignumber(item); } Bignumber(double item){ char *ch; std::string buf; //std::cout<
<
<
to_Bignumber(buf); } ~Bignumber() = default; std::string to_string(){ std::string ret; if(mark)ret.push_back('-'); char ch[5]; sprintf(ch,"%d",num.front()); ret += ch; for(int i = num.size();i > 1;--i){ sprintf(ch,"%04d",num[i-1]); ret += ch; } ret.push_back('.'); sprintf(ch,"%d",float_num.front()); ret+=ch; for(int i = 2;i <= float_num.size();++i){ sprintf(ch,"%04d",float_num[i-1]); ret += ch; } return ret; } void to_Bignumber (std::string &st) { if(st[0] == '-') { mark = 1; st.erase(0,1);//处理负数 } int dot_pos = st.find_first_of('.');//处理小数点 str_num = st.substr(0,dot_pos); str_float_num = st.substr(dot_pos+1,st.length() - 1 - dot_pos); int len = (str_num.length() - 1) / POWER + 1; for(int i = 0;i < len; ++i) { int x = 0; int end = str_num.length() - i * POWER; int start = 0 > (end - POWER)?0:(end - POWER); sscanf(str_num.substr(start, end-start).c_str(),"%d", &x); num.push_front(x); } len = (str_float_num.length() - 1) /POWER + 1; for(int i = 0;i < len; ++i){ int x = 0; int end = str_float_num.length() - i * POWER; int start = 0 > (end - POWER)?0:(end - POWER); sscanf(str_float_num.substr(start,end-start).c_str(), "%d", &x); float_num.push_front(x); } } bool operator <(const Bignumber &other) {//重载小于号 if(this == &other) return false;//如果地址相同直接返回假 if(num.size()
other.num.size())return false; auto i = num.end(); auto j = other.num.end(); --i;--j; for(;i>=num.begin();i--,j--){ if(*i > *j)return false; } i = float_num.end(); j = other.float_num.end(); --i;--j; for(;i>=float_num.begin() && j>=other.float_num.begin();i--,j--){ if(*i > *j)return false; } return true; } bool operator ==(const Bignumber &other){//重载等于号 if(this == &other) return true; if(mark != other.mark)return false; if(!(num.size() == other.num.size())) return false; if(!(float_num.size() == other.float_num.size()))return false; for(int i = num.size() -1;i >= 0;++i){ if(!(num[i] == other.num[i])) return false; } for(int i = float_num.size()-1;i>=0;++i){ if(!(float_num[i] == other.float_num[i]))return false; } return true; } bool operator <=(const Bignumber &other){ if(this == &other) return true; return (*this < other || *this == other); } bool operator >(const Bignumber &other){ if(this == &other) return false; return !(*this <= other); } bool operator >=(const Bignumber &other){ if(this == &other) return true; return *this > other || *this==other; } bool operator !=(const Bignumber &other){ if(this == &other) return false; return !(*this == other); } Bignumber &operator =(const Bignumber &other){ if(this == &other) return *this; this->mark = other.mark; this->float_num = other.float_num; this->num = other.num; this->str_num = other.str_num; return *this; } Bignumber &operator =(const double &item){ char *ch; std::string buf; std::sprintf(ch, "%.10lf",item); buf = ch; to_Bignumber(buf); return *this; }std::ostream& operator <<(std::ostream &fout, Bignumber &item){ fout<
>(std::istream &fin, Bignumber &item){ std::string temp; fin>>temp;//输入 item.to_Bignumber(temp); return fin; } 复制代码

转载地址:http://cjmia.baihongyu.com/

你可能感兴趣的文章
Configuring Apache Kafka for Performance and Resource Management
查看>>
excel 截取单元格部分内容(从指定位置截取)
查看>>
Email-ext plugin
查看>>
绝对干货,教你4分钟插入1000万条数据到mysql数据库表,快快进来
查看>>
文件系统
查看>>
Android模拟器Genymotion安装apk
查看>>
chrome使用技巧(看了定不让你失望)
查看>>
数据字典
查看>>
Laravel Model 的 fillable (白名单)与 guarded (黑名单)
查看>>
idea激活
查看>>
Presto 性能优化点
查看>>
Key Lookup开销过大导致聚集索引扫描
查看>>
CSS 中的字体兼容写法:用CSS为英文和中文字体分别设置不同的字体
查看>>
Java全栈程序员之04:Ubuntu下安装MySQL、注册服务及Navcat
查看>>
读吴恩达算-EM算法笔记
查看>>
Bug是一种财富-------研发同学的错题集、测试同学的遗漏用例集
查看>>
Spring1:Spring简介、环境搭建、源码下载及导入MyEclipse
查看>>
服务测试碰钉子Server GC
查看>>
go关键字之select
查看>>
国内医保控费公司简单比较
查看>>