current position:Home>C++ code. Freshmen have just come into contact with this language

C++ code. Freshmen have just come into contact with this language

2022-06-24 09:38:41CSDN Q & A

using namespace std;
#define length 41
//write your code here
class HugeInteger
HugeInteger(char t[])
int z;
for(z=0; t[z]!='/0';z++)

```}        p=new int [z-1];        for(z=0;t[z]!='\0';z++)        {            *(p+z)=t[z]-48;        }        size=z;    }    HugeInteger(HugeInteger &pt){    p=new int [pt.size];    for(int i=0;(*(pt.p+i))!='\0';i++)    *(p+i)=*(pt.p+i);}    ~HugeInteger(){
   delete p;}    void input(char s[]) {        int z;        if(s[z]!='\0'){z++;}        p=new int [z-1];        for(z=0;s[z]!='\0';z++)        {            *(p+z)=s[z]-48;        }        size=z;    }    void output() {   if(size<=40)        {        int i;        for(i=0;i<size;i++)        {cout<<*(p+i);}        }        else        {cout<<"outrage";}    }    HugeInteger add(HugeInteger &b) {        long int sum1=0,sum2=0;        int i,j,mii=b.size;        for(j=mii-1,i=0;(j<mii)&&(i<=mii-1);j--,i++)        {            sum1=sum1+((*(b.p+j))*(pow(10,i)));        }        int j1,i1;        for(j1=mii-1,i1=0;(j1<size)&&(i1<=size-1);i1++,j1--)        {            sum2=sum2+((*(p+j1))*(pow(10,i1)));        }        long long int sum=sum1+sum2;        long long int summ=sum;        // How many digits is the judgment         int len=0;        while(sum){sum=sum/10;len++;}         int temp=pow(10,len-1);        int k=len,n=0,tap[length];        for(;(k>=0)&&(n<=len-1);n++)        {            tap[n]=(summ/temp);            summ=summ-(tap[n]*temp);            k=k-1;            temp=pow(10,k);        }        HugeInteger *r=new HugeInteger;        int z;char tem[length];        for(z=0;tap[z]!='\0';z++)        {            tem[z]=tap[z]+48;        }        r->input(tem);         return *r;    }    private:    int *p;    int size;};int main(){    HugeInteger hi1;    char *c = new char[length];    cin >> c;    char *d = new char[length];    cin >> d;    hi1.input(c);    HugeInteger hi2(d);    (hi1.add(hi2)).output();    return 0;    delete [] c;    delete [] d;}****


copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended