Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Robert Lafore - Object-Oriented Programming in C++, 4th Edition :</p> <pre><code>// verylong.cpp // implements very long integer type #include "verylong.h" //header file for verylong //-------------------------------------------------------------- void verylong::putvl() const //display verylong { char temp[SZ]; strcpy(temp,vlstr); //make copy cout &lt;&lt; strrev(temp); //reverse the copy } //and display it //-------------------------------------------------------------- void verylong::getvl() //get verylong from user { cin &gt;&gt; vlstr; //get string from user vlen = strlen(vlstr); //find its length strrev(vlstr); //reverse it } //-------------------------------------------------------------- verylong verylong::operator + (const verylong v) //add verylongs { char temp[SZ]; int j; //find longest number int maxlen = (vlen &gt; v.vlen) ? vlen : v.vlen; int carry = 0; //set to 1 if sum &gt;= 10 for(j = 0; j&lt;maxlen; j++) //for each position { int d1 = (j &gt; vlen-1) ? 0 : vlstr[j]-'0'; //get digit int d2 = (j &gt; v.vlen-1) ? 0 : v.vlstr[j]-'0'; //get digit int digitsum = d1 + d2 + carry; //add digits if( digitsum &gt;= 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 verylong(temp); //return temp verylong } //-------------------------------------------------------------- verylong verylong::operator * (const verylong v) //multiply { //verylongs verylong pprod; //product of one digit verylong tempsum; //running total for(int j=0; j&lt;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&lt;j; k++) //multiply result by pprod = mult10(pprod); // power of 10 tempsum = tempsum + pprod; //add product to total } return tempsum; //return total of prods } //-------------------------------------------------------------- verylong verylong::mult10(const verylong v) const //multiply { //arg by 10 char temp[SZ]; for(int j=v.vlen-1; j&gt;=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 verylong(temp); //return result } //-------------------------------------------------------------- verylong verylong::multdigit(const int d2) const { //multiply this verylong char temp[SZ]; //by digit in argument int j, carry = 0; for(j = 0; j&lt;vlen; j++) //for each position { // in this verylong int d1 = vlstr[j]-'0'; //get digit from this int digitprod = d1 * d2; //multiply by that digit digitprod += carry; //add old carry if( digitprod &gt;= 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 verylong(temp); //return verylong } </code></pre> <p>Verylong class header</p> <pre><code>// verylong.h // class specifier for very long integer type #include &lt;iostream&gt; #include &lt;string.h&gt; //for strlen(), etc. #include &lt;stdlib.h&gt; //for ltoa() using namespace std; const int SZ = 1000; //maximum digits in verylongs class verylong { private: char vlstr[SZ]; //verylong number, as a string int vlen; //length of verylong string verylong multdigit(const int) const; //prototypes for verylong mult10(const verylong) const; //private functions public: verylong() : vlen(0) //no-arg constructor { vlstr[0]='\0'; } verylong(const char s[SZ]) //one-arg constructor { strcpy(vlstr, s); vlen=strlen(s); } //for string verylong(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 verylong void getvl(); //get verylong from user verylong operator + (const verylong); //add verylongs verylong operator * (const verylong); //multiply verylongs }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload