黑白双芙 2024-07-22 17:04 采纳率: 66.7%
浏览 3
已结题

哪出了问题(高精度)?


#include<bits/stdc++.h>
#include <stdlib.h>
using namespace std;
bool TreatmentErase=false;
class immense
{
    protected:
        string LTGp,DCMp;
        int LTGn,DCMn;
        bool symbol;
        
    public:
        immense(){
            LTGp="0";//整数部分 
            DCMp="";//小数部分 
            LTGn=1,DCMn=0;//整数数位;小数数位
            symbol=true;//symbol=true->非负数 
        }//初始化
        void Obtain(void) {
            if(!symbol)cout<<'-';
            reverse(LTGp.begin(),LTGp.end());
            cout<<LTGp;
            reverse(LTGp.begin(),LTGp.end());
            if(DCMn!=0){
                cout<<'.'<<DCMp;
            }
        }//输入 
        void rationalize(void){
            bool T=true;
            for(int i=LTGp.size()-1;i>=0;i--){
                if(LTGp[i]>'9'||LTGp[i]<'0'){
                    if(TreatmentErase){
                        immense();
                        break;
                    }
                    else LTGp.erase(i,1);
                }
                if(T&&LTGp[i]=='0'&&LTGp!="0")LTGp.erase(i,1);
                else T=false;
            }
            if(LTGp=="")LTGp="0";
            T=true;
            for(int i=DCMp.size()-1;i>=1;i--){
                if(DCMp[i]>'9'||DCMp[i]<'0'){
                    if(TreatmentErase){
                        immense();
                        break;
                    }
                    else DCMp.erase(i,1);
                }
                if(T&&DCMp[i]=='0')DCMp.erase(i,1);
                else T=false;
            }
            LTGn=LTGp.size();
            DCMn=DCMp.size();
        }
        void operator=(const immense &T){//赋值(huge) 
            LTGp=T.LTGp;
            DCMp=T.DCMp;
            LTGn=T.LTGn;
            DCMn=T.DCMn;
            symbol=T.symbol;
        }
        void operator=(string T) {//赋值(string)
            if(T[0]=='-'){
                symbol=false;
                T.erase(0,1);
            }
            string rdL,rdD;
            int split;
            for(int i=0;i<T.size();i++){
                if(T[i]=='.'){
                    split=i;
                    rdL=T.substr(0,split);
                    rdD=T.substr(split+1,T.size()-split);
                    goto assignment;
                }
            }
            rdL=T;
            rdD="";
            assignment:    ;
            reverse(rdL.begin(),rdL.end());
            LTGp=rdL;
            DCMp=rdD;
            LTGn=rdL.size();
            DCMn=rdD.size();
            rationalize();
        }
        bool operator==(immense other){
            if(this->symbol==other.symbol)
                if(this->DCMp==other.DCMp)
                    if(this->LTGp==other.LTGp)
                        return true;
            return false;
        }
        bool operator>(immense other){
            if(*this==other)return true;
            if(this->symbol>other.symbol)return true;
            if(this->symbol<other.symbol)return false;
            bool x=this->symbol,y=true;
            if(this->LTGn!=other.LTGn){
                if(this->LTGn>other.LTGn)y=true;
                else y=false;
            }
            for(int i=this->LTGn-1;i>=0;i--){
                if(this->LTGp[i]>other.LTGp[i]){
                    y=true;
                    goto finish;
                }
                if(this->LTGp[i]<other.LTGp[i]){
                    y=false;
                    goto finish;
                }
            }
            for(int i=0;i<min(this->DCMn,other.DCMn);i++){
                if(this->DCMp[i]-'0'>other.DCMp[i]-'0'){
                    y=true;
                    goto finish;
                }
                if(this->DCMp[i]-'0'<other.DCMp[i]-'0'){
                    y=false;
                    goto finish;
                }
            }
            if(this->DCMn>other.DCMn)
                y=true;
            if(this->DCMn<other.DCMn)
                y=false;
            finish: ;
            return x==y;
        }
        immense abs(void){
            immense k=*this;
            k.symbol=true;
            return k;
        }
        immense operator-(void){
            immense k;
            k.symbol=false;
            return k;
        }
        immense swap(immense &other){
            immense k=*this;
            *this=other;
            other=k;
        }
        void Add(immense other){
            int carry=0,x=0,y=0;
            for(int i=other.DCMn-1;i>=0;i--){
                if(this->DCMn<i)
                    this->DCMp+=other.DCMp.substr(this->DCMn),i=this->DCMn;
                else{
                    x=this->DCMp[i]-'0';
                    y=other.DCMp[i]-'0';
                    this->DCMp[i]=(x+y+carry)%10+'0';
                    carry=(x+y+carry)/10;
                }
            }
            for(int i=0;i<other.LTGn;i++){
                if(i>this->LTGn){
                    if(other.LTGp[i]-'0'+carry>9)
                        this->LTGp+=(other.LTGp[i]-'0'+carry)%10+'0',carry=1;
                    else 
                        this->LTGp+=other.LTGp[i],carry=0;
                }     
                else{
                    x=this->LTGp[i]-'0';
                    y=other.LTGp[i]-'0';
                    this->LTGp[i]=(x+y+carry)%10+'0';
                    carry=(x+y+carry)/10;
                }
            }
            if(carry==1)LTGp+='1';
            this->LTGn=this->LTGp.size();
            this->DCMn=this->DCMp.size();
        }
        immense Less(immense other){
            
        }
        immense Centre(bool AmS,immense other){
            immense result=*this;
            if(1);
            return result;
        }
        //赋值
};
immense x,y,z;
string a,b;
int main()
{
    while(cin>>a>>b)
    {
        x=a,y=b;
        cout<<((x>y)?"true":"false")<<endl;
    }
    


    return 0;
}

img


输出1:

-1 5
false
24 -2
false

输出2:

24 -2
true

为什么会不一样
出问题的是bool operator>(immense other)函数
哪出了问题

  • 写回答

4条回答 默认 最新

  • 黑白双芙 2024-07-22 17:36
    关注

    朋友们,出问题的是bool operator>(immense other)函数

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月4日
  • 已采纳回答 7月27日
  • 修改了问题 7月22日
  • 修改了问题 7月22日
  • 展开全部

悬赏问题

  • ¥15 关于stm32hal库驱动ft6336触摸屏遇到的问题
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥100 关于#audobe audition#的问题,如何解决?
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥15 高通uboot 打印ubi init err 22