Thread: Help with structures and classes

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Murfreesboro, TN
    Posts
    5

    Help with structures and classes

    Hi guys,

    I have to write a program that will read in large numbers from a file as strings, then store each digit in a linked list. The program will then add or subtract each line depending on the operator between them and return a result. My instructor has provided the class definition files and a simple structure for the entire program, however I am a little confused on how to store a linked list in a single variable. In the main program he has:

    BigInt result, rhs;

    result = BigInt(operand1);

    BigInt is the name of the class and there is a constructor for BigInt that has a string parameter. Basically my question is how do I store this linked list in result so that result contains the integer? I'm not sure if that makes sense... here are the files if they might help explain it better.

    main.cpp
    Code:
    /* Jerome Collins
    CSCS 2170
    Open Lab 5
    Due Nov 16, 2009 */
    
    #include <fstream>
    #include "BigInt.h"
    
    using namespace std;
    
    int main( void )
    {
        ifstream    ifs;
    	ifs.open("data.txt");
    
        if ( !ifs )
        {
            cout << "Cannot open the file data.txt!" << endl;
            return -1;
        }
    
        else
        {
            string      operand1, operand2;
            char        op;
            BigInt      result, rhs;
    
    		//read the first value  
    		ifs >> operand1;
    
    		result = BigInt(operand1);
    
    	   /*//read operator and operand
    	    ifs >> op >> operand2;
    
    		//process all dat in file
            while ( !ifs.eof() )
            {
                rhs = BigInt(operand2);
    			//what operation is needed?
                if ( op == '-' )
                    result = result - rhs;
                else if ( op == '+' )
                    result = result + rhs;
    
    			//print the result of the operation
                cout << result << endl;
    
    			//next opeator and operand from he file
                ifs >> op >> operand2;
            }*/
    
    		//no more data
            ifs.close();
    
            return 0;
        }
    }

    BigInt.h
    Code:
    #ifndef _BIGINT_H_
    #define _BIGINT_H_
    
    #include <iostream>
    #include <string>
    
    class BigInt
    {
    public:
        //create a list to represent integer +0
        BigInt(void);
    
        //constructor: create a list to represent integer sepecified by the string bigintstr
        // assume bigintstr is a string consists of digits, and may start with + or - to
        // indicate the sign of the integer.
        BigInt(std::string bigintstr);
    
        //copy constructor
        BigInt( const BigInt& );
    
        //destructor
        ~BigInt(void);
    
        //overloaded + operator: it calculates and returns the total of "current" integer with
        //the integer represented by rhs.
        BigInt operator + (const BigInt& rhs) const;
    
        //overloaded + operator: it calculates and returns the difference between "current" integer 
        //and the integer represented by rhs.
        BigInt operator - (const BigInt& rhs) const;
    
        //////////////////////////////////////////////////////////////////////////////////////
        // The implementation of the following two functions are provided by the instructor.//
        //////////////////////////////////////////////////////////////////////////////////////
    
        // Assignment operator. The implementation of this function is provided by the instructor
        BigInt& operator = (const BigInt& rhs);
    
        //overloaded << operator as global function
        friend std::ostream& operator << (std::ostream& os, const BigInt& rhs );
    
    private:
        //Calculate the addition of two integers represented by lhs and rhs.
        //The calculation will ignore the signs of both lhs and rhs.
        //The return result will be a positive number.
        //precondition: lhs and rhs are not 0
        //               The absolute value of lhs is not smaller than 
        //               the absolute value of rhs.
        BigInt add(const BigInt& lhs, const BigInt& rhs) const;
    
        //Calculate the difference of two integers represented by lhs and rhs, i.e. lhs - rhs
        //The calculation will ignore the signs of both lhs and rhs.
        //The return result will be a positive number.
        //precondition: lhs and rhs are not 0
        //              The absolute value of lhs is not smaller than 
        //              the absolute value of rhs.
        BigInt subtract(const BigInt& lhs, const BigInt& rhs) const;
    
    
    private:
        struct ListNode
        {
            int         digit;
            ListNode*   next;
        };
        /*The signum of this BigInteger: -1 for negative, 0 for zero, or
         * 1 for positive. Note that the BigInteger zero <i>must</i> have
         * a signum of 0. This is necessary to ensures that there is exactly one
         * representation for each BigInteger value. */ 
        int         signum; 
        ListNode    *headPtr;   //the header pointer to the list
    
    private:
        ///////////////////////////////////////////////////////////////////////////
        //The following three functions are helper functions. Instructor provides//
        //the implementation of them.                                            // 
        ///////////////////////////////////////////////////////////////////////////
        //compare the absolute values of lhs and rhs
        //return 0 if |lhs| = |rhs|
        //return -1 if |lhs| < |rhs|
        //return 1 if |lhs| > |rhs|
        int compareAbsoluteValue(const BigInt& rhs) const;
    
        //Make a deep copy of the list org, and returned the duplicated list 
        //through the parameter copy
        void copyList(const ListNode *org, ListNode *&copy);
    
        //Destroy the list org
        void destroyList(ListNode *& org);
    };
    
    #endif
    BigInt.cpp
    Code:
    #include "BigInt.h"
    
    using namespace std;
    
    //create a list to represent integer +0
    BigInt::BigInt(void) 
    {
    	headPtr = new ListNode;           
    	headPtr->digit = 0;        		
    	headPtr->next = NULL;
    }
    
    //constructor: bigintstr represents the big integer
    BigInt::BigInt(std::string bigintstr) 
    {
    	int size = bigintstr.length();
    	int count = 0;
    	
    	// Store each digit of the string in the struct
    	while (count < size-1)
    	{
    	cout << bigintstr[count] << endl;
    	count++;
    	}
    
    }
    
    
    //copy constructor
    BigInt::BigInt( const BigInt& rhs )
    {
    	//need to complete this
    	//you may delete statements in this block and write your own
    }
    
    
    //destructor
    BigInt::~BigInt(void)
    {
        destroyList( headPtr );
    }
    
    void BigInt::copyList(const ListNode *org, ListNode *&copy)
    {
       if (org == NULL)
          copy = NULL;  // original list is empty
       else
       {  // copy first node
          copy = new ListNode;
          copy->digit = org->digit;
          copy->next = NULL;
    
          // copy rest of list
          ListNode *newPtr = copy;  // new list pointer
          // newPtr points to last node in new list
          // origPtr points to nodes in original list
          for ( ListNode *origPtr = org->next;
    	        origPtr != NULL;
    	        origPtr = origPtr->next)
          {  
            newPtr->next = new ListNode;
            newPtr = newPtr->next;
    	    newPtr->digit = origPtr->digit;
            newPtr->next = NULL;
          }  // end for
       }  // end if
    }
    
    void BigInt::destroyList(ListNode *& org)
    {
        while( org != NULL )
        {
            ListNode    *second = org->next;
    
            org->next = NULL;
            delete org;
            org = second;
        }
    }
    
    BigInt& BigInt::operator = (const BigInt& rhs)
    {
        if ( this != &rhs )
        {
            destroyList( headPtr );
            signum = rhs.signum;
            copyList(rhs.headPtr, headPtr);
        }
        return *this;
    }
    
    //compare the absolute value of lhs and rhs
    //return 0 if lhs = rhs
    //return -1 if lhs < rhs
    //return 1 if lhs > rhs
    int BigInt::compareAbsoluteValue(const BigInt& rhs) const
    {
        string  lhsVal;
        string  rhsVal;
    
        if ( signum == 0 )
            lhsVal = "0";
        else
            for(ListNode    *lhsPtr = headPtr; lhsPtr != NULL; lhsPtr=lhsPtr->next)
                lhsVal.insert(0, string(1, char(lhsPtr->digit + '0'))); 
    
        if ( rhs.signum == 0 )
            rhsVal = "0";
        else
            for(ListNode    *rhsPtr = rhs.headPtr; rhsPtr != NULL; rhsPtr=rhsPtr->next)
                rhsVal.insert(0, string(1, char(rhsPtr->digit + '0'))); 
    
        if ( lhsVal.length() < rhsVal.length() )
            return -1;
        else if ( lhsVal.length() > rhsVal.length() )
            return 1;
        else
            return lhsVal.compare( rhsVal );
    }
        
    
    //Addition of two positive numbers, and return the result
    //We do addition assuming that two numbers are positive
    BigInt BigInt::add(const BigInt& large, const BigInt& small) const
    {
        BigInt      result(large);
    
    	//need to complete this
    	//you may delete statements in this block and write your own
    
     
        return result;
    }
    
    //Subtraction of two positive numbers, and return the result
    //We do subtraction assuming that two numbers are positive
    BigInt BigInt::subtract(const BigInt& large, const BigInt& small) const
    {
        BigInt      result(large);
    
    	//need to complete this
    	//you may delete statements in this block and write your own
    
     
        return result;
    }
    
    BigInt BigInt::operator + (const BigInt& rhs) const
    {
        BigInt      result;
    
    	//need to complete this
    	//you may delete statements in this block and write your own
    
     
        return result;
    }
    
    BigInt BigInt::operator - (const BigInt& rhs) const
    {
        BigInt      result;
    
    	//need to complete this
    	//you may delete statements in this block and write your own
    
     
        return result;
    }
    
    std::ostream& operator << (std::ostream& os, const BigInt& rhs )
    {
        string  rhsVal;
    
        if ( rhs.signum == 0 )
            rhsVal = "0";
        else
            for(BigInt::ListNode    *rhsPtr = rhs.headPtr; rhsPtr != NULL; rhsPtr=rhsPtr->next)
                rhsVal.insert(0, string(1, char(rhsPtr->digit + '0'))); 
    
        if ( rhs.signum < 0 )
            rhsVal.insert(0, "-");
    
        os << rhsVal;
    
        return os;
    }
    data.txt
    Code:
      11111111111134523452345
    +
      2222222222222345345435
    +
     +333333333333345345345345
    +
     -12312343545523456789123450
    +
      999999999923445
    -
      9472648234523324
    -
     -32984583934534563734772986465
    -
     -5720349374435435
    -
     +439859358439058908439058904358349085458430859

  2. #2
    Registered User
    Join Date
    Sep 2009
    Location
    Murfreesboro, TN
    Posts
    5
    And I was just messing around with the constructor where I have:

    Code:
    {
    	int size = bigintstr.length();
    	int count = 0;
    	
    	// Store each digit of the string in the struct
    	while (count < size-1)
    	{
    	cout << bigintstr[count] << endl;
    	count++;
    	}
    
    }
    I guess you could say this constructor is what I need help with. I've never really understood linked lists.

    I was thinking something like:

    Code:
    //constructor: bigintstr represents the big integer
    BigInt::BigInt(std::string bigintstr) 
    {
    	int size = bigintstr.length();
    	int count = 0;
    	ListNode *bigInt, *p;
    	bigInt = new ListNode;
    	p = new ListNode;
    	
    	// Store each digit of the string in the struct
    	bigInt->digit = bigintstr[count];
    	bigInt->next = NULL;
    
    	while (count < size-1)
    	{
    		count++;
    		p->digit = bigintstr[count];
    		p->next = bigInt;
    		bigInt = p;
    	}
    
    }
    But this causes the program to crash... any suggestions?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-17-2005, 07:31 AM
  2. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  3. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  4. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  5. structures fooloing to look like classes
    By samsam1 in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2003, 11:43 AM