0

这个 C++ 程序要求用户输入他/她接下来要输入的位序列的长度。这个长度变量被命名为 xx,它的类型是 int。我正在使用三个初始大小为 5 的动态位集,它们是 inpSeq、operSeq 和 bit。例如,我正在调整位集的大小 inpSeq.resize(xx);。编译程序的时候,出现一个很大的错误列表,感觉这里贴不上去。但我确信这些错误都与这个变量 xx 有关,代码在使用它作为变量来调整位集大小之前编译得很好。我调整位集大小的方式有什么问题?我可以在不要求用户输入 inpSeq 位集长度的情况下制作 inpSeq 大小的位集吗?

#include <iostream>  //Standard library.
#include <boost/dynamic_bitset.hpp>    //Library for 10 handling.
#include <vector>    //Variable size array.
#include <algorithm> //We use sorting from it.

using namespace std;

int main()
{
 int y = 0;
 int turnCount = 0;
 int count1 = 0, count0 = 0;
 int xx = 0; 
 boost::dynamic_bitset<> inpSeq(5);
 int polyLoc;
 boost::dynamic_bitset<> operSeq(5);
 boost::dynamic_bitset<> bit(5);
 vector <int> xorArray;
 vector <int> keyReg;
 cout << "What is the legnth of the sequence?";
 cin << xx;
 inpSeq.resize(xx);
 operSeq.resize(xx);
 bit.resize(xx);
 cout << "Enter a bit sequence: \n";
 cin >> inpSeq;
 int seq_end = inpSeq.size() - 1;
 cout << "Enter polynomial:";
 cin >> polyLoc;
 while(polyLoc>0)
 {
  xorArray.push_back(polyLoc%10);
  polyLoc/=10;
 }
 cout << "xorArray is: ";
 for ( unsigned int i = 0; i < xorArray.size(); i++)
 {
  cout << xorArray[i] << " ";
 }
 sort(xorArray.rbegin(), xorArray.rend());
 cout << "\n";
 operSeq = inpSeq;
 keyReg.push_back(inpSeq[0]);
  int x = xorArray[0];
  cout << "x is: " << x << "\n";
  for ( unsigned int  i = 0; i < xorArray.size();  i++)
  {
   cout << xorArray[i] << "\n";
  }
  cout << "bit 3 of initial " << bit[seq_end] << "\n";
  do {
  for (unsigned int r = 1; r < xorArray.size(); r++)
  {
  bit[seq_end] = operSeq[x];
  y = xorArray[r];
  bit[seq_end] = bit[seq_end] ^ operSeq[y];
  }
  operSeq >>= 1;
  operSeq[seq_end]  = bit[seq_end];
  keyReg.push_back(operSeq[0]);
  turnCount ++;
  cout << "--\n";
 }
 while ((operSeq != inpSeq) && (turnCount < 20));
 cout << "Generated key is: ";
 for (unsigned int k = 0; k < keyReg.size(); k++)
  {
  cout  <<  keyReg[k];
  }
 cout << "\n";
 cout << "Bit 1 positions: ";
 for ( unsigned int g = 0; g < xorArray.size(); g++)
 {
  cout << xorArray[g];
 }
 cout << "\n";
 cout << "Key length is: " << keyReg.size();
 cout << "\n";
 for ( unsigned int i = 0; i < keyReg.size(); i++)
 {
  if (keyReg[i]==1)
   {
    count1++;
   }
  else {
    count0++;
  }
 }
 cout << "Number of 0's: " << count0 << "\n";
 cout << "Number of 1's: " << count1 << "\n";
 if ( keyReg.size()%2 ==0)
  {
   cout << "key length is even. \n";
   if (count1==count0)
    {
   cout << "Key is perfect! \n";
    }
  else {
   cout << "Key is not perfect! \n";
    }
 }
  else
   {
  cout << "key length is odd. \n";
   if  ((count1==count0+1) || (count0==count1+1))
    {
   cout << "Key is perfect! \n";
    }
  else {
   cout << "Key is not perfect! \n";
    }
   }
  cin.get();
}
4

0 回答 0