0

所以我指的是 Geeks For Geeks 在数据通信中实现 CRC
这是代码:-

#include <bits/stdc++.h>
using namespace std;

string xor1(string a, string b)
{
  string result = "";
  int n = b.length();
  for (int i = 1; i < n; i++)
  {
    if (a[i] == b[i])
      result += "0";
    else
      result += "1";
  }
  return result;
}

string mod2div(string divident, string divisor)
{
  int pick = divisor.length();
  string tmp = divident.substr(0, pick);
  int n = divident.length();
  while (pick < n)
  {
    if (tmp[0] == '1')
      tmp = xor1(divisor, tmp) + divident[pick];
    else
      tmp = xor1(std::string(pick, '0'), tmp) + divident[pick];
    pick += 1;
  }
  if (tmp[0] == '1')
    tmp = xor1(divisor, tmp);
  else
    tmp = xor1(std::string(pick, '0'), tmp);
  return tmp;
}

void encodeData(string data, string key)
{
  int l_key = key.length();
  string zeroes_to_be_added;
  for(int i=0;i<l_key-1;i++)
    zeroes_to_be_added = zeroes_to_be_added + "0";
  string appended_data = (data + zeroes_to_be_added);
  string remainder = mod2div(appended_data, key);
  string codeword = data + remainder;
  cout << "Remainder : " << remainder << "\n";
  cout << "Encoded Data (Data + Remainder) :" << codeword << "\n";
}

int main()
{
  string data,key;
  cout<<"enter the data to be send to the receiver side from the sender side"<<endl;
  cin>>data;
  cout<<"Enter the key value that is present on both sender and reciever side"<<endl;
  cin>>key;
  encodeData(data, key);
  return 0;
}

我得到了除以下代码之外的所有代码:-

string mod2div(string divident, string divisor)
{
  int pick = divisor.length();
  string tmp = divident.substr(0, pick);
  int n = divident.length();
  while (pick < n)
  {
    if (tmp[0] == '1')
      tmp = xor1(divisor, tmp) + divident[pick];
    else
      tmp = xor1(std::string(pick, '0'), tmp) + divident[pick];
    pick += 1;
  }
  if (tmp[0] == '1')
    tmp = xor1(divisor, tmp);
  else
    tmp = xor1(std::string(pick, '0'), tmp);
  return tmp;
}

有人可以帮助我理解这部分吗?让我们说数据 = 1010101010 和密钥 = 11001。


问题:-这有什么用,就像这部分到底在做什么:-

标准::字符串(挑选,'0')

任何帮助将不胜感激。

4

1 回答 1

1

std::string(pick, '0')是字符串构造函数,它创建一个长度为 的pick字符串'0'。在其他方面,它是以下代码的缩写形式:-

  string zeroes_to_be_added;
  zeroes_to_be_added.resize(pick);
  for (int i = 0; i < pick; i++)
    zeroes_to_be_added[pick] = '0';
于 2021-11-11T13:11:16.570 回答