HDU 1047 Integer Inquiry

Source: Internet
Author: User

/*
Question: enter some big integers and output the sum of them.
 
A major error cause is found in this question, that is, initialization at ②.
*/
# Include <cstdio>
# Include <cstring>
Const int nmax= 200;
Struct BigNumber
{
Int data [nMax];
Int len;
BigNumber () {len = 1; memset (data, 0, sizeof (data ));}
BigNumber (char * str) {* this = str ;}
BigNumber & operator = (char * str );
BigNumber operator + (BigNumber & );
Void clearLeadZero ();
Void print ();
};
BigNumber & BigNumber: operator = (char * str)
{
Memset (data, 0, sizeof (data); // ① the latest addition. If you want to set the remaining parts to zero, add this statement!
Len = strlen (str );
Int I;
For (I = 0; I <len; ++ I)
Data [I] = str [len-I-1]-'0 ';
Return * this;
}
BigNumber: operator + (BigNumber &)
{
Int q = 0;
Int I, j;
BigNumber z;
For (I = 0, j = 0; q | I <len | j <a. len; ++ I, ++ j)
{
Int p = data [I] + a. data [j] + q;
Z. data [I] = p % 10;
Q = p/10;
}
Z. len = I;
Z. clearLeadZero ();
Return z;
}
Void BigNumber: clearLeadZero ()
{
While (len> 1 &&! Data [len-1]) -- len;
}
Void BigNumber: print ()
{
Int I;
For (I = len-1; I> = 0; -- I)
Printf ("% d", data [I]);
Printf ("\ n ");
}
Int main ()
{
// Freopen ("f: // data. in", "r", stdin );
Int T;
Scanf ("% d", & T );
BigNumber ans;
While (T --)
{
Ans = "0 ";
Char s [nMax];
While (scanf ("% s", s )! = EOF)
{
If (s [0] = '0' & s [1] = 0)
Break;
Ans = ans + BigNumber (s );
}
Ans. print ();
If (T)
Printf ("\ n ");
}
Return 0;
}

 


From lhshaoren

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.