C++ 汉明码(7,4)-C++;实施失败 #包括 #包括 使用名称空间std; int get_bin_表示(char x){ 如果(x='1'){ 返回1; } 如果(x=='0',则为else){ 返回0; } } int gen_hamming_代码(字符串标记){ 整数位[4]; 内部温度(0); 对于(int k=0;k

C++ 汉明码(7,4)-C++;实施失败 #包括 #包括 使用名称空间std; int get_bin_表示(char x){ 如果(x='1'){ 返回1; } 如果(x=='0',则为else){ 返回0; } } int gen_hamming_代码(字符串标记){ 整数位[4]; 内部温度(0); 对于(int k=0;k,c++,hamming-code,C++,Hamming Code,那么,这是错误的: #include <iostream> #include <string> using namespace std; int get_bin_representation(char x){ if(x == '1'){ return 1; } else if(x == '0'){ return 0; } } int gen_hamming_code(string token){ int bits[4]; i

那么,这是错误的:

#include <iostream>
#include <string>

using namespace std;

int get_bin_representation(char x){

  if(x == '1'){
    return 1;
  }
  else if(x == '0'){
    return 0;
  }
}
int gen_hamming_code(string token){

  int bits[4];
  int temp(0);

  for(int k=0; k<4; k++){
    bits[k] = get_bin_representation(token.at(k));
  }

  int ham_code[7];

  ham_code[0] = bits[0] + bits[1] + bits[3];
  ham_code[1] = bits[0] + bits[2] + bits[3];
  ham_code[2] = bits[0];
  ham_code[3] = bits[1] + bits[2] + bits[3];
  ham_code[4] = bits[1];
  ham_code[5] = bits[2];
  ham_code[6] = bits[3];

  for(int h=0; h<7; h++){
    temp = ham_code[h];
    ham_code[h] = temp%2;
    temp = 0;
  }

  for(int e=0; e<7; e++){
    cout << ham_code[e];
  }
  cout << endl;

  return 0;
}
int main(){

  string usr_input;
  string msg;
  int index(0);

  cout << "Hamming Code Program" << endl;

  while(true){

    cout << endl << ": ";
    getline(cin, usr_input);

    if(usr_input.find("gen") != std::string::npos){
        for(int i=0; i<usr_input.length(); i++){
            if(usr_input.at(i) == ' '){
                index = i;
            }
        }

        for(int j=index; j<usr_input.length(); j++){
            msg+=usr_input.at(j);
        }

        cout << "Hamming code (7,4): ";
        gen_hamming_code(msg);
    }
  }
}
< GF > Hamming码是用GF(2)算法定义的,GF(2)中的加法是C++ XOR算子(<代码> ^ < /代码>)。使用右运算符,可以消除后面的<代码> %2 < /Cord>循环> < /P>
您还将奇偶校验位与明文混合,这是我了解它时从未做过的事情。此外,在线模拟器使用简单顺序(明文,奇偶校验)无交错。

您是使用0和1的位或字符串执行此操作的吗?我问的原因是您使用的getline用于读取ASCII格式的字符串。您可能希望从stdin读取fread。此外,如果您使用的是位,您可能希望查看std::bitset以获得更好的方式来处理位。输入被解释为字符串,然后每个字符被转换成一个整数,可以是零,也可以是一,这是由get_bin_representation()函数完成的。
ham_code[0] = bits[0] + bits[1] + bits[3];