• 019:别叫,这个大整数已经很简化了!


    总时间限制: 1000ms 内存限制: 65536kB
    描述
    程序填空,输出指定结果
    
    #include <iostream> 
    #include <cstring> 
    #include <cstdlib> 
    #include <cstdio> 
    using namespace std;
    const int MAX = 110; 
    class CHugeInt {
    // 在此处补充你的代码
    };
    int  main() 
    { 
        char s[210];
        int n;
    
        while (cin >> s >> n) {
            CHugeInt a(s);
            CHugeInt b(n);
    
            cout << a + b << endl;
            cout << n + a << endl;
            cout << a + n << endl;
            b += n;
            cout  << ++ b << endl;
            cout << b++ << endl;
            cout << b << endl;
        }
        return 0;
    }
    输入
    多组数据,每组数据是两个非负整数s和 n。s最多可能200位, n用int能表示
    输出
    对每组数据,输出6行,内容分别是:
    样例输入
    99999999999999999999999999888888888888888812345678901234567789 12
    6 6
    样例输出
    99999999999999999999999999888888888888888812345678901234567801
    99999999999999999999999999888888888888888812345678901234567801
    99999999999999999999999999888888888888888812345678901234567801
    25
    25
    26
    12
    12
    12
    13
    13
    14

    这题需要写三个构造函数,分别是无参,int参数,char *参数。还需要重载3个+运算符,分别对应CHugeInt+int,int+CHugeInt,CHugeInt+CHugeInt,还需要重载前置++和后置++,最后还需要一个<<的重载。代码如下:

    #include <iostream> 
    #include <cstring> 
    #include <cstdlib> 
    #include <cstdio> 
    using namespace std;
    const int MAX = 110; 
    class CHugeInt {
    // 在此处补充你的代码
    private:
        char maxNum[210];
        int len;
    public:
        CHugeInt(char * s){
            strcpy(maxNum,s);
            int i=0,j=strlen(s)-1;
            while(i<j)
            {
                swap(maxNum[i],maxNum[j]);
                i++;
                j--;
            }
            //cout<<"init:"<<maxNum<<endl;
            len=strlen(s);
            //cout<<"Init success"<<endl;
        }
        CHugeInt(){
            len=0;
        } 
        CHugeInt(int n){
            int i=0;
            if(n==0)
            {
                maxNum[i++]='0';
            }else{
                while(n)
                {
                    maxNum[i++]=n%10+'0';
                    n=n/10;
                }    
            }
            maxNum[i]='';
            len=i;
            //cout<<maxNum<<endl;
        }
        CHugeInt  operator+(CHugeInt & a)
        {
                //cout<<"hrer"<<endl;
                int i=0,j=0;
                int t,sum=0;
                CHugeInt temps;
                strcpy(temps.maxNum,maxNum);
                temps.len=len;
                //cout<<"before:"<<temps.maxNum<<endl;
                //maxNum=new char[strlen(a.maxNum)+1];
                //cout<<a.len<<","<<len<<endl;
                int flag=0;
                while(j<a.len&&i<temps.len)
                {
                    t=a.maxNum[j]-'0';
                    int te=temps.maxNum[i]-'0';
                    sum=t+te;
                    //cout<<t<<"+"<<te<<":"<<sum<<endl;
                    if(sum>=10)
                    {
                        temps.maxNum[i]=sum%10+'0';
                        //cout<<temps.maxNum[i]<<endl;
                        temps.maxNum[i+1]=sum/10+temps.maxNum[i+1];
                        if(i+1>=temps.len)
                        {
                            temps.maxNum[i+1]+='0'; 
                        }
                        flag=1;
                    }else{
                        //cout<<"sum:"<<sum<<endl;
                        flag=0;
                        temps.maxNum[i]=sum+'0';
                    }
                    //cout<<temps.maxNum[i]<<endl;
                    i++,j++;
                    sum=0;
                }
                while(j<a.len)
                {
                    if(flag==1)
                    {
                        temps.maxNum[i+1]=a.maxNum[j];
                        i++,j++;    
                    }else{
                        temps.maxNum[i]=a.maxNum[j];
                        i++,j++;
                    }
                }
                if(i>=len)
                {
                    if(flag==1){
                        temps.maxNum[i+1]='';
                        temps.len=i+1;
                    }
                    else{
                        temps.maxNum[i]='';
                        temps.len=i;
                    }        
                }
            return temps;
        }
        /*operator char *()
        {
            return maxNum;
        }*/
        CHugeInt & operator +=(int n)
        {
            CHugeInt temps(n);
            *this=this->operator+(temps);
            //cout<<this->maxNum<<endl;
            return *this;
        }
        friend ostream & operator<<(ostream & os,const CHugeInt & s)
        {
                int i=0,j=s.len-1;
                //cout<<"len:"<<s.len<<endl;
                //cout<<"输出:"<<s.maxNum<<endl;
                for(;j>=i;j--)
                    os<<s.maxNum[j];
                return os;
        }
        friend CHugeInt  operator+(int n,CHugeInt  s)
        {
            CHugeInt temps(n);
            s=s+temps;
            return s;
        }
        friend CHugeInt  operator+(CHugeInt  s,int n)
        {
            CHugeInt temps(n);
            s=s+temps;
            return s;
        }
        CHugeInt &  operator++()
        {
            (*this)+=1;
            //cout<<"前置自增后:"<<this->maxNum<<endl;
            return *(this);
        }
        CHugeInt   operator++(int n)
        {
            CHugeInt temps;
            strcpy(temps.maxNum,maxNum);
            temps.len=len;
            this->operator +=(1);
            //cout<<temps.maxNum<<endl;
            //cout<<"len:"<<temps.len<<endl;
            return temps;
        }
    };
    int  main() 
    { 
        char s[210];
        int n;
    
        while (cin >> s >> n) {
            CHugeInt a(s);
            CHugeInt b(n);
    
            cout << a + b << endl;
            cout << n + a << endl;
            cout << a + n << endl;
            b += n;
            cout  << ++ b << endl;
            cout << b++ << endl;
            cout << b << endl;
        }
        return 0;
    }
  • 相关阅读:
    jquery datatable后台分页、移动端隐藏列、自定义列显示格式
    今日热门文章
    深入理解STL源码 系列文章
    LED显示屏通讯协议 2
    LED显示屏通讯协议 1
    定制的小键盘输入数字显示的LED计分显示屏
    Sql语句中的LIKE关键字中通配符以及Escape关键字
    Virtual Box扩展虚拟硬盘的问题
    选择腾讯云部署应用的要慎重了,私有网络阉割,可靠性变得难以确定!
    rabbitmq 重置
  • 原文地址:https://www.cnblogs.com/JsonZhangAA/p/8010679.html
Copyright © 2020-2023  润新知