data structure lab 1

ahmeddxn7
CSC240_LargeInteger.zip

CSC240_LargeInteger/.cproject

CSC240_LargeInteger/.project

CSC240_LargeInteger org.eclipse.cdt.managedbuilder.core.genmakebuilder clean,full,incremental, org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder full,incremental, org.eclipse.cdt.core.cnature org.eclipse.cdt.core.ccnature org.eclipse.cdt.managedbuilder.core.managedBuildNature org.eclipse.cdt.managedbuilder.core.ScannerConfigNature

CSC240_LargeInteger/.settings/language.settings.xml

CSC240_LargeInteger/.settings/org.eclipse.cdt.managedbuilder.core.prefs

eclipse.preferences.version=1 environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/CPATH/delimiter=; environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/CPATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/CPLUS_INCLUDE_PATH/delimiter=; environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/CPLUS_INCLUDE_PATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/C_INCLUDE_PATH/delimiter=; environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/C_INCLUDE_PATH/operation=remove environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/append=true environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/appendContributed=true environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/LIBRARY_PATH/delimiter=; environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/LIBRARY_PATH/operation=remove environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/append=true environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.382811594/appendContributed=true

CSC240_LargeInteger/Debug/CSC240_LargeInteger.exe

CSC240_LargeInteger/Debug/src/LargeIntDriver.o

CSC240_LargeInteger/src/LargeInt.h

#ifndef LARGEINT_H #define LARGEINT_H // This file contains the coded functions from the text. // The implementations are incomplete. #include "SpecializedList.h" // Gain access to SpecializedList #include <fstream> #include <string> #include <iostream> using namespace std; enum SignType {PLUS, MINUS}; enum RelationType {LESS, GREATER, EQUAL}; class LargeInt { public: LargeInt(); LargeInt(string); bool operator<(LargeInt second); bool operator==(LargeInt second); LargeInt operator+(LargeInt second); LargeInt operator-(LargeInt second); LargeInt operator*(LargeInt second); void InsertDigit(int); friend ostream& operator<<(ostream&, LargeInt&); private: SpecializedList number; SignType sign; int numDigits; }; LargeInt::LargeInt(){ sign = PLUS; numDigits = 0; } LargeInt::LargeInt(string num){ //number = new SpecializedList(); sign = PLUS; int firstDigitPosition; // Position of first digit in intString int lastDigitPosition; // Position of last digit in intString // Used to translate character to byte char digitChar; int digitInt; unsigned char digitByte; firstDigitPosition = 0; if (num[0] == '+'){ // Skip leading plus sign firstDigitPosition = 1; sign = PLUS; } else if (num[0] == '-') // Handle leading minus sign { firstDigitPosition = 1; sign = MINUS; } lastDigitPosition = num.size() - 1; for (int count = firstDigitPosition; count <= lastDigitPosition; count++) { digitChar = num[count]; digitInt = digitChar - '0'; digitByte = (unsigned char)digitInt; InsertDigit(digitByte); } } void Add(SpecializedList first, SpecializedList second, SpecializedList& result) // Post: result = first + second. { int carry = 0; bool finished1 = false; bool finished2 = false; int temp; int digit1; int digit2; first.ResetBackward(); second.ResetBackward(); while (!finished1 && !finished2) { first.GetPriorItem(digit1, finished1); second.GetPriorItem(digit2, finished2); temp = digit1 + digit2 + carry; carry = temp / 10; result.PutFront(temp % 10); } while ( !finished1) {// Adds remaining digits (if any) in first to the sum. first.GetPriorItem(digit1, finished1); temp = digit1 + carry; carry = temp / 10; result.PutFront(temp % 10); } while ( !finished2) {// Adds remaining digits (if any) in second to the sum. second.GetPriorItem(digit2, finished2); temp = digit2 + carry; carry = temp / 10; result.PutFront(temp % 10); } if (carry != 0) // Adds in carry (if any) result.PutFront(carry); } void Sub(SpecializedList first, SpecializedList second, SpecializedList& result) // Post: result = first - second. { } //IT 5/6/2019 LargeInt LargeInt::operator*(LargeInt second){ SignType selfSign; SignType secondSign; LargeInt result; return result; } LargeInt LargeInt::operator+(LargeInt second) // self is first operand { SignType selfSign; SignType secondSign; LargeInt result; if (sign == second.sign) { Add(number, second.number, result.number); result.sign = sign; } else { selfSign = sign; secondSign = second.sign; sign = PLUS; second.sign = PLUS; if (*this < second) { Sub(second.number, number, result.number); //NOT WORKING!!! result.sign = secondSign; } else if (second < *this) { Sub(number, second.number, result.number); //NOT WORKING!!! result.sign = selfSign; } sign = selfSign; } return result; } RelationType CompareDigits(SpecializedList first, SpecializedList second) { bool finished = false; int digit1; int digit2; first.ResetForward(); second.ResetForward(); while ( !finished) { first.GetNextItem(digit1, finished); second.GetNextItem(digit2, finished); if (digit1 < digit2) return LESS; if (digit1 > digit2) return GREATER; } return EQUAL; } bool LargeInt::operator<(LargeInt second) { RelationType relation; if (sign == MINUS && second.sign == PLUS) return true; else if (sign == PLUS && second.sign == MINUS) return false; else if (sign == PLUS && numDigits < second.numDigits) return true; else if (sign == PLUS && numDigits > second.numDigits) return false; else if (sign == MINUS && numDigits > second.numDigits) return true; else if (sign == MINUS && numDigits < second.numDigits) return false; else{ // Must compare digit by digit relation = CompareDigits(number, second.number); if (sign == PLUS && relation == LESS){ return true; } else if (sign == PLUS && relation == GREATER){ return false; } else if (sign == MINUS && relation == GREATER){ return true; } else{ return false; } } } void LargeInt::InsertDigit(int digit){ number.PutEnd(digit); } ostream& operator<<(ostream& out, LargeInt& largeInt){ int digit; bool flag = true; string largeIntString; if (largeInt.sign == PLUS) largeIntString = "+"; else largeIntString = "-"; int length; char d; length = largeInt.number.GetLength(); largeInt.number.ResetForward(); for (int count = length; count >= 1; count--) { largeInt.number.GetNextItem(digit, flag); d = '0' + digit; largeIntString += d; if ((((count - 1) % 3) == 0) && (count != 1)) largeIntString = largeIntString + ","; } out << largeIntString; return out; } #endif

CSC240_LargeInteger/src/LargeIntDriver.cpp

CSC240_LargeInteger/src/LargeIntDriver.cpp

/*
 * LargeIntDriver.cpp
 *  Test the functionality of the LargeInt class.
 *  Test and implement the multiply function.
 *  Created on: Mar 6, 2019
 *      Author: itemesva
 */
#include   "LargeInt.h"
#include   < iostream >
using   namespace  std ;
int  main (){
     LargeInt  num1 ( "123456789" );
     LargeInt  num2 ( "-123456789" );
    cout  <<  num1  <<  endl ;
    cout  <<  num2  <<  endl ;
     //LargeInt sum = num1 + num1;
     LargeInt  sum  =  num1  +  num2 ;
    cout  <<  sum  <<  endl ;
     //LargeInt product = num1*num2;
     //cout << product << endl;
     return   0 ;
}



CSC240_LargeInteger/src/SpecializedList.h

#ifndef SPECIALIZED_LIST_H #define SPECIALIZED_LIST_H // Specialized List that goes in both directions struct NodeType; #include <cstddef> class SpecializedList { public: SpecializedList(); // Class constructor // ~SpecializedList(); // Class destructor // SpecializedList(const SpecializedList& someList); // Copy constructor void ResetForward(); // Initializes current position for an iteration // through the list from first item to last item. void GetNextItem(int& item, bool& finished); // Gets the next item in the structure. // finished is true if all the items have been accessed. // GetNextItem and GetPriorItem are independent; a forward // iteration and a backward iteration may be in progress // at the same time. void ResetBackward(); // Initializes current position for an iteration // through the list from last item to first item. void GetPriorItem(int& item, bool& finished); // Gets the previous item in the structure. // finished is true if all the items have been accessed. void PutFront(int item); // Inserts item as the first item in the structure. void PutEnd(int item); // Inserts item as the last item in the structure. int GetLength() const; // Returns the number of items in the structure. private: NodeType* list; NodeType* currentNextPos; NodeType* currentBackPos; int length; }; struct NodeType { NodeType* next; NodeType* back; int info; }; SpecializedList::SpecializedList() { length = 0; list = NULL; } void SpecializedList::ResetForward() // Post: currentNextPos has been initialized for a forward // traversal. { currentNextPos = NULL; } void SpecializedList::GetNextItem(int& item, bool& finished) // Pre: ResetForward has been called before the first call to // this function. // Post: item is a copy of the next item in the list. // finished is true if item is the last item in the list; // false otherwise. { if (currentNextPos == NULL) currentNextPos = list->next; else currentNextPos = currentNextPos->next; item = currentNextPos->info; finished = (currentNextPos == list); } void SpecializedList::ResetBackward() // Post: currentBackPos has been initialized for a backward // traversal. { currentBackPos = NULL; } void SpecializedList::GetPriorItem(int& item, bool& finished) // Post: item is a copy of the previous item in the list. // finished is true if item is the first item in the list; // false otherwise. { if (currentBackPos == NULL) currentBackPos = list; else currentBackPos = currentBackPos->back; item = currentBackPos->info; finished = (currentBackPos == list->next); } void SpecializedList::PutFront(int item) // Post: item has been inserted at the front of the list. { NodeType* newNode; newNode = new NodeType; newNode->info = item; if (list == NULL) { // list is empty. newNode->back = newNode; newNode->next = newNode; list = newNode; } else { newNode->back = list; newNode->next = list->next; list->next->back = newNode; list->next = newNode; } length++; } void SpecializedList::PutEnd(int item) // Post: item has been inserted at the end of the list. { PutFront(item); list = list->next; } int SpecializedList::GetLength() const{ return length; } #endif