Exemplo n.º 1
0
 public static void Main(string[] args)
 {
     //                                        check that arguments have been supplied
     if (args.Length != 2) {
       Console.WriteLine("missing args");
       System.Environment.Exit(1);
     }
       //                                        attempt to open data file
     InFile data = new InFile(args[0]);
     if (data.OpenError()) {
       Console.WriteLine("cannot open " + args[0]);
       System.Environment.Exit(1);
     }
       //                                        attempt to open results file
     OutFile results = new OutFile(args[1]);
     if (results.OpenError()) {
       Console.WriteLine("cannot open " + args[1]);
       System.Environment.Exit(1);
     }
       //                                        various initializations
     int total = 0;
     IntSet mySet = new IntSet();
     IntSet smallSet = new IntSet(1, 2, 3, 4, 5);
     string smallSetStr = smallSet.ToString();
       //                                        read and process data file
     int item = data.ReadInt();
     while (!data.NoMoreData()) {
       total = total + item;
       if (item > 0) mySet.Incl(item);
       item = data.ReadInt();
     }
       //                                        write various results to output file
     results.Write("total = ");
     results.WriteLine(total, 5);
     results.WriteLine("unique positive numbers " + mySet.ToString());
     results.WriteLine("union with " + smallSetStr
                    + " = " + mySet.Union(smallSet).ToString());
     results.WriteLine("intersection with " + smallSetStr
                    + " = " + mySet.Intersection(smallSet).ToString());
     results.Close();
 }
Exemplo n.º 2
0
 public static void Main(string [] args)
 {
     const int Max = 32000;
     IntSet uncrossed = new IntSet();        // the sieve
     int i, n, k, it, iterations, primes = 0;   // counters
     IO.Write("How many iterations? ");
     iterations = IO.ReadInt();
     bool display = iterations == 1;
     IO.Write("Supply largest number to be tested ");
     n = IO.ReadInt();
     if (n > Max) {
       IO.Write("n too large, sorry");
       System.Environment.Exit(1);
     }
     IO.WriteLine("Prime numbers between 2 and " + n);
     IO.WriteLine("-----------------------------------");
     for (it = 1; it <= iterations; it++) {
       primes = 0;
       for (i = 2; i <= n; i++)                 // clear sieve
     uncrossed.Incl(i);
       for (i = 2; i <= n; i++)                 // the passes over the sieve
     if (uncrossed.Contains(i)) {
       if (display && primes % 8 == 0) {               // ensure line not too long
         IO.WriteLine();
       }
       primes++;
       if (display) IO.Write(i, 6);
       k = i;                               // now cross out multiples of i
       do {
         uncrossed.Excl(k);
         k += i;
       } while (k <= n);
     }
       if (display) IO.WriteLine();
     }
     IO.Write(primes + " primes");
 }
Exemplo n.º 3
0
 private IntSet Expand(IntSet old, int newSize)
 {
     IntSet expandedSet = old.Copy();
       if (newSize > old.bitSet.Length) expandedSet.bitSet.Length = newSize;
       return expandedSet;
 }
Exemplo n.º 4
0
 public IntSet SymDiff(IntSet that)
 {
     // Set symmetric difference
       int commonSize = max(this.bitSet.Length, that.bitSet.Length);
       IntSet one = Expand(this, commonSize);
       IntSet two = Expand(that, commonSize);
       return new IntSet(one.bitSet.Xor(two.bitSet));
 }
Exemplo n.º 5
0
 public IntSet Union(IntSet that)
 {
     // Set union
       int commonSize = max(this.bitSet.Length, that.bitSet.Length);
       IntSet one = Expand(this, commonSize);
       IntSet two = Expand(that, commonSize);
       return new IntSet(one.bitSet.Or(two.bitSet));
 }
Exemplo n.º 6
0
 /*
 public IntSet Intersection(IntSet that) {
 // Set intersection
   int commonSize = max(this.bitSet.Length, that.bitSet.Length);
   IntSet one = Expand(this, commonSize);
   IntSet two = Expand(that, commonSize);
   return new IntSet(one.bitSet.And(two.bitSet));
 } // IntSet.intersection
   */
 public IntSet Intersection(IntSet that)
 {
     // Set intersection
       int minSize = min(this.bitSet.Length, that.bitSet.Length);
       IntSet one = new IntSet(minSize);
       for (int i = 0; i < minSize; i++) one.bitSet[i] = this.bitSet[i]  && that.bitSet[i];
       return one;
 }
Exemplo n.º 7
0
 public bool Equals(IntSet that)
 {
     // Value comparison - sets contain same elements
       if (that == null) return false;
       int commonSize = max(this.bitSet.Length, that.bitSet.Length);
       IntSet one = Expand(this, commonSize);
       IntSet two = Expand(that, commonSize);
       for (int i = 0; i < commonSize; i++)  {
     if (one.bitSet[i] != two.bitSet[i]) return false;
       }
       return true;
 }
Exemplo n.º 8
0
 /*
 public IntSet Difference(IntSet that) {
 // Set difference
   int commonSize = max(this.bitSet.Length, that.bitSet.Length);
   IntSet one = Expand(this, commonSize);
   IntSet two = Expand(that, commonSize);
   for (int i = 0; i < commonSize; i++) one.bitSet[i] = one.bitSet[i]  && ! two.bitSet[i];
   return one;
 } // IntSet.Difference
   */
 public IntSet Difference(IntSet that)
 {
     // Set difference
       int minSize = min(this.bitSet.Length, that.bitSet.Length);
       IntSet one = this.Copy();
       for (int i = 0; i < minSize; i++) one.bitSet[i] = one.bitSet[i]  && ! that.bitSet[i];
       return one;
 }
Exemplo n.º 9
0
 public bool Contains(IntSet that)
 {
     // Returns true if that is a subset of this set
       return that.IsEmpty() || that.Difference(this).IsEmpty();
 }
Exemplo n.º 10
0
 // Empty set constructor
 public IntSet(IntSet old)
 {
     // Construct set from old
       this.bitSet = new BitArray(old.bitSet);
 }