PDA

View Full Version : C++ project



HardRockers
03-30-2012, 02:37 PM
ok, so I have a project where I am supposed to take 2 Large numbers and add, subtract, and multiply them together, these large numbers need to be stored in arrays, like every digit gets its own spot in the array. I have the addition figured out(After a hell of a time) but I am having a lot of trouble with subtraction and multiplication. The overloading and having to carry digits over is what really kills ya.




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

char get_menu_choice( char & choice );
int get_number( int arr[] );
int add( int entry1[ ], int entry2[ ], int total[] );
void mult( int entry1[ ], int entry2[ ], int total[ ], int arr4[] );
void clear( int arr[] );
void show_right( int arr[ ], int count );
void overflow( int entry1[], int entry2[], int total[], int counter1, int counter2 );

int const size = 8;

int main ()
{
char choice;
int carry[2 * size] = {0};
int array1[size] = {0};
int array2[size] = {0};
int ans[size] = {0};
int m_ans[2 * size] = {0};
int count = 0;
int count1 = 0;
int count2 = 0;
int i = 0;

do
{
get_menu_choice( choice );

switch( choice )
{
case 'A':
cin.ignore();

count1 = get_number( array1 );
count2 = get_number( array2 );
add( array1, array2, ans );

cout << "The result of Adding " << endl;
show_right( array1, count1 );
show_right( array2, count2 );

cout << "________________________________________" << endl;

overflow( array1, array2, ans, count1, count2 );

clear( array1 );
clear( array2 );
clear( ans );

break;
case 'S':
cout << endl;
break;
case 'M':
cin.ignore();

count1 = get_number( array1 );
count2 = get_number( array2 );
mult( array1, array2, m_ans, carry );
cout << endl;

clear( array1 );
clear( array2 );
clear( ans );
break;
case 'X':
cout << "Exiting Program. Goodbye! \n " << endl;
break;
default:
cout << choice << " is not a valid option. \n " << endl;
cout << endl;
}
}while( choice != 'X');

return 0;
}

char get_menu_choice( char & choice )
{

cout << "*** Mines Big Number Calculator *** \n " << endl;
cout << "A. Add two numbers " << endl;
cout << "S. Subtract two numbers " << endl;
cout << "M. Multiply two numbers " << endl;
cout << "X. Exit Program \n" << endl;
cout << "Your choice: " ;
cin >> choice;

choice = toupper( choice );

return choice;

}

int get_number( int arr[] )
{
int i = 0;
int x = 0;
char num = 0;
int num_value;
int temp_arr[size] = {0};
int counter = 0;
cout << "Enter a number, up to " << size << " digits. " << endl;

while( num != '\n' && i < size )
{
cin.get( num );
if( num < '0' || num > '9' )
{
continue;
}

num_value = num - '0';
temp_arr[i] = num_value;

i++;
counter++;
}

for( x = 0; x < counter; x++ )
{
arr[size - counter + x] = temp_arr[x];
}
cout << counter;
cout << endl;

return counter;

}

int add( int entry1[ ], int entry2[], int total[] )
{
int carry[size] = {0};
int sum;
int i;
int x;
int countx = 1;

for( i = 1; i <= size; i++ )
{
sum = entry1[size - i] + entry2[ size - i] + carry[size - i];
if( sum >= 10)
{
total[size - i] = sum - 10;
carry[size - i - 1] = 1;

}
else
{
total[size - i] = sum;
}


countx++;
}
cout << endl;

return countx;
}

void mult(int entry1[], int entry2[], int total[], int arr4[])
{
int i = 0;
int count = 0;
int mult;
int x;

for( i = 1; i <= size; i++ )
{
mult = ( entry1[size - i] * entry2[size - i] ) + arr4[size - i];
arr4[2 * size - i -1] = mult / 10;
total[2 * size - i] = mult % 10;
}

cout << "The result of multiplying " << endl;

for( i = 0; i < size; i++ )
{
cout << entry1[i];
}

cout << endl;

for( i = 0; i < size; i++ )
{
cout << entry2[i];
}

cout << "____________________________________________" << endl;

for( x = 0; x < size * 2; x++)
{
cout << total[x];
}
}

void clear( int arr[] )
{
int i;

for( i = 0; i < size; i++ )
{
arr[i] = 0;
}
}

void show_right( int arr[], int count )
{
int i = 0;

cout << setw( 40 - count );

for( i = 0; i < count; i++ )
{
cout << arr[size - count + i];
}
cout << endl;
}

void overflow( int entry1[], int entry2[], int total[], int counter1, int counter2 )
{
if( counter2 < counter1)
{
show_right( total, counter1 );
if( add( entry1, entry2, total ) == 1)
cout << "Overflow occured, result is incomplete. " << endl;
}
else if( counter2 > counter1 )
{
show_right( total, counter2);
if ( add( entry1, entry2, total ) == 1 )
cout << "Overflow occured, result is incomplete. " << endl;
}
else
{
show_right( total, counter1 );
if( add( entry1, entry2, total ) == 1)
cout << "Overflow occured, result is incomplete. " << endl;
}

}