PROBLEM: ADDITION USING CHARACTER ARRAY OF ANY LENGTH.
Summery:
This program uses Adder Class to do the addition operation. It accepts the character from user and manipulates the character using ASCII to convert it to numeric and perform the addition operation and again gives out the equivalent output in characters. This program can add two numbers of any sizes. The Joe Programmer enjoys the facility of object, as he can use the class's public method called Adjust_Addends_Size() and type in any number in the parenthesis to get the sum of that many numbered Addends. This is done by dynamically allocating the memory space by the class Adder. Hence, when a Joe Programmer creates an object of the class then they can just call this method to set the addends for sum. The additions of two 20-digit numbers are performed using the default constructor as well as the constructor which takes two arguments. Using the default constructor, we have to set the addends by calling setAddent_1() and setAddend_2() methods. The object is initiated for the class in a different manner than for the constructor which accepts two arguments. For the constructor, we don't need to set the addends as it is already done in the constructor itself. Hence, this program uses the class and its objects to get the sum of any number of addends very smartly.
Solution: Adder.h
#pragma once
class Adder {
public: // Programmer Interface (programmer can access/use these).
Adder (void); // Default constructor.
Adder (char* , char*); // Constructor
void setAddend_1 (char*);
void setAddend_2 (char*);
void getSum (char*); // To get the sum.
// To display.
void displayAddend_1(char*);
void displayAddend_2(char*);
void setZero(); // To set zero to the addends.
void Adjust_Addends_Size(int); // Dynammic Allocation of memory as programmer
// requests.
void toNumericChar(char *); // Convert any non numeric characters to Zero.
~Adder (void); // Destructor.
private: // Data members not accessible by programmer.
char* Addend_1; // Addend one.
char* Addend_2; // Addend Two.
char* Sum; // Sum.
int maxSize; // Maximum size allowed.
};
Solution: Adder.cpp
#include
#include iostream
#include iomanip
#include cctype
#include cstring
#include fstream
#include cmath
#include ctime
#include cstdlib
#include utility
#include conio.h
#include "Adder.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
const int MAX = 20; // Maximum number of Addends.
const int MIN = 2; // Minimun number of Addends.
const int MAX_ARRAY = 150; // Setting maximum Array size.
//---------------------------------------------------------------------------
// Define member functions for class Adder.
Adder::Adder (void) { // Default constructor.
maxSize = MAX;
// Dynamically Allocating Memory.
Addend_1 = new char [maxSize];
Addend_2 = new char [maxSize];
Sum = new char [maxSize + 1];
setZero(); // Setting Addends value to zero for the time being.
}
//---------------------------------------------------------------------------
Adder::Adder (char* addend_1, char* addend_2) { // Constructor
maxSize = MAX;
// Dynamically Allocating Memory.
Addend_1 = new char [maxSize];
Addend_2 = new char [maxSize];
Sum = new char [maxSize + 1];
setZero(); // Setting Addends value to zero for the time being.
// Setting Addents to convert any non-numeric characters to zero, right
// Justified, and filling every element of the array as zero and assigning
// value to the addends.
setAddend_1 (addend_1);
setAddend_2 (addend_2);
}
//---------------------------------------------------------------------------
void Adder::setAddend_1 (char* addend_1) {
// Convert any non-numeric characters to zero, Right
// justified, and filling every element of the array as zero and assigning
// value to the addends.
if (strlen(addend_1) > maxSize) {
addend_1[maxSize] = '\0'; // If the user inputs more characters than
// maxSize then it ignores it.
}
toNumericChar(addend_1); // Converts any non-numeric characters to zero.
for (int i = 0; i < maxSize; ++i) {
Addend_1[i] = '0';
}
// Load and Right Justification.
int last_addend_1_index = strlen(addend_1)- 1;
int last_Addend_1_index = maxSize - 1;
for(int j = last_addend_1_index; j >= 0; --j, --last_Addend_1_index){
Addend_1[last_Addend_1_index] = addend_1[j];
}
}
//---------------------------------------------------------------------------
void Adder::setAddend_2 (char* addend_2) {
// Convert any non-numeric characters to zero, Right
// justified, and filling every element of the array as zero and assigning
// value to the addends.
if (strlen(addend_2) > maxSize) {
addend_2[maxSize] = '\0';
}
toNumericChar(addend_2); // Converts any non-numeric characters to zero.
for (int i = 0; i < maxSize; ++i) {
Addend_2[i] = '0';
}
// Load and Right Justification.
int last_addend_2_index = strlen(addend_2)- 1;
int last_Addend_2_index = maxSize - 1;
for(int j = last_addend_2_index; j >= 0; --j, --last_Addend_2_index){
Addend_2[last_Addend_2_index] = addend_2[j];
}
}
//---------------------------------------------------------------------------
void Adder::getSum(char* sum) {
// Calculates the sum of two character array and stores it to a third
// character array called sum.
int sum_digit;
int carry = 0;
for (int i = maxSize - 1; i >= 0; --i) {
sum_digit = (Addend_1[i] - '0') + (Addend_2[i] - '0') + carry;
carry = sum_digit / 10; // Getting Carry.
sum[i + 1] = (sum_digit % 10) + '0'; // Getting Sum.
}
sum[0] = carry + '0';
char temp[MAX_ARRAY]; // Creating temp Array.
strncpy(temp, sum, maxSize + 1); // Loading the value of Sum in to the temp
// array.
temp[maxSize + 1] = '\0'; // Putting string terminator for Sum.
strcpy(sum, temp); // Copying the value from temp to Sum.
}
//---------------------------------------------------------------------------
void Adder::displayAddend_1(char* addend_1) {
// Displays the Addend.
for (int i = 0; i < maxSize; ++i) {
cout << Addend_1[i];
}
}
//---------------------------------------------------------------------------
void Adder::displayAddend_2(char* addend_2) {
// Displays the Addend.
for (int i = 0; i < maxSize; ++i) {
cout << Addend_2[i];
}
}
//---------------------------------------------------------------------------
void Adder::setZero(){
// Setting zero the elements of Addends.
for (int i = 0; i < maxSize; ++i){
Addend_2[i] = '0';
Addend_1[i] = '0';
}
}
//---------------------------------------------------------------------------
void Adder::Adjust_Addends_Size(int userSize) {
// It allow the joe programmer to get the sum of any size of the addends.
// Deleting the current value of addends and the sum.
delete [] Addend_1;
delete [] Addend_2;
delete [] Sum;
maxSize = userSize; // Changing the value to the programmer defined number.
if (maxSize < MIN) {
// If the size the programmer inputs is less than MIN then it will set it
// as a MIN.
maxSize = MIN;
}
// Dynamically changing the size of addends to programmer defined size.
Addend_1 = new char [maxSize];
Addend_2 = new char [maxSize];
// Dynamically changing the size of sum as well to reflect the change in
// addends.
Sum = new char [maxSize + 1];
setZero(); // Setting the addends value to zero.
}
//---------------------------------------------------------------------------
void Adder::toNumericChar(char * NonNumericChar) {
// Converting any value user enters except from 0 to 9 to ZERO.
// Validating!
for(int x = 0; x < strlen(NonNumericChar); ++x){
if (!(NonNumericChar[x] >= '0' && NonNumericChar[x] <= '9')) {
NonNumericChar[x] = '0';
}
}
}
//---------------------------------------------------------------------------
Adder::~Adder (void) { // Destructor.
// Deleting the memory space taken as it is no more required.
delete [] Addend_1;
delete [] Addend_2;
delete [] Sum;
}
//---------------------------------------------------------------------------
Solution: Main
// Programming Assignment: # Final Assignment --> Adder.
// This program uses Adder Class to do the addition operation. It accepts the
// character from user and manipulates the character using ASCII to convert it
// to numeric and perform the addition operation and again gives out the
// equivalent output in characters. This program can add two numbers of any size.
// The Joe Programmer has facility, as he can use the class's public method called
// Adjust_Addends_Size() and type in any number in the parenthesis to get the
// sum of that many numbered Addends. This is done by dynamically allocating the
// memory space by the class Adder. Hence, when a Joe Programmer creates an object
// of the class then they can just call this method to set the addends for sum.
// The addition of two 20-digit numbers are performed using the default constructor
// as well as the constructor which takes two argument. Using the default
// constructor, we have to set the addends by calling setAddent_1() and
// setAddend_2() methods. The object is inniciated for the class in a different
// manner than for the constructor which accepts two arguments. For the
// constructor, we don't need to set the addends as it is already done in the
// constructor itself. Hence, this program uses the class and its objects to
// get the sum of any number of addends very smartly.
//------------------------------------------------------------------------------
// Programming Environment: Bloodshed
// Program Description:
// Date/Time: 5/8/2010 6:08:57 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Adder.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
const int MAX = 20; // Constant for Addend.
const int LOCAL_LENGTH = 150; // Maximum array length for user.
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int main (void) {
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
char My_Adder_1[MAX]; // 1st Addend Array with max element 20.
char My_Adder_2[MAX]; // 2nd Addend Array with max element 20.
char My_Sum[MAX + 1]; // Sum is one greater than the addends.
// USING DEFAULT CONSTRUCTOR
cout << endl << "ADDITION OPERATION OF 20 DIGIT NUMBERS Ver. 1.7";
cout << endl << "-----------------------------------------------";
cout << endl << "-----------USING DEFAULT CONSTRUCTOR-----------";
cout << endl << "-----------------------------------------------";
// User input
cout << endl << endl << "Enter First Number to ADD: ";
cin.getline(My_Adder_1, LOCAL_LENGTH);
cout << endl << "Enter Second Number to ADD: ";
cin.getline(My_Adder_2, LOCAL_LENGTH);
// Creating the object using default constructor.
Adder My_Add;
My_Add.setAddend_1(My_Adder_1); // Setting addends.
My_Add.setAddend_2(My_Adder_2);
My_Add.getSum(My_Sum); // Getting Sum.
// Displaying the addends, Sum in organised way!!
cout << endl << endl << setw (10) << "Addend ONE: " << setw (7);
My_Add.displayAddend_1(My_Adder_1);
cout << endl << setw (10) << "Addend TWO: " << setw (7);
My_Add.displayAddend_2(My_Adder_2);
cout << endl << setw (10) << " + " << "--------------------";
cout << endl << setw (10) << " SUM: ";
cout << My_Sum;
// USING CONSTRUCTOR
cout << endl << endl;
cout << endl << "-----------------------------------------------";
cout << endl << "---------------USING CONSTRUCTOR---------------" << endl;
cout << endl << "-----------------------------------------------";
// Getting Input.
cout << endl << endl << "Enter First Number to ADD: ";
cin.getline(My_Adder_1, LOCAL_LENGTH);
cout << endl << "Enter Second Number to ADD: ";
cin.getline(My_Adder_2, LOCAL_LENGTH);
// Creating object from the class Adder using the constructor having
// two parenthesis as two addends.
Adder My_Adder(My_Adder_1, My_Adder_2);
My_Adder.getSum(My_Sum); // Getting Sum.
// Displaying the addends, Sum in organised way!!
cout << endl << endl << setw (10) << "Addend ONE: " << setw (7);
My_Adder.displayAddend_1(My_Adder_1);
cout << endl << setw (10) << "Addend TWO: " << setw (7);
My_Adder.displayAddend_2(My_Adder_2);
cout << endl << setw (10) << " + " << "--------------------";
cout << endl << setw (10) << " SUM: ";
cout << My_Sum;
// USING JOE PROGRAMMER MODIFYING ADDENDS SIZE TO ANY NUMBER.
cout << endl << endl;
cout << endl << "-----------------------------------------------";
cout << endl << " ADDITION OPERATION OF 7 DIGIT NUMBERS ";
cout << endl << " JOE PROGRAMMER MODIFYING ADDEND'S SIZE TO 7 " << endl;
cout << endl << "-----------------------------------------------";
// Taking Input.
cout << endl << endl << "Enter First Number to ADD: ";
cin.getline(My_Adder_1, LOCAL_LENGTH);
cout << endl << "Enter Second Number to ADD: ";
cin.getline(My_Adder_2, LOCAL_LENGTH);
// Creating Object of the class Adder.
Adder My_Adder_7;
// Joe Programmer changing the size of addends to 7. He can type 50 to
// get the sum of 50 digit Addends.
My_Adder_7.Adjust_Addends_Size(7);
// Settting Addends.
My_Adder_7.setAddend_1(My_Adder_1);
My_Adder_7.setAddend_2(My_Adder_2);
// Getting Sum.
My_Adder_7.getSum(My_Sum);
// Displaying the addends & Sum in organised way!!
cout << endl << endl << setw (10) << "Addend ONE: " << setw (7);
My_Adder_7.displayAddend_1(My_Adder_1);
cout << endl << setw (10) << "Addend TWO: " << setw (7);
My_Adder_7.displayAddend_2(My_Adder_2);
cout << endl << setw (10) << " + " << "-------";
cout << endl << setw (10) << " SUM: ";
cout << My_Sum;
cout << endl << endl << "-----------------------------------------------";
cout << endl << "Contact the Programmer, if you want to perform "
<< endl << "specific number of digit's sum.";
cout << endl << "-----------------------------------------------";
cout << endl << "THANK YOU FOR USING THIS PROGRAM! VERSION 1.7";
cout << endl << "-----------------------------------------------";
//---------------------------------------------------------
cout << endl << endl << "\aDepress Any Key to Continue...";
_getch ();
return 0;
}
//------------------------------------------------------------------------
// Function Definitions
//------------------------------------------------------------------------