Turning numbers into vectors; Passing vector arguments

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 920 Views - Last Post: 22 February 2012 - 12:12 PM Rate Topic: -----

#16 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

Re: Turning numbers into vectors; Passing vector arguments

Posted 21 February 2012 - 09:35 PM

Here is the code that finally does everything it is supposed to! Thanks for all your help!

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include "HugeInteger.h"
using namespace std;

// helper function for HugeInteger::HugeInteger( unsigned long num )
string intToStr( unsigned long num )
{
	ostringstream result;
	result << num;
	return result.str();
}

// constructor for string input
HugeInteger::HugeInteger( const string &str )
{
	number.resize( maxSize );

	for( int index = 0; index < str.size(); ++index )
	{
		string s = str.substr( ((str.size() - 1) - index), 1 );
		int n = atoi( s.c_str() );
		number.at( str.size() - 1 ) = n;
	}
	reverse( number.begin(), number.end() );
}

// constructor for unsigned long input
HugeInteger::HugeInteger( unsigned long num )
{
	string wasNumber = intToStr( num );

	number.resize( maxSize );
	int count = 0;
	for( int index = wasNumber.size() - 1; index >= 0; --index )
	{
		number.at( (wasNumber.size() - 1) - index ) = num % 10;
		num /= 10;
		++count;
	}
	reverse( number.begin(), number.end() );
}

// constructor for vector inout
HugeInteger::HugeInteger( const vector< int > &vector )
{
	number.resize( maxSize );
	number = vector;
}

// constructor for empty input
HugeInteger::HugeInteger()
{
	number.resize( maxSize );
}

// manipulation and comparison functions
bool HugeInteger::isZero() const
{
	for( int index = 0; index < this->number.size(); ++index )
	{
		if( this->number.at( index ) != 0 )
		{
			return false;
		}
	}

	return true;
}// end bool HugeInteger::isZero() const

// addition
HugeInteger HugeInteger::add( const HugeInteger &arg )
{
	vector< int > result( maxSize );

	for( int index = (arg.number.size() - 1); index >= 0; --index )
	{
		result.at( index ) = ((this->number.at( index )) + (arg.number.at( index )));
		if( result.at( index ) > 9 )
		{
			result.at( index ) -= 10;
			result.at( index - 1 ) += 1;
		}
	}
	HugeInteger::HugeInteger( result );
	return result;
}

// subtraction
HugeInteger HugeInteger::subtract( const HugeInteger &arg )
{
	vector< int > result( maxSize );
	vector< int > tempVector = this->number;

	for( int index = (tempVector.size() - 1 ); index >= 0; --index )
	{
		result.at( index ) = tempVector.at( index ) - arg.number.at( index );
		if( result.at( index ) < 0 )
		{
			int temp = index;

			result.at( index ) += 10;
			do
			{
				tempVector.at( temp - 1 ) += 9;
				--temp;
			}while( tempVector.at( temp - 1) == 0 );
			tempVector.at( temp - 1 ) += (9 - tempVector.at( temp - 1 ));
		}
	}
	HugeInteger::HugeInteger( result );
	return result;
}

// compares ==
bool HugeInteger::isEqualTo( const HugeInteger &arg ) const
{
	if( this->number == arg.number )
	{
		return true;
	}
	else
	{
		return false;
	}
}

// compares !=
bool HugeInteger::isNotEqualTo( const HugeInteger &arg ) const
{
	if( this->number != arg.number )
	{
		return true;
	}
	else
	{
		return false;
	}
}

// compares >
bool HugeInteger::isGreaterThan( const HugeInteger &arg ) const
{
	if( this->number > arg.number )
	{
		return true;
	}
	else
	{
		return false;
	}
}

// compares <
bool HugeInteger::isLessThan( const HugeInteger &arg ) const
{
	if( this->number < arg.number )
	{
		return true;
	}
	else
	{
		return false;
	}
}

// compares >=
bool HugeInteger::isGreaterThanOrEqualTo( const HugeInteger &arg ) const
{
	if( this->number >= arg.number )
	{
		return true;
	}
	else
	{
		return false;
	}
}

// compares <=
bool HugeInteger::isLessThanOrEqualTo( const HugeInteger &arg) const
{
	if( arg.number <= this->number )
	{
		return true;
	}
	else
	{
		return false;
	}
}

// Optional functions
/*
void HugeInteger::multiply(  )
{
}
void HugeInteger::divide(  )
{
}
void HugeInteger::modulus(  )
{
}
*/

// output function
void HugeInteger::output() const
{
	for( int index = 0; index < number.size(); ++index )
	{
		cout << number[ index ];
	}
}

Was This Post Helpful? 0
  • +
  • -

#17 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4892
  • View blog
  • Posts: 11,288
  • Joined: 16-October 07

Re: Turning numbers into vectors; Passing vector arguments

Posted 22 February 2012 - 05:39 AM

Hmm... your string constructor doesn't seem to work. Did you test it?

I tested with this:
HugeInteger("123456").output(); cout << endl;
HugeInteger(123456).output(); cout << endl;



And got:
0000000000000000000000000000000000100000
0000000000000000000000000000000000123456



The long with a string conversion? And what the hell is going on with your string? Why reverse?

Long is simple, but you've made it complex. Let's start there:
HugeInteger::HugeInteger( unsigned long num ) {
	number.resize( maxSize );  // fine.  Ugly, but fine
	int pos = maxSize-1; // begin at the end
	while(num>9) { // until it's less than 10 we have to whittle it down
		number[pos--] = num % 10; // take off the tail with mod
		num /= 10; // shorten the number
	}
	number[pos] = num; // we're here, num is < 10 and pos is in the right place
}



Given the above, string isn't drastically different. If you want to use STL methods like reverse, get to know the string methods. We can iterate over the string in the opposite direction...
HugeInteger::HugeInteger( const string &str ) {
	number.resize( maxSize );
	int pos = maxSize-1;
	for ( string::const_reverse_iterator it=str.rbegin() ; it != str.rend(); ++it) {
		int n = *it - '0'; // ascii to int conversion
		// if it's crap, skip it
		if (n>=0 && n<10) { number[pos--] = n; }
	}
}



Good luck.
Was This Post Helpful? 0
  • +
  • -

#18 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

Re: Turning numbers into vectors; Passing vector arguments

Posted 22 February 2012 - 10:59 AM

View Postbaavgai, on 22 February 2012 - 05:39 AM, said:

Hmm... your string constructor doesn't seem to work. Did you test it?

I only tested it with the main.cpp that I have posted. The only string given is "10000000000000"...so it seemed to work fine :/



View Postbaavgai, on 22 February 2012 - 05:39 AM, said:

The long with a string conversion? And what the hell is going on with your string? Why reverse?

Long to string: Before I thought to use %, converting to a string seemed the easiest thing. Once I realized I should use %, I was already so entrenched in my idea of using a string conversion that I never thought to do it differently. Reversal: That was the only way I could figure out to get it to store in manner that would make the addition/subtraction/comparisons/etc. to work correctly.



View Postbaavgai, on 22 February 2012 - 05:39 AM, said:

Long is simple, but you've made it complex. Let's start there:
HugeInteger::HugeInteger( unsigned long num ) {
	number.resize( maxSize );  // fine.  Ugly, but fine
	int pos = maxSize-1; // begin at the end
	while(num>9) { // until it's less than 10 we have to whittle it down
		number[pos--] = num % 10; // take off the tail with mod
		num /= 10; // shorten the number
	}
	number[pos] = num; // we're here, num is < 10 and pos is in the right place
}


Making the vectors all maxSize is horribly ugly....but that's what the teacher wanted. As for the rest of my method...I realize now that I was trying to integrate a past idea with the idea that your code here shows. It seems obvious now, but at the time I was stuck in my earlier bad idea.



View Postbaavgai, on 22 February 2012 - 05:39 AM, said:

Given the above, string isn't drastically different. If you want to use STL methods like reverse, get to know the string methods. We can iterate over the string in the opposite direction...
HugeInteger::HugeInteger( const string &str ) {
	number.resize( maxSize );
	int pos = maxSize-1;
	for ( string::const_reverse_iterator it=str.rbegin() ; it != str.rend(); ++it) {
		int n = *it - '0'; // ascii to int conversion
		// if it's crap, skip it
		if (n>=0 && n<10) { number[pos--] = n; }
	}
}


I didn't know string could reverse itself. When I googled vector reversing (which was my end-state goal) the method that I used was the recommended way. As for the rest of my string constructor...well I was on a time crunch and that was the only way I could figure it out. :)


Thanks for all your help. I know I am a novice programmer and make a lot of things look ugly and complicated, so thank you for taking the time to look at my stuff and teach me how to make it better!
Was This Post Helpful? 0
  • +
  • -

#19 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: Turning numbers into vectors; Passing vector arguments

Posted 22 February 2012 - 11:52 AM

View Postbaavgai, on 20 February 2012 - 07:47 AM, said:

For the isZero, I'd check for empty too:
bool HugeInteger::isZero() const {
	if (number.size()==0) { return true; }
	return ( (number.size() == 1) && (number.at( 0 ) == 0) );
}


I just want to add that there is another way to check if the string is empty and in my opinion it's easier to read the code and see what it is supposed to be doing:
bool HugeInteger::isZero() const {
//	if (number.size()==0) { return true; }
        if(number.empty()) { return true; }
	return ( (number.size() == 1) && (number.at( 0 ) == 0) );
}


Pretty much all of the standard C++ container classes(including string) have the member function empty. Don't get me wrong, the way baavgai did it is fine, I just wanted to point out this way.

*EDIT*: Changed the if-statement due to logic error.

This post has been edited by vividexstance: 22 February 2012 - 12:42 PM

Was This Post Helpful? -1
  • +
  • -

#20 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Turning numbers into vectors; Passing vector arguments

Posted 22 February 2012 - 12:12 PM

I think you meant:
        if(number.empty()) { return true; }


Was This Post Helpful? -1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2