
24-03-2009, 04:57 PM
|
عضو مبدع
|
|
تاريخ التسجيل: Oct 2007
الجنس :
المشاركات: 893
|
|
رد: خدمات البرمجة ---- أطلب أي مساعدة في البرمجة
[quote=zaynubya;671338] ****** Oriented Programming
Write a program to carry out manipulation on sets. Create a class called set with two private members, the elements of the set and the size of the set. The elements should be an array of integers to hold an appropriate number of elements (e.g. 20). Provide a default constructor to initialize a newly defined set to be the empty set, the size of which should be zero. Provide also three utility functions to add, remove and to print elements of a set
-----------------------------------------
Brother Ahmed12 first of all thx for ur help but i would like from u to look at this solution:
#include <iostream>
using namespace std;
class set
{
private:
int elements [20];
int N;
public:
set ();
void add (int); // add elements into the set
void remove ( int); // remove elements from the set
void printSet (); // print the elements of the set
bool isEmptySet (); // check if set is empty or not; if empty return true
set Union (set &); // perform a union between two sets
set intersection (set &); // perform an intersection between two sets
bool testIfSubset (set & ); // check if first set is subset in the second set
};
set::set ()
{
N = 0; // size of the set
for (int i =0; i<=19; i++)
elements[i] = 0;
}
void set::add(int e)
{
// check if the element fits in the set; i.e. if the set is full or not
if (N == 20) // means that the set is full
cout <<"The set is full"<<endl;
else // the set is not full
{
bool found = false; // element not found in set
for (int i = 0; i < N; i++)
{
if (e == elements[i]){
found = true; break;}
}
if (found)
cout <<"element found in the set";
else
{
elements[N] = e;
N++; // update the size of the set
cout <<"Element added"<<endl;
}
}
}
void set::remove(int e)
{
if (!isEmptySet ()) // set is not empty
{
// search for the position of the element
int position = -1;
for (int i = 0; i < N; i++)
{
if (elements[i] == e)
{
position = i;
break;
}
}
if (position != -1) // element found in the set at index position
{
elements[position]=elements[N-1];
N--; // update the size of the set
cout <<"element removed"<<endl;
}
else
cout <<e<<" is not an element in the set"<<endl;
}
else
cout <<"The set is empty; there is no elements found till yet"<<endl;
}
void set::printSet()
{
if (isEmptySet ())
cout <<"Set is empty"<<endl;
else
{
cout <<"Elements are:"<<endl;
for (int i = 0; i < N; i++)
cout <<elements [i]<<" ";
}
cout <<endl;
}
bool set::isEmptySet()
{
return N == 0;
}
set set::Union(set & s1)
{
set resultSet;
if (N == 0 && s1.N != 0) // if first set is empty and the second is not
for (int i = 0; i < s1.N ; i++)
resultSet.add(s1.elements[i]);
else if (N != 0 && s1.N == 0) // if second set is empty and the first is not
for (int i = 0; i < N; i++)
resultSet.add(elements[i]);
else if (N != 0 && s1.N != 0) // if two sets are not empty
{
// add elements of first set into result set
for (int i = 0; i < N; i++)
resultSet.add(elements[i]);
// check if there exists elements found in the second set that can be added to the result set
for (int i = 0; i < s1.N; i++)
{
resultSet.add(s1.elements[i]);
}
}
else // if two sets are empty
cout <<"Union between to (phi) sets is (phi)" <<endl;
return resultSet;
}
set set::intersection(set & s1)
{
set resultSet;
if (N != 0 && s1.N != 0) // if first set is empty and the second is not
// check if there exists elements found in the second set that can be added to the result set
for (int i = 0; i < N; i++)
{
bool found = false; // indicates that element is not found in the second set
for (int j = 0; j < s1.N; j++)
{
if (elements[i] == s1.elements[j])
{
found = true; // element in first set is found in the second set
break;
}
}
if (found)
resultSet.add(elements[i]);
}
}
else // if two sets are empty
cout <<"Intersection between the sets is impossible" <<endl;
return resultSet;
}
bool set::testIfSubset (set & s1)
{
Set rs = (*this).intersection(s1);
if (rs == *this)
return true;
else
return false;
}
void main ()
{
// --- testing the class --- //
int n;
set s1;
// add to set
cout <<"Enter a number to add to set (-5 to end): ";
cin >> n;
while (n != -5)
{
s1.add(n);
cout <<endl;
s1.printSet();
cout <<"Enter a number to add to set (-5 to end): ";
cin >> n;
}
// remove from set;
system("cls");
s1.printSet();
cout <<"Enter a number to remove from set (-5 to end): ";
cin >> n;
while (n != -5)
{
s1.remove(n);
cout <<endl;
s1.printSet();
cout <<"Enter a number to remove from set (-5 to end): ";
cin >> n;
}
system("cls");
// testing union, intersection and subset
cout <<"Elements of set 1 are"<<endl;
s1.printSet();
set s2;
for (int i = 1; i <= 10; i++)
s2.add(i * 2 - 3);
cout <<"Elements of set 2 are"<<endl;
s2.printSet();
set r1, r2;
r1 = s1.Union(s2);
r1.printSet();
r2 = s1.intersection(s2);
r2.printSet();
if (s1.testIfSubset(s2))
cout <<"s1 is subset of s2"<<endl;
else
cout <<"s1 is not subset of s2"<<endl;
}
__________________
بالصبر تبلغ ما ترجوه من أمل *** فاصبر فلا ضيق إلا بعده فرج
|