C++ examples for Data Structure:Algorithm
Implements very long integer type
#include <iostream> #include <string.h> //for strlen(), etc. #include <stdlib.h> //for ltoa() using namespace std; const int SZ = 1000; //maximum digits in BigIntegers class BigInteger/*from ww w . j ava2 s .c o m*/ { private: char vlstr[SZ]; //BigInteger number, as a string int vlen; //length of BigInteger string BigInteger multdigit(const int) const; //prototypes for BigInteger mult10(const BigInteger) const; //private functions public: BigInteger() : vlen(0) //no-arg constructor { vlstr[0]='\0'; } BigInteger(const char s[SZ]) //one-arg constructor { strcpy(vlstr, s); vlen=strlen(s); } //for string BigInteger(const unsigned long n) //one-arg constructor { //for long int ltoa(n, vlstr, 10); //convert to string strrev(vlstr); //reverse it vlen=strlen(vlstr); //find length } void putvl() const; //display BigInteger void getvl(); //get BigInteger from user BigInteger operator + (const BigInteger); //add BigIntegers BigInteger operator * (const BigInteger); //multiply BigIntegers }; void BigInteger::putvl() const //display BigInteger { char temp[SZ]; strcpy(temp,vlstr); //make copy cout << strrev(temp); //reverse the copy } //and display it void BigInteger::getvl() //get BigInteger from user { cin >> vlstr; //get string from user vlen = strlen(vlstr); //find its length strrev(vlstr); //reverse it } BigInteger BigInteger::operator + (const BigInteger v) //add BigIntegers { char temp[SZ]; int j; //find longest number int maxlen = (vlen > v.vlen) ? vlen : v.vlen; int carry = 0; //set to 1 if sum >= 10 for(j = 0; j<maxlen; j++) //for each position { int d1 = (j > vlen-1) ? 0 : vlstr[j]-'0'; //get digit int d2 = (j > v.vlen-1) ? 0 : v.vlstr[j]-'0'; //get digit int digitsum = d1 + d2 + carry; //add digits if( digitsum >= 10 ) //if there's a carry, { digitsum -= 10; carry=1; } //decrease sum by 10, else //set carry to 1 carry = 0; //otherwise carry is 0 temp[j] = digitsum+'0'; //insert char in string } if(carry==1) //if carry at end, temp[j++] = '1'; //last digit is 1 temp[j] = '\0'; //terminate string return BigInteger(temp); //return temp BigInteger } BigInteger BigInteger::operator * (const BigInteger v) //multiply { //BigIntegers BigInteger pprod; //product of one digit BigInteger tempsum; //running total for(int j=0; j<v.vlen; j++) //for each digit in arg { int digit = v.vlstr[j]-'0'; //get the digit pprod = multdigit(digit); //multiply this by digit for(int k=0; k<j; k++) //multiply result by pprod = mult10(pprod); // power of 10 tempsum = tempsum + pprod; //add product to total } return tempsum; //return total of prods } BigInteger BigInteger::mult10(const BigInteger v) const //multiply { //arg by 10 char temp[SZ]; for(int j=v.vlen-1; j>=0; j--) //move digits one temp[j+1] = v.vlstr[j]; // position higher temp[0] = '0'; //put zero on low end temp[v.vlen+1] = '\0'; //terminate string return BigInteger(temp); //return result } BigInteger BigInteger::multdigit(const int d2) const { //multiply this BigInteger char temp[SZ]; //by digit in argument int j, carry = 0; for(j = 0; j<vlen; j++) //for each position { // in this BigInteger int d1 = vlstr[j]-'0'; //get digit from this int digitprod = d1 * d2; //multiply by that digit digitprod += carry; //add old carry if( digitprod >= 10 ) //if there's a new carry, { carry = digitprod/10; //carry is high digit digitprod -= carry*10; //result is low digit } else carry = 0; //otherwise carry is 0 temp[j] = digitprod+'0'; //insert char in string } if(carry != 0) //if carry at end, temp[j++] = carry+'0'; //it's last digit temp[j] = '\0'; //terminate string return BigInteger(temp); //return BigInteger } int main() { unsigned long numb, j; BigInteger fact=1; //initialize BigInteger cout << "\n\nEnter number: "; cin >> numb; //input a long int for(j=numb; j>0; j--) //factorial is numb * fact = fact * j; // numb-1 * numb-2 * cout << "Factorial is "; // numb-3 and so on fact.putvl(); //display factorial cout << endl; return 0; }