Big Data Calculator

Source: Internet
Author: User
# Include <iostream> # include <algorithm> # include <stdio. h> # include <string. h> using namespace STD; # define max_digit 500 // large number operation: Addition int multiply (int * a, int * B, int * & result); // large number operation: multiplication int add (int * a, int * B, int * & result); // large number operation: subtraction int sub (int * a, int * B, int * & result); // adjust the large number one by one. For example, if the result is 11 9 22-3-2, the value is 1 0 3 9-3, then the symbol is raised to the highest digit: 9 9 6 0 2 ...... -1 // indicates the value of a large number is-20699 void ajustencodearray (int * & result, unsigned int IMA Xnum); // output the large number void printbignum (int * result); // convert the character array with a single position in the high position into a single position in the low position (subscript 0) void carray2revdigitarray (char * carray, int * digitarray, unsigned int inum); // large number operation: Addition int add (int * a, int * B, int * & result) {if (a = NULL | B = NULL | result = NULL) {printf ("Multiply: input error/N "); return-1;} int I = 0, j = 0; // initialize the result array for (I = 0; I <max_digit * 2; ++ I) {result [I] = 0;} for (I = 0; I <max_digit; ++ I) {result [I] + = (a [I] + B [I ]);} Ajustencodearray (result, max_digit * 2); Return 0;} // large number operation: subtraction int sub (int * a, int * B, int * & result) {if (a = NULL | B = NULL | result = NULL) {printf ("Multiply: input error/N"); Return-1 ;} int I = 0, j = 0; // initialize the result array for (I = 0; I <max_digit * 2; ++ I) {result [I] = 0 ;} for (I = 0; I <max_digit; ++ I) {result [I] + = (a [I]-B [I]); // The result can be a negative number} ajustencodearray (result, max_digit * 2); Return 0;} // multiplication of large numbers int multiply (int * A, int * B, int * & result) {if (a = NULL | B = NULL | result = NULL) {printf ("Multiply: input error/N "); Return-1;} int I = 0, j = 0; // initialize the result array for (I = 0; I <max_digit * 2; ++ I) {result [I] = 0;} // A [0] is a single-digit calculation, and the result offset is I + J. // For example, a single-digit * ten digits, the offset of the calculation result is 0 + 1, that is, 1 // example: A [0] is 9, B [1] is 7, then result [1] is added with 63 for (I = 0; I <max_digit; ++ I) {for (j = 0; j <max_digit; ++ J) {result [I + J] + = A [I] * B [J] ;}} ajustencodearray (result, max_digit * 2); Return 0 ;}// Adjust the number of digits. For example, if the result is 11 9 22-3-2, it is adjusted to 1 0 3 9-3, and then the symbol is raised to the highest digit: 9 9 6 0 2 ...... -1 // indicates the value of a large number is-20699 void ajustencodearray (int * & result, unsigned int imaxnum) {int I = 0; unsigned int inumflag = 0; unsigned int iweight = 0; // the weight of the most valid bits // traverse to find the subscript of the highest valid bits (both positive and negative are possible) for (I = iMaxNum-1; I> = 0; -- I) {If (result [I]! = 0) {inumflag = I; break ;}// adjust the value from the low position. Rule: a positive number greater than 10 must be carried, and a negative number must be used for (I = 0; I <inumflag; ++ I) {// carry if (result [I]> 0) {result [I + 1] + = Result [I]/10; // carry result [I] = Result [I] % 10 for more than 10 digits; // adjust the current bit after carry} else if (result [I] <0) {result [I + 1]-= 1; // ignore whether a high position can be used, forcibly borrow result [I] = 10 + result [I];} // if the maximum valid bits are negative, you need to increase the negative number, that is, perform a subtraction of the high and the remaining low bits if (result [inumflag] <0) {iweight =-result [inumflag]; int iminuend [2 * max_digit] = {0 }; Iminuend [inumflag] = iweight; // subtrahend result [inumflag] = 0; // construct a subtrahend for (I = 0; I <inumflag + 1; ++ I) {result [I] = (iminuend [I]-result [I]) ;}// adjust the borrow position for (I = 0; I <inumflag; ++ I) {If (result [I] <0) {result [I + 1]-= 1; Result [I] = 10 + result [I] ;}} result [iMaxNum-1] =-1 ;}} void printbignum (int * result) {int Index = max_digit * 2-1; bool bnegative = false; // locate the first non-negative bit from the highest bit, and start from this bit as the valid value while (result [Index] <= 0) {If (resu Lt [Index] <0) {bnegative = true;} index --;} If (bnegative) {printf ("-") ;}for (INT I = index; i> = 0; -- I) {printf ("% d", result [I]);} printf ("/N");} void carray2revdigitarray (char * carray, int * digitarray, unsigned int inum) {int I = 0; unsigned int ilen = strlen (carray); // initialize digitarray for (I = 0; I <inum; ++ I) {digitarray [I] = 0 ;}for (I = 0; I <ilen; ++ I) {digitarray [iLen-1-i] = carray [I]-'0';} int main () {Int A [max_digit], B [max_digit], resultarray [2 * max_digit]; char carray1 [max_digit], carray2 [max_digit]; int ret = 0; int * result = (int *) resultarray; printf ("input multiplier:/N"); scanf ("% s", carray1); printf ("input multiplicand: /n "); scanf (" % s ", carray2); carray2revdigitarray (carray1, A, max_digit); carray2revdigitarray (carray2, B, max_digit); ret = multiply (, b, result); If (Ret! = 0) {printf ("Multiply error, RET: % d/N", RET); Return-1;} printbignum (result); ret = add (A, B, result); If (Ret! = 0) {printf ("Multiply error, RET: % d/N", RET); Return-1;} printbignum (result); ret = sub (A, B, result); If (Ret! = 0) {printf ("Multiply error, RET: % d/N", RET); Return-1;} printbignum (result); Return 0 ;}

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.