Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Open the new version of the program AddMoney.cpp (This program can be se

ID: 3723940 • Letter: P

Question

Part 1: Open the new version of the program AddMoney.cpp (This program can be seen below), in which we read the two input values from an input file, in_file.dat, and writes the results into an output file, out_file.dat.

Overload the >> so that writing an object of type AltMoney can be done using >>.  


// lab6_AddMoney_n.cpp: This program adds money of two different people
// It reads the amounts for two people from an
// input file in_file.dat and writes the result into a file out_file.dat

#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;

class AltMoney
{
    public:
        AltMoney();
        friend void read_money(istream& ins, AltMoney& m);
        friend AltMoney operator +(AltMoney m1, AltMoney m2);
        friend void write_money(ofstream& ous, AltMoney m);
    private:
        int dollars;
        int cents;
};

void read_money(istream& ins, AltMoney& m);
void get_streams(ifstream& ins, ofstream& ous);
void write_money(ofstream& ous, AltMoney m);

int main( )
{
     ifstream ins;
    ofstream ous;
     AltMoney m1, m2, sum;

     get_streams(ins, ous);

     read_money(ins, m1);
     ous << "The first money is:";
     write_money(ous, m1);

     read_money(ins, m2);
     ous << "The second money is:";
     write_money(ous, m2);

     sum = m1+m2;
     ous << "The sum is:";
     write_money(ous, sum);

     ins.close();
     ous.close();

     return 0;
}

AltMoney::AltMoney()
{
}

void write_money(ofstream& ous, AltMoney m)
{
     ous << "$" << m.dollars << ".";
     if(m.cents <= 9)
         ous << "0"; //to display a 0 on the left for numbers less than 10
     ous << m.cents << endl;
}

AltMoney operator +(AltMoney m1, AltMoney m2)
{
     AltMoney temp;
     int extra = 0;
     temp.cents = m1.cents + m2.cents;
     if(temp.cents >=100){
         temp.cents = temp.cents - 100;
         extra = 1;
      }
      temp.dollars = m1.dollars + m2.dollars + extra;

      return temp;
}

void read_money(istream& ins, AltMoney& m)
{
     int d, c;
     ins >> d;
     ins >> c;
     if( d < 0 || c < 0)
     {
            cout << "Invalid dollars and cents, negative values ";
            exit(1);
     }
     m.dollars = d;
     m.cents = c;
}

void get_streams(ifstream& ins, ofstream& ous)
{

    ins.open("in_file.dat");
    if(ins.fail())
   {
       cout << "Failed to open the input file. ";
       exit(1);
    }

    ous.open("out_file.dat");
    if(ous.fail())
   {
       cout << "Failed to open the output file. ";
       exit(1);
    }
}

Part 2: Set Class

Define a class for set elements. A set class is used to represent sets of integers. It supports most of the standard set operations. The program will demonstrate overloaded operators, member functions, friend functions and random number generation.


****************************************************************************************

Set data structure:

A set is a collection of objects need not to be in any particular order. Elements

should not be repeated.

Union: Combine two or more sets (here two sets)

Intersection: Gathering common elements in both the sets together as a single set

Difference: Forming a set with elements which are in first set and not in second set

A-B= {x| for all x belongs to A but not in B}

***************************************************************************************

Sample class declaration:

You class should have all of the followings.

Can be defined as friend functions

class Set {

public:

//default constructor

Set();

//add element to set

void addElement (int element);

//remove element from set

void removeElement(int element);

//check for membership

bool isMember(int element);

//set union, modifies curremtn set

void Union (Set s);

//set difference modifiers current set

void difference (Set s);

//size of set

int size();

//get element i

int getElement (int i);

private:

//binary search for element, returns index

bool search(int element, int& index);

//set members

int elements[maxElements];

//next empty position in elements

int next;

};

Overload the following operators the class Set:

A suitable overloading of the quality operator ==

A suitable overloading of the inquality operator !=

A suitable overloading of the add element operator +

A suitable overloading of the set union operator +

A suitable overloading of the remove operator -

A suitable overloading of the set difference operator -

Hints: some regular functions might helpful.

void printSet(Set s);

void generateSample(int sample[], int n);

Generate a set of value of a certain size. Produce an array that is used to initialize the sets in our experiment.

Explanation / Answer

PART 1 - Here is the Code (changes are boldened)

// lab6_AddMoney_n.cpp: This program adds money of two different people
// It reads the amounts for two people from an
// input file in_file.dat and writes the result into a file out_file.dat

#include<iostream>
#include<cstdlib>
#include<fstream>

using namespace std;

class AltMoney
{
public:
AltMoney();
friend void read_money(istream& ins, AltMoney& m);
friend AltMoney operator +(AltMoney m1, AltMoney m2);
friend void write_money(ofstream& ous, AltMoney m);
// Define a friend function for the operator
friend ostream & operator << (ostream &out, const Complex &c);

private:
int dollars;
int cents;
};

void read_money(istream& ins, AltMoney& m);
void get_streams(ifstream& ins, ofstream& ous);
void write_money(ofstream& ous, AltMoney m);

ostream & operator << (ostream &out, const AltMoney &c)
{
out << "$" << c.dollars << ".";
if (c.cents <= 9)
out << "0"; // display 0 on the left if number is less than 10
out << c.cents;
}

int main( )
{
ifstream ins;
ofstream ous;
AltMoney m1, m2, sum;

get_streams(ins, ous);

read_money(ins, m1);
ous << "The first money is:";
write_money(ous, m1);

read_money(ins, m2);
ous << "The second money is:";
write_money(ous, m2);

sum = m1+m2;
ous << "The sum is:";
write_money(ous, sum);

ins.close();
ous.close();

return 0;
}

AltMoney::AltMoney()
{
}

void write_money(ofstream& ous, AltMoney m)
{
ous << "$" << m.dollars << ".";
if(m.cents <= 9)
ous << "0"; //to display a 0 on the left for numbers less than 10
ous << m.cents << endl;
}

AltMoney operator +(AltMoney m1, AltMoney m2)
{
AltMoney temp;
int extra = 0;
temp.cents = m1.cents + m2.cents;
if(temp.cents >=100){
temp.cents = temp.cents - 100;
extra = 1;
}
temp.dollars = m1.dollars + m2.dollars + extra;
return temp;
}

void read_money(istream& ins, AltMoney& m)
{
int d, c;
ins >> d;
ins >> c;
if( d < 0 || c < 0)
{
cout << "Invalid dollars and cents, negative values ";
exit(1);
}
m.dollars = d;
m.cents = c;
}

void get_streams(ifstream& ins, ofstream& ous)
{
ins.open("in_file.dat");
if(ins.fail())
{
cout << "Failed to open the input file. ";
exit(1);
}

ous.open("out_file.dat");
if(ous.fail())
{
cout << "Failed to open the output file. ";
exit(1);
}
}

---------------------

PART 2 - Here is the C++ code for the set class including the implementations of the methods and operators -

class Set {

public:
//default constructor
Set();
//add element to set
void addElement (int element);
//remove element from set
void removeElement(int element);
//check for membership
bool isMember(int element) const;
//set union, modifies curremtn set
void Union (Set s);
//set difference modifiers current set
void difference (Set s);
//size of set
int size () const;
//get element i
int getElement (int i) const;
// Declare the operators as friend functions
friend Set operator + (Set const &, Set const &); // Set Union
friend Set operator - (Set const &, Set const &); // Set Difference
friend bool operator == (Set const &, Set const &); // Set Equality
friend bool operator != (Set const &, Set const &); // Set Inequality
friend Set operator + (Set const &, int const &); // Add Element
friend Set operator + (int const &, Set const &); // Add Element
friend Set operator - (Set const &, int const &); // Remove Element
friend Set operator - (int const &, Set const &); // Remove Element


private:
//binary search for element, returns index
bool search(int element, int& index) const;
//set members
int elements[maxElements];
//next empty position in elements
int next;
};

Set::Set() {
int next = 0; // Set 0 elements in the begininng
for(int i = 0; i < maxElements; i++)
elements[i] = 0; // Initialize elements array with 0s (benign)
}

bool Set::search(int element, int& index) const {
// Search for the element in the set
for(int i = 0; i < next; i++) {
// Search for the element
if (elements[i] == element) {
// If found get its position, return true
index = i;
return true;
}
}
// If element not found, return false
return false;
}

void Set::addElement(int element) {
// Add the element at position next and increment it
if (next < maxElements) {
// If the index to add the next element does not exceed the size of the set
// add it to the set
elements[next++] = element;
}
}

void Set::removeElement(int element) {
int pos; // Initialize pos with -1
// Remove the given element from the set
if (search(element, pos)) {
for(int i = pos; i < next - 1; i++) {
// If the element was found then Shift elements further to it
// one step behind
elements[i] = elements[i+1];
}
next--;
}
}

bool Set::isMember(int element) const {
int pos;
if(search(element, pos)) {
// If element found during search, return true
return true;
}
// Else, return false
return false;
}

void Set::Union(Set s) {
for(int i = 0; i < s.size(); i++) {
// For each element in the Set s
int elem = s.getElement(i); // get the element
if(!isMember(elem)) {
// If it is not present in the set already
addElement(elem);
}
}
}

void Set::difference(Set s) {
for(int i = 0; i < s.size(); i++) {
// For each element in the Set s
int elem = s.getElement(i);
if(isMember(elem)) {
// If it is present in this set, then remove it
removeElement(elem);
}
}
}

int Set::size() const {
return next;
}

int Set::getElement(int i) const{
if (i < size()) {
// If i is less than the size of the set then return the element
return elements[i];
}
// Otherwise return 0 (benign addition though)
return 0;
}

Set operator + (Set const &s1, Set const &s2)
{
// Union operator
// Takes Union of s1 and s2 and stores it in S
Set S;
S.Union(s1);
S.Union(s2);
return S;
}

Set operator - (Set const &s1, Set const &s2)
{
// Difference operator
// Takes Difference of s1 and s2 and stores it in S
Set S;
S.Union(s1);
S.difference(s2);
return S;
}

bool operator == (Set const &s1, Set const &s2)
{
// Equality operator
// If all the elements of set s2 are present in set s1, they are equal
bool equal = true;
for(int i = 0; i < s2.size(); i++) {
// Loop through all elements of s2 and check if they are present in s1
int elem = s2.getElement(i);
if(!s1.isMember(elem)) {
// If we find a non-existing member, then they are not equal
// and we set the equal to false
equal = false;
break;
}

}
// If all members are found equal return true
return equal;
}

bool operator != (Set const &s1, Set const &s2)
{
if(s1 == s2) {
// If they are equal return false
return false;
}
else {
// Otherwise return false
return true;
}
}

Set operator + (Set const &s1, int const &e)
{
// Add element e to Set S which equals s1
Set S;
S.Union(s1);
S.addElement(e);
return S;
}

Set operator + (int const &e, Set const &s1)
{
// Same as the above, just introducing commutativity
Set S;
S.Union(s1);
S.addElement(e);
return S;
}

Set operator - (Set const &s1, int const &e)
{
// Remove element e from the set s1
Set S;
S.Union(s1);
S.removeElement(e);
return S;
}

Set operator - (int const &e, Set const &s1)
{
Set S;
S.Union(s1);
S.removeElement(e);
return S;
}

NOTE: You have to define the constant maxElements to use this class. you can either set it in the class or use the preprocessor directive #define maxElements 10 for example, to set it to 10.