Basic operations for long integers

Source: Internet
Author: User
// Longint. CPP: defines the entry point for the console application. // # include "stdafx. H "# include" string. H "# define maxlen 50/* function inputlongint () long integer input function */INT inputlongint (int * a, char * para) {int Len, I; char number [maxlen]; printf ("enter a long integer [% s]:", para); scanf ("% s", number); Len = strlen (number ); for (I = Len; I> = 1; I --) A [I] = number [Len-I]-'0'; A [0] = Len; return (A [0]);}/* function outputlongint () Long Integer Output Function */void outputlongint (int * a, char * para) {int I; printf ("output [% s] Long Integer:", para ); for (I = A [0]; I> = 1; I --) printf ("% d", a [I]); printf ("");} /* function formatlongint () Long Integer normalization function */INT formatlongint (int * A) {int P; For (P = 1; P <A [0] | A [p]> = 10; P ++) {If (P> = A [0]) A [p ++] = 0; A [p ++] + = A [p]/10; A [p] = A [p] % 10;} If (P> A [0]) A [0] = P; Return (A [0]);}/* function longintaddlongint () Long Integer lengthen the sum of integer functions and put them in array a */void longintaddlong INT (int * a, int * B) {int I; while (A [0] <B [0]) A [++ A [0] = 0; for (I = 1; I <= B [0]; I ++) A [I] + = B [I]; formatlongint ();} /* function longintdivint () Long Integer in addition to the quotient obtained by the ordinary integer function is placed in array A, and the remainder is placed in the return value */INT longintdivint (int * a, int divisor) {int P, // store the remainder subscripts K after division; // store the number of numbers (the number of valid long integers) k = P = A [0]; A [0] = 0; while (P> 0) {A [P-1] + = A [p] % divisor * 10; A [p] = A [p]/divisor; if (a [k] = 0) k --; p --;} p = A [0]/10; // Save the remainder a [0] = K; // Return the number of valid digits formatlongint (a); Return (p);}/* function longintdivint () convert a long integer to a binary number. The binary number converted by the function is stored in array B */void longinttobin (int * a, int * B) {int P; B [0] = 0; while (A [0]> 0) {B [0] ++; B [B [0] = A [1] % 2; P = A [0]; while (P> 0) {If (A [p] % 2) & (p> 1) a P-1] + = 10; A [p]/= 2; if (a [A [0] = 0) A [0] --; p -- ;}} int main (INT argc, char * argv []) {int A [maxlen], B [maxlen]; int Len, residue;/* Len = inputlongint (); Printf ("the length of the source long integer is % d", Len); STR = "Source"; outputlongint (A, STR); Len = formatlongint (); printf ("the length of the long integer after normalization is % d", Len); STR = "after normalization"; outputlongint (A, STR); residue = longintdivint (A, 11 ); STR = "divided by an ordinary integer 11"; outputlongint (A, STR); printf ("the remainder of a long integer divided by an ordinary integer 11 is: % d", residue ); */Len = inputlongint (a, "first addend"); // Len = inputlongint (B, "Second addend"); // longintaddlongint (A, B ); // outputlongint (a, "sum of two numbers and"); longinttobin (A, B); outputlongint (B, "converted binary"); printf ("the application is running! "); 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.