🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: att.lang.c++
Article #186

Please Help with this

#186
From: Sam Sutherland
Date: Sun, 25 Apr 1999 00:00
140 lines
3716 bytes
I need some assistance. The program with it description below is what I'm
trying to accomplish. I've included the code, and the output. As you can
see, I can do everything but Remove, the Intersection, and the Difference.
I've spent more time than you would believe trying to get these last methods
going. Could someone be kind enough to end my misery and show me how this is
done? I'd be very appreciative. I believe it's possible, and know it's
simple, however I'm thinking wrong. If you can help me, I'll definitely be
back to pitch in with help here in the future. Thanks in advance-

Sam

The prog. I am writing: Creates a set-

Adds a new element to this set-
Removes and element from the set-
Computes the intersection of the two sets: elements in both set 1 and set 2-

Computes the difference of two sets: elements in set1 but not set 2-

Computes the Union of two sets-

Here is the code..........

#include <iostream.h>
#include <string.h>
template <class T>
class Set
{
int size;
T container[50];
public:
Set() { size = 0; }
void insert( T i);
void remove(T r);
Set & setUnion(Set & b);
Set & setIntersect(Set & b);
Set & setDifference(Set & b);
bool operator==(Set & b);
void print() {
  cout << "SET = ";
   for(int i = 0; i < size; i++)
    cout << container[i] << ", ";
   cout << endl << "with " << size << " elements\n";
}
};
template <class T>
void Set<T>::insert(T i)
{
for(int j = 0; j < size; j++)
  if(container[j]==i)
   return;

  container[size++] = i;
}

void Set<char *>::insert(char * str)
{
for(int j = 0; j < size; j++)
  if(strcmp(str,container[j])==0)
   return;

  container[size] = new char [strlen(str)+ 1];
  strcpy(container[size++],str);
}
template <class T>
Set<T> & Set<T>::setUnion(Set<T> & b)
{
static Set<T> c;
c = *this;
for(int j = 0; j < b.size; j++)
{
  bool flag=true;
  for(int k = 0; k < c.size; k++)
   if(b.container[j] == c.container[k])
   {
    flag = false;
    break;
   }
  if(flag == true)
   c.container[c.size++] = b.container[j];
}
return c;
}
//template <class T>
Set<char *> & Set<char *>::setUnion(Set<char *> & b)
{
static Set<char *> c;
c = *this;
for(int k = 0; k < size; k++)
{
  c.container[k] = new char[strlen(container[k])+1];
  strcpy(c.container[k], container[k]);
}
c.size = k;
  for(int j = 0; j < b.size; j++)
{
  bool flag=true;
  for(int k = 0; k < c.size; k++)
   if(strcmp(b.container[j],c.container[k]) == 0)
   {
    flag = false;
    break;
   }
  if(flag == true)
  {
   c.container[c.size] =
    new char [strlen(b.container[j])+ 1];
   strcpy(c.container[c.size++],b.container[j]);
  }
}
return c;

}


void main()
{
cout << endl;
char a[5] = "324";
Set<char *> myset;
Set<char *> set2;
Set<char *> C;
set2.insert(a); set2.insert("7");
myset.insert("1.5");
myset.insert("hello");
myset.insert("324");
cout << "Myset " ;myset.print();
cout << "set2 "; set2.print();
C = myset.setUnion(set2);
cout << "C "; C.print();
}


Myset SET = 1.5, hello, 324,
with 3 elements
set2 SET = 324, 7,
with 2 elements
C SET = 1.5, hello, 324, 7,
with 4 elements
Press any key to continue
Technical Details
Message-ID: <7g02uo$mrt$1@autumn.news.rcn.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d4ba,106607ba857ed8a6,start
X-Google-Attributes: gid10d4ba,public
From: "Sam Sutherland" <samantha_sutherl@hotmail.com>
Subject: Please Help with this
Date: 1999/04/25
Message-ID: <7g02uo$mrt$1@autumn.news.rcn.net>
X-Deja-AN: 470815081
X-Trace: TWLW9Ssyycczq8EiVNKtZ6rCn3klz3KSzD60xIK57yQ=
NNTP-Posting-Date: 25 Apr 1999 21:54:32 GMT
Newsgroups: att.lang.c++
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
X-Complaints-To: abuse@rcn.com