在Java中使用位串设置麻烦

时间:2012-01-13 21:00:44

标签: java java.util.scanner set bit

public class BitStringOperations3
{

public static void main (String args[])
 {
  Scanner in = new Scanner (System.in);

  int setA = 0;
  int setB = 0;
  int elementsSetA = 0;
  int elementsSetB = 0;


  System.out.println ("How many integers are in set A?");
  elementsSetA = in.nextInt ();
    while (elementsSetA > 9 || elementsSetA < 0)
      {
       System.out.println ("This input is invalid. Please enter a number between 0 and 9 and try again.");
        elementsSetA = in.nextInt();
      }



  System.out.println ("How many integers are in set B?");
  elementsSetB = in.nextInt ();
    while (elementsSetB > 9 || elementsSetB < 0)
      {
       System.out.println ("This input is invalid. Please enter a number between 0 and 9 and try again.");
        elementsSetB = in.nextInt();
      }




    for (int i = 1; i <= elementsSetA; i++)
     {
      System.out.println ("Please enter integer number " + i + " in set A: ");
       setA = add(setA, in.nextInt() );
     }

     for (int i = 1; i <= elementsSetB; i++)
      {
       System.out.println ("Please enter integer number " + i + " in set B: ");
        setB = add(setB, in.nextInt () );
      }








 }


 public static boolean setContainsValue (int set, int value)
 {
boolean setContainsValue = (set & maskForValue) != 0;
return true;
 }


 public static int addValueToSet (int set, int newValue)
 {
 set = set | maskForValue;
 return set;
 }

 public static void printSet (int set, int value)
{
 int mask = 1;
 System.out.print ("{");
  for (int i = 0; i<= 9; i++)
  {
    if(( mask & set ) == 1)
     System.out.print(i + " " );
        int maskForValue = 1 << value;
        set >>= 1; //set = (set >> 1);

  }

  System.out.println ("} ");


}







  }

我在上学时遇到了麻烦。我们得到通用集U = {0-9}。我必须为两个集合收集用户输入,然后使用位字符串(我们不允许在java中使用Set或HashSet类)来存储集合并对它们执行操作,例如补集,Set A union Set B和这样。我知道如何做,但我的代码没有正确地将集合A和B转换为内存,因此,我无法对它们执行任何操作。将非常感谢帮助!提前致谢。 :)

编辑1:

好吧,我读了你的想法并试图尽我所能地实施它们,我已经给出了上面的结果。这个程序真的让我离开了我的舒适区,我非常感谢所有的帮助。

2 个答案:

答案 0 :(得分:4)

首先,帮自己一个忙,并创建帮助方法。然后专注于使它们正确:

public static boolean contains(int set, int value) {
   //return true if value bit is 1 in set
}

public static int add(int set, int newValue) {
   //add newValue to set and return it
}

之后,您可以更清楚地表达您的逻辑:

if ( contains(set, 1) ) {
   //print 1
}

一些一般提示:

  • 不要使用Math.pow(),因为它是针对浮点数的。要获得2的幂,请使用位移:

    int maskForValue = 1 << value;
    
  • 要检查某个位是否已设置,请找到该位的掩码并使用&。除了你正在检查的位之外,这会将所有位都归零。

    boolean setContainsValue = (set & maskForValue) != 0;
    
  • 要在位字段中设置位,请找到该位的掩码并使用|。这可确保该位变为1

    set = set | maskForValue;
    

修改

关于你的直接问题,请看一下:

  for (int i = 1; i <= elementsSetB; i++)
  {
     System.out.println ("Please enter integer number " + i + " in set B: ");
     setB = in.nextInt ();
  }

您每次都会覆盖setAsetB。最后,setAsetB将包含用户指定的最后一个值。然后,你这样做:

  for (int i = 0; i <=9; i++)
     setB |= (int)pow(2.0, i-1);

这只是忽略用户的输入并覆盖所有位0-9(虽然以不安全的方式!)。当然,用户输入的内容无关紧要。

摆脱后者的循环,然后像这样存储输入(使用我上面描述的辅助方法):

  for (int i = 1; i <= elementsSetB; i++)
  {
     System.out.println ("Please enter integer number " + i + " in set B: ");
     setB = add(setB, in.nextInt());
  }

编辑2

你似乎在理解我来自哪里的这些“助手”方法时遇到了问题。如果这是您第一次使用具有参数的方法,请抱歉让问题蒙上阴影。但它们允许您专注于一次使用一个功能。我将在这里扩展我的意思:

public static boolean setContainsValue(int set, int value) {
   //return true if the bit string (or bit set) represented by the "set" parameter
   //contains the value stored in the "value" parameter
   //see the FIRST and SECOND bullet points above for how to do this
}

public static int addValueToSet(int originalSet, int valueToAdd) {
   //add the value stored in the "valueToAdd" parameter to the set represented by the
   //"originalSet" parameter and return the result
   //see the FIRST and THIRD bullet points above for how to do this.
}

我甚至会为你写一些测试。在至少以下所有内容打印为真之前,上述方法尚未正确实施:

int set = 0;

System.out.println( ! contains(set, 1) ); //make sure set doesn't contain 1

set = addValueToSet(set, 1);
System.out.println( contains(set, 1) ); //make sure set now contains 1
System.out.println( !contains(set, 2) ); //make sure set doesn't contain 2

set = addValueToSet(set, 2);
System.out.println( contains(set, 1) ); //make sure set still contains 1
System.out.println( contains(set, 2) ); //make sure set now contains 2

答案 1 :(得分:0)

首先,你需要一个类(这是面向对象的编程,对吧?)来包含“DigitSet”。

public DigitSet {

   private BitSet digits;

   public DigitSet() {
     // digits contains one bit for each digit
     digits = new BitSet(10);
   }

   ... rest of DigitSet code goes here, like ...
   /**
    * Check if this set contains a particular digit.
    */
   public boolean contains(int value) {
     // check to see if value is a valid input (0-9)
     // look in digits to see if the "right" bit is set.
   }

   public void set(int value) {
     // check to see if value is a valid input (0-9)
     // set the "right" bit in digits to 1.
   }

   public void clear(int value) {
     // check to see if value is a valid input (0-9)
     // set the "right" bit in digits to 0.         
   }

   public DigitSet union(DigitSet other) {
     // construct a "new" output DigitSet.
     // Walk through all of the digits in "this" set
       // if a digit is set in this set, set it in the output set.
     // Walk through all of the digits in the "other" set
       // if a digit is set in the other set, set it in the output set.
   }

   public String toString() {
     // return a display string based on the set "digit" bits
   }

}

然后其余的只是输入处理和“执行操作”

public static void main(String[] args) {
  DigitSet first = new DigitSet();
  // read in the values for the first digit set, for each value
    // set the digit in first like so
    first.set(value);
  DigitSet second = new DigitSet();
  // read in the values for the second digit set, for each value
    second.set(value);

  DigitSet result = first.union(second);
  System.out.println("Result: " + result.toString());

}