Programalama > C++

Etiketler: küme, islemleri, yapan, bir, program

Ort. 0
Puan ver:
//class bölümü
#ifndef SET_H
#define SET_H

class Set{
  private:
    int members[100];
    int index;
    int size;
  public:
    Set();
    void add_element(int i);
    void remove();
    void sort();
    Set intersection(Set &s);
    Set unions(Set &s);
    Set difference(Set &s);
    int get_size();
    void display_set();
};

#endif



//bu bölüm cpp dosyası olacak
Set.cpp Fonksiyonların olduğu dosyaya yazılacak
#include"Set.h"
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

Set::Set()
{
  for(int i=0;i<100;i++) members[i]=0;
  index=0;
  size=0;
}

void Set::add_element(int i)
{
  members[index]=i;
  index++;
  size=index;
}

void Set::remove()
{
  size=size-1;
}

void Set::sort()
{
  int hold=0;
  for(int pass=0;pass<size-1;pass++)
  {
    for(int j=0;j<size-1;j++)
    {
      if(members[j]>members[j+1])
      {
        hold=members[j];
        members[j]=members[j+1];
        members[j+1]=hold;
      }
    }
  }
}

Set Set::intersection(Set &s)
{
  Set Sin;
  for(int i=0;i<size;i++)
  {
    for(int t=0;t<s.size;t++)
    {
      if(members[i]==s.members[t]) Sin.add_element(members[i]);
    }
  }
  return Sin;
}

Set Set::unions(Set &s)
{
  Set Sun;
  int num=0;
  for(int j=0;j<size;j++)
  {
    Sun.add_element(members[j]);
  }
  for(int y=0;y<s.size;y++)
  {
    int hold=0;
    for(int r=0;r<size;r++)
    {
      if(s.members[y]!=Sun.members[r]) hold++;
    }
    if(hold==size) Sun.add_element(s.members[y]);
  }
  return Sun;
}

void Set::display_set()
{
  for(int i=0;i<size;i++)
  {
    cout<<members[i]<<"  ";
  }
}

Set Set::difference(Set &s)
{
  Set Sdif;
  for(int i=0;i<size;i++)
  {
    int hold=0;
    for(int t=0;t<s.size;t++)
    {
      if(members[i]!=s.members[t]) hold++;
    }
    if(hold==s.size) Sdif.add_element(members[i]);
  }
  return Sdif;
}

int Set::get_size()
{
  return size;
}
 
//main.cpp Main program
#include "Set.h"
#include <iostream>
using std::cout;
using std::endl;

int main()
{
  Set s1,s2,s3,s4,s5;
  
  //add elemts to the Sets and display them
  for(int i=0;i<20;i++) s1.add_element(i);
  cout<<"Set 1: "<<endl;
  s1.display_set();
  for(i=0;i<20;i+=2) s2.add_element(i);
  cout<<endl<<endl<<"Set 2: "<<endl;
  s2.display_set();
  for(i=1;i<40;i*=2) s3.add_element(i);
  cout<<endl<<"Set 3: "<<endl;
  s3.display_set();
       
  //find the intersection of the set 1 and set 3
  s4=s1.intersection(s3);
  cout<<endl<<endl<<"The intersection of the set_1 and set_3 (set_4): "<<endl;
  s4.display_set();

  //find the difference of set_3 and set_1
  s3=s3.difference(s1);
  cout<<endl<<endl<<"the difference of set_3 and set_1: "<<endl;
  s3.display_set();

  //find the union of the set_2 and set_4
  s5=s4.unions(s2);
  cout<<endl<<endl<<"The union of the set_2 and set_4(set_5): "<<endl;
  s5.display_set();

  //sort set_5
  s5.sort();
  cout<<endl<<endl<<"Sorted set(set_5): "<<endl;
  s5.display_set();

  //find the differnce of set_5 and set_1
  s1=s1.difference(s5);
  cout<<endl<<endl<<"the difference of set_5 and set_1(set_1): "<<endl;
  s1.display_set();
        
  //remove five elemnts from set_1
  for(i=0;i<5;i++) s1.remove();
  int a=0;
  cout<<endl<<endl<<"remove five elements from set_1: "<<endl;
  s1.display_set();
  cout<<endl;
  return 0;
} 


Yorumlar                 Yorum Yaz
Bu hazır kod'a ilk yorumu siz yapın!
KATEGORİLER
ASP - 240
ASP.NET - 24
C# - 75
C++ - 174
CGI - 8
DELPHI - 247
FLASH - 49
HTML - 536
PASCAL - 246
PERL - 11
PHP - 160
WML - 9
XML - 2
Copyright © 2002 - 2024 Hazır Kod - Tüm Hakları Saklıdır.
Siteden yararlanırken gizlilik ilkelerini okumanızı tavsiye ederiz.
hazirkod.com bir İSOBİL projesidir.