🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Sam Sutherland Sun, 25 Apr 1999 00:00
Please Help with this
#186
Author: 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
Re: Please Help with this
#187
Author: Sam Sutherland
Date: Sun, 25 Apr 1999 00:00
10 lines
1138 bytes
If anybody can contribute just one of the mentioned functions (Remove,
Intersection, Difference), even that would make me happy. thx

Sam Sutherland wrote in message <7g02uo$mrt$1@autumn.news.rcn.net>...
>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
Re: Please Help with this
#188
Author: Sam Sutherland
Date: Sun, 25 Apr 1999 00:00
48 lines
1726 bytes
To show that I know what I need to do, (but just can't do it) here's some
psuedo code I'm trying to turn into real code for the program in my original
message. Hopefully this is easier for someone. ??  Thanks-  samantha

REMOVE element y from set A

for(each element x in set A) {
  if(x equals y) {
    // delete x if necessary, e.g., for char * case
    for(i = index of x + 1 up to size - 1) {
      copy element i to element i-1
    }
    set size to size-1
  }
}

INTERSECTION of set A and set B

create new set C
for(each element x in set A) {
  for(each element y in set B) {
    if(x equals y) {
      add x to set C
    }
  }
}

DIFFERENCE of set A and set B

create new set C
for(each element x in set A) {
  set flag to true
  for(each element y in set B) {
    if(x equals y) {
      set flag to false
    }
  }
  if(flag is true) {
    add x to set C
  }
}



Sam Sutherland wrote in message <7g07gb$n9p$1@autumn.news.rcn.net>...
>If anybody can contribute just one of the mentioned functions (Remove,
>Intersection, Difference), even that would make me happy. thx
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads