High-Precision computing template C Language

Source: Internet
Author: User
# Include <iostream> # include <string. h> # include <algorithm> # include <stdio. h> # define Maxx 100 using namespace STD; // high-precision addition: void add (char * S1, char * S2) {int n, m, I, J, A [Maxx + 10] = {0}, B [Maxx + 10] = {0}; int L1 = strlen (S1); int L2 = strlen (S2 ); for (I = l1-1, j = Maxx; I> = 0; I --, j --) A [J] = S1 [I]-'0 '; for (I = l2-1, j = Maxx; I> = 0; I --, j --) B [J] = S2 [I]-'0 '; for (I = Maxx; I> = 0; I --) {A [I] + = B [I]; if (a [I]> = 10) A [I-1] + = A [I]/10, a [I] % = 10;} int temp = 0; for (I = 0; I <= Maxx; I ++) {if (a [I]! = 0 & temp = 0) temp = 1; if (temp = 1) printf ("% d", a [I]);} puts ("");} // high-precision subtraction void sub (char * S1, char * S2) {int n, m, I, J, A [Maxx + 10] = {0}, B [Maxx + 10] = {0}, C [Maxx + 10] = {0 }; int L1 = strlen (S1); int L2 = strlen (S2); for (I = l1-1, j = Maxx; I> = 0; I --, j --) A [J] = S1 [I]-'0'; for (I = l2-1, j = Maxx; I> = 0; I --, j --) B [J] = S2 [I]-'0'; for (I = Maxx; I> = 0; I --) {c [I] + = A [I]-B [I]; If (C [I] <0) {C [I] + = 10; c [I-1] --;} int temp = 0; for (I = 0; I <= Maxx; I ++) {If (C [I]! = 0 & temp = 0) temp = 1; if (temp = 1) printf ("% d", C [I]);} puts ("");} // high-precision multiplication void MUL (char * S1, char * S2) {int n, m, I, j, i1, J1, A [Maxx + 10] = {0}, B [Maxx + 10] = {0}, C [2 * Maxx + 10] = {0 }; int L1 = strlen (S1); int L2 = strlen (S2); for (I = l1-1, j = 0; I> = 0; I --) A [J ++] = S1 [I]-'0'; for (I = l2-1, j = 0; I> = 0; I --) B [J ++] = S2 [I]-'0'; for (I = 0; I <L2; I ++) for (j = 0; j <L1; j ++) C [I + J] + = B [I] * A [J]; for (I = 0; I <Maxx * 2; I ++) if (C [I]> = 10) {C [I + 1] + = C [I]/10; C [I] % = 10;} int T = 0; For (I = Maxx * 2; I> = 0; I --) if (t) printf ("% d", C [I]); else if (C [I]) {printf ("% d", C [I]); t = 1 ;}if (! T) printf ("0"); puts ("");} // high-precision division # include <iostream> # include <string. h> # include <algorithm> # include <stdio. h> # define Maxx 100 using namespace STD; int an1 [Maxx + 10], an2 [Maxx + 10]; int ans [Maxx + 10];/* Sun function: A large integer P1 whose length is L1 minus a large integer P2 whose length is L2. The result is placed in P1. The returned value indicates the length of the result */INT sub (int * P1, int * P2, int L1, int l2) {int I; If (L1 <l2) Return-1; // check whether P1 is larger than P2. If not, returns-1 bool temp = false; If (L1 = l2) {for (I = l1-1; I> = 0; I --) {If (P1 [I]> P2 [I]) TEM P = true; else if (P1 [I] <P2 [I]) {If (! Temp) Return-1 ;}}for (I = 0; I <L1; I ++) {// do the subtraction P1 [I]-= P2 [I]; // The parameter required to call this function can ensure that when I> = len2, P2 [I] = 0 if (P1 [I] <0) {p1 [I] + = 10; P1 [I + 1] -- ;}} for (I = l1-1; I> = 0; I --) if (P1 [I]) return I + 1; return 0;} int main () {int I, j; char S1 [Maxx + 10], S2 [Maxx + 10]; scanf ("% S % s", S1, S2); int L1 = strlen (S1), L2 = strlen (S2); memset (an1, 0, sizeof (an1); memset (an2, 0, sizeof (an2); memset (ANS, 0, sizeof (ANS); for (I = l1-1, j = 0; i> = 0; I --) an1 [J ++] = S1 [I]-'0'; fo R (I = l2-1, j = 0; I> = 0; I --) an2 [J ++] = S2 [I]-'0'; If (L1 <l2) {printf ("0 \ n"); Return 0;} L1 = sub (an1, an2, L1, L2); If (L1 <0) {printf ("0 \ n"); Return 0;} else if (L1 = 0) {printf ("1 \ n"); Return 0 ;} ans [0] ++; int ntimes = L1-L2; If (ntimes <0) goto outputresult; else if (ntimes> 0) {for (I = l1-1; I> = 0; I --) {if (I> = ntimes) an2 [I] = an2 [I-ntimes]; elsean2 [I] = 0 ;}} L2 = L1; for (j = 0; j <= ntimes; j ++) {int ntmp; // until the limit is reached, first subtract several ans * (the ntimes power of 10) // not enough. Subtract again. Go to several ans * (10 ntimes power) while (ntmp = sub (an1, an2 + J, L1, l2-j)> = 0) {L1 = ntmp; ans [ntimes-J] ++ ;}} outputresult: // The following loop uniformly handles the carry problem for (I = 0; I <Maxx; I ++) {If (ANS [I] >=10) {ans [I + 1] + = ans [I]/10; ans [I] % = 10 ;}} bool T = false; for (I = Maxx; I> = 0; I --) if (t) printf ("% d", ANS [I]); else if (ANS [I]) {printf ("% d", ANS [I]); t = true;} If (! T) printf ("0 \ n"); printf ("\ n"); Return 0 ;}

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.