SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 4 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 28 Nov 2017 My Price 10.00

Modified from textbook Larry Nyhoff, ADTs, Data Structures,

Hi bro,Help me finish this assignment and also need the muddiest point for it,thanks

  • //cmpsc122 Assignment 6
    //Modified from textbook Larry Nyhoff,  ADTs, Data Structures, and Problem Solving
    //with C++, 2nd ed., Prentice-Hall, 2005.

    /*-- BigInt.cpp-------------------------------------------------------------
                    This file implements BigInt member functions.
    --------------------------------------------------------------------------*/

    #include <iostream>
    #include <cmath>
    using namespace std;

    #include "BigInt.h"

    //--- Definition of read()
    void BigInt::read(istream & in)
    {
      static bool instruct = true;
      if (instruct)
      {
         cout << "Enter " << DIGITS_PER_BLOCK << "-digit blocks, separated by "
                "spaces.\nEnter a negative integer in last block to signal "
                "the end of input.\n\n";
        instruct = false;
      }
      short int block;
      const short int MAX_BLOCK = (short) pow(10.0, DIGITS_PER_BLOCK) - 1;
      for (;;)
      {
        in >> block;
        if (block < 0) return;

        if (block > MAX_BLOCK)
          cerr << "Illegal block -- " << block << " -- ignoring\n";
        else
          myList.push_back(block);
      }
    }

    //--- Definition of display()
    void BigInt::display(ostream & out) const
    {
       int blockCount = 0;
       const int BLOCKS_PER_LINE = 20;   // number of blocks to display per line

       for (list<short int>::const_iterator it = myList.begin(); ; )
       {
          out << setfill('0');
          if (blockCount == 0)
             out << setfill(' ');
     
          if (it == myList.end())
             return;

          out << setw(3) << *it;
          blockCount++ ;

          it++;
          if (it != myList.end())
          {
             out << ',';
             if (blockCount > 0 && blockCount % BLOCKS_PER_LINE == 0)
                out << endl;
          }
       }
    }

    //--- Definition of operator+()
    BigInt BigInt::operator+(BigInt addend2)
    {
       BigInt sum;
       short int first,                  // a block of 1st addend (this object)
                 second,                 // a block of 2nd addend (addend2)
                 result,                 // a block in their sum
                 carry = 0;              // the carry in adding two blocks

       list<short int>::reverse_iterator // to iterate right to left
          it1 = myList.rbegin(),         //   through 1st list, and
          it2 = addend2.myList.rbegin(); //   through 2nd list

       while (it1 != myList.rend() || it2 != addend2.myList.rend())
       {
          if (it1 != myList.rend())
          {
             first = *it1;
             it1++ ;
          }
          else
             first = 0;
          if (it2 != addend2.myList.rend())
          {
             second = *it2;
             it2++ ;
          }
          else
             second = 0;

          short int temp = first + second + carry;
          result = temp % 1000;
          carry = temp / 1000;
          sum.myList.push_front(result);
       }

       if (carry > 0)
          sum.myList.push_front(carry);

       return sum;//cmpsc122 Su 2012 Assignment 6
    //Modified from textbook Larry Nyhoff,  ADTs, Data Structures, and Problem Solving
    //with C++, 2nd ed., Prentice-Hall, 2005.

    /*-- BigInt.h -------------------------------------------------------------
     
      This header file defines the data type BigInt for processing
      integers of any size.
      Basic operations are:
         Constructor
         +:         Addition operator
         read():    Read a BigInt object
         display(): Display a BigInt object
         <<, >> :   Input and output operators
    -------------------------------------------------------------------------*/

    #include <iostream>
    #include <iomanip>       // setfill(), setw()
    #include <list>
    #include <cmath>    // pow

    #ifndef BIGINT
    #define BIGINT

    const int DIGITS_PER_BLOCK = 3;

    const int MODULUS = (short int)pow(10.0, DIGITS_PER_BLOCK);

    class BigInt
    {
     public:
        /***** Constructors *****/
        BigInt()
        { }
        /*-----------------------------------------------------------------------
        Default cConstructor
        Precondition: None
        Postcondition: list<short int>'s constructor was used to build
        this BigInt object.
        -----------------------------------------------------------------------*/
        BigInt(int n);
        /*-----------------------------------------------------------------------
        Construct BigInt equivalent of n.
        Precondition: n >= 0.
        Postcondition: This BigInt is the equivalent of integer n.
        -----------------------------------------------------------------------*/
        /******** Function Members ********/
        /***** Constructor *****/
        // Let the list<short int> constructor take care of this.

        /***** read *****/
        void read(istream & in);
        /*-----------------------------------------------------------------------
            Read a BigInt.

            Precondition:  istream in is open and contains blocks of nonnegative
                integers having at most DIGITS_PER_BLOCK digits per block.
            Postcondition: Blocks have been removed from in and added to myList.
        -----------------------------------------------------------------------*/

        /***** display *****/
        void display(ostream & out) const;
        /*-----------------------------------------------------------------------
            Display a BigInt.

            Precondition:  ostream out is open.
            Postcondition: The large integer represented by this BigInt object
                has been formatted with the usual comma separators and inserted
                into ostream out.
        ------------------------------------------------------------------------*/

        /***** addition operator *****/
        BigInt operator+(BigInt addend2);
        /*------------------------------------------------------------------------
            Add two BigInts.

            Precondition:  addend2 is the second addend.
            Postcondition: The BigInt representing the sum of the large integer
            represented by this BigInt object and addend2 is returned.
        ------------------------------------------------------------------------*/
       
        private:
        /*** Data Members ***/
        list<short int> myList;
    };  // end of BigInt class declaration

    //-- Definition of constructor
    inline BigInt::BigInt(int n)
    {
        do
        {
            myList.push_front(n % MODULUS);
            n /= MODULUS;
        }
        while (n > 0);
    }
    //------ Input and output operators
    inline istream & operator>>(istream & in, BigInt & number)
    {
      number.read(in);
      return in;
    }

    inline ostream & operator<<(ostream & out, const BigInt & number)
    {
      number.display(out);
      return out;
    }

    #endif
    //cmpsc122 Assignment 6
    // Please do not modify this file!
    // --- Program to test BigInt class.
    // Modified from textbook Larry Nyhoff,  ADTs, Data Structures, and Problem Solving
    // with C++, 2nd ed., Prentice-Hall, 2005.

    #include <iostream>
    using namespace std;

    #include "BigInt.h"

    int main()
    {
      
    //  However, you are not allowed to modify the following codes.

       char response;
       do
       {
          BigInt number1, number2;
          cout <<"Enter a big integer:\n";
          cin >> number1;
          cout <<"Enter another big integer:\n";
          cin >> number2;
         
          // original one: test the operation +
          cout << "The sum of\n\t"
               << number1 << " + " << number2
               << "\nis\n\t" << number1 + number2 << endl;

          // 1. test the operation >
          cout << "\nThe bigger number of\n\t"
               << number1 << "\nand\n\t" << number2
               << "\nis\n\t" << ((number1 > number2) ? number1 : number2) << endl;

          // 2. test the operation -
          cout << "\nThe subtraction of\n\t"
               << number1 << " - " << number2
               << "\nis\n\t" << number1 - number2 << endl;


          // 3.(bonus) test the operation *

         
          // comment the following out if you don't do 3.
         
          cout << "\nBONUS part:" << endl;
          cout << "The multiplication of\n\t"
               << number1 << " * " << number2
               << "\nis\n\t" << number1 * number2 << endl;

          cout << "\nAdd more integers (Y or N)? ";
          cin >> response;
       }
       while (response == 'y' || response == 'Y');

       return 0;
    }

    }

Attachments:

Answers

(5)
Status NEW Posted 28 Nov 2017 01:11 PM My Price 10.00

-----------  ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly

Not Rated(0)