To achieve a set of integers, intersection, difference operation

  Problem: Write a program to achieve the basic operation of a set of integers
  Requirements: Enter the set of integers {2,4,1,3,5} and {2,5,10}, and the number of elements of the output of the former, and they are set, intersection, difference calculation
   IntSet a design class, including private data member len (length set) and an array of s (a collection of storage elements), and the following method:
    public void insert (int d) à add an element to the collection, duplicate elements can not be added
    public int length () à returns the number of elements of the set
    public int length () à return element for the position i of the set of
    public void disp () of all the elements of the output set à
    public IntSet union (IntSet s2) à implement and operation of the two sets
    public IntSet intersection (IntSet s2) à two sets of operation to achieve post
    public IntSet difference (IntSet s2) à implement arithmetic difference of the two sets
    public IntSet copySet (IntSet s2) à implement two sets of copies
 
Java code:
public class IntSet {
       private int len; // set length
       private int [] s; // Array
       
       public IntSet() {
              as = 0;
              s = new int[100];
       }
       
       public void insert (int d) // set of insert elements
       {
              for(int i=0;i<len;i++) {
                     if(s[i]==d){
                           return;
                     }
              }
          s [s] = d;
          as ++;
       }
       
       public int length () // Get set length
       {
              return len;
       }
       
       public int getInt(int i) {
              
              if(i>=0 && i<len)
              {
                     return s[i];
              }else {
                     return -1;
              }
       }
       
       public void disp() {
              for(int i=0;i<len;i++)
              {
                     System.out.print("\t"+s[i]);
              }
       }
       
       public IntSet union(IntSet s2) //并
       {
              int same;
              for(int i=0;i<this.len;i++)
              {
                     same = 0;
                     for(int j=0;j<s2.len;j++)
                     {
                           if(this.getInt(i)==s2.getInt(j))
                           {
                                  same = 1;
                                  break;
                           }
                           if(same == 0)
                           {
                                  this.insert(s2.getInt(j));
                           }
                     }
              }
              return this;
       }
       
       public IntSet intersection(IntSet s2) { //交
              
              int same;
              for(int i=0;i<this.len;i++) {
                     same = 0;
                     for(int j=0;j<s2.len;j++) {
                           if(this.s[i] == s2.getInt(j))
                           {
                                  same = 1; // find repeating elements, the same = 1
                                  break;
                           }
                     }
                           if (same == 0) {// find no duplicate elements, delete
                                  for(int k=i;k<this.len;k++)
                                  {
                                         this.s[k] =this.s[k+1];
                                  }
                                  
                                  i--;
                                  this.len--;
                                  
                           }
                     
              }
              return this;
       }
       public IntSet difference(IntSet s2) { //差
              
              int same;
              for(int i=0;i<this.len;i++) {
                     same = 0;
                     for(int j=0;j<s2.len;j++) {
                           if(this.s[i] == s2.getInt(j))
                           {
                                  same = 1; // find repeating elements, the same = 1
                                  break;
                           }
                     }
                           if (same == 1) {// find duplicate elements, delete
                                  for(int k=i;k<this.len;k++)
                                  {
                                         this.s[k] =this.s[k+1];
                                  }
                                  
                                  i--;
                                  this.len--;
                                  
                           }
                     
              }
              return this;
       }
       
       public IntSet copySet(IntSet s2) {
              this.len = s2.len;
              for(int i=0;i<s2.len;i++) {
                     this.s[i] = s2.getInt(i);
              }
              return this;
       }
}
 
Test code:
  public class Test {
       public static void main(String[] args) {
              IntSet s1, s2;
              s1 = new IntSet();
              s2 = new IntSet();
              s1.insert(2);
              s1.insert(4);
              s1.insert(1);
              s1.insert(3);
              s1.insert(5);
              
              s1.disp ();
              
              s2.insert(2);
              s2.insert(5);
              s2.insert(10);
              
              System.out.println();
              s2.disp ();
              
//            s1.union(s2);
//            System.out.println();
// s1.disp ();
              
//            s1.intersection(s2);
//            System.out.println();
// s1.disp ();
              
//            s1.difference(s2);
//            System.out.println();
// s1.disp ();
              
// IntSet s6 = new IntSet ();
//            s6.copySet(s2);
//            s6.disp();
              
              s2.difference(s1);
              System.out.println();
              s2.disp ();
       }
}
 

Guess you like

Origin www.cnblogs.com/lone5wolf/p/11111937.html