我在汉明码中做错了什么?

时间:2017-05-14 09:04:02

标签: c++

我开始编写哼唱代码的大学项目。我给出了下面的代码,对我来说似乎没问题,但我的论文中有0。不知道我在这里做错了什么。在问我的教授之前,我可以帮助我找到。

#include <iostream.h>
#include <math.h>

main(){
  int alp,k;
  do{
    cout<<"\n ****************************************************************";
    cout<<"\n \n";
    cout<<"\n WELCOME TO ERROR DETECTION AND CORRECTION";
    cout<<"\n";
    cout<<"\n THE PROGRAM USES SINGLE BIT CORRECTION METHOD OFHAMMING CODES";
    cout<<"\n \n";
    cout<<"\n ****************************************************************";
    cout<<"\n \n \n 1.ALPHABET \n \n 2.NUMERIC \n \n 3.EXIT \n \n ENTER THECHOICE::";
    cin>> alp;
    if(alp==1){
      int num,err_bito,err_bitn;
      int bit[]={0,0,0,0,0,0,0,0,0,0,0,0};
      int bits[]={0,0,0,0,0,0,0,0,0,0,0,0};
      cout << "\n \n \n THIS IS CHARACTER ERROR DETECTION ANDCORRECTION PROGRAM";
      char one_char;
      int aq;
      cout << "\n \n \n \n Enter any a CHARACTER of upper or lower case: ";
      cin >> one_char;
      cout << "\n \n The character entered was : " << one_char << '\n';
      aq=(int) one_char;
      cout << "\n \n Its ASCII value is : " << aq << '\n';
      num=aq;
      bit[1]=num%2;
      int a;
      a=num-num%2;
      int b=a/2;
      bit[2]=b%2;
      a=b-b%2;
      b=a/2;
      bit[3]=b%2;
      a=b-b%2;
      b=a/2;
      bit[4]=b%2;
      a=b-b%2;
      b=a/2;
      bit[5]=b%2;
      a=b-b%2;
      b=a/2;
      bit[6]=b%2;
      a=b-b%2;
      b=a/2;
      bit[7]=b%2;
      a=b-b%2;
      b=a/2;
      bit[8]=b%2;
      a=b-b%2;
      b=a/2;
      bit[9]=b%2;
      a=b-b%2;
      b=a/2;
      bit[10]=b%2;
      a=b-b%2;
      b=a/2;
      bit[11]=b%2;
      a=b-b%2;
      b=a/2;
      cout << "\n \n The equivalent binary data is:";
      for( k=11;k>0;k--)
        cout << bit[k];
      cout << endl;
      int r1,r2,r3,r4,r1o,r2o,r3o,r4o,r1n,r2n,r3n,r4n;
      r1=r2=r3=r4=r1o=r2o=r3o=r4o=r1n=r2n=r3n=r4n=0;
      if(bit[1]==1){
        r1++;
        r2++;
      }
      if(bit[2]==1){
        r1++;
        r3++;
      }
      if(bit[3]==1){
        r2++;
        r3++;
      }
      if(bit[4]==1){
        r1++;
        r2++;
        r3++;
      }
      if(bit[5]==1){
        r1++;
        r4++;
      }
      if(bit[6]==1){
        r2++;
        r4++;
      }
      if(bit[7]==1){
        r1++;
        r2++;
        r4++;
      }
      if(bit[8]==1){
        r3++;
        r4++;
      }
      if(bit[9]==1){
        r1++;
        r3++;
        r4++;
      }
      if(bit[10]==1){
        r2++;
        r3++;
        r4++;
      }
      if(bit[11]==1){
        r1++;
        r2++;
        r3++;
        r4++;
      }
      r1o=r1%2;
      r2o=r2%2;
      r3o=r3%2;
      r4o=r4%2;
      cout<<"\n \n The REDUNDENT BITS are: ";
      cout << r1o << r2o << r3o << r4o << endl;
      r1=r2=r3=r4=0;
      int tx_data[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
      int rx_data[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
      cout <<"\n \n Data with REDUNDANT BITS are:";
      tx_data[15]=bit[11];
      cout << tx_data[15];
      tx_data[14]=bit[10];
      cout <<tx_data[14];
      tx_data[13]=bit[9];
      cout << tx_data[13];
      tx_data[12]=bit[8];
      cout << tx_data[12];
      tx_data[11]=bit[7];
      cout << tx_data[11];
      tx_data[10]=bit[6];
      cout << tx_data[10];
      tx_data[9]=bit[5];
      cout << tx_data[9];
      tx_data[8]=r4o;
      cout <<"("<<tx_data[8]<<")";
      tx_data[7]=bit[4];
      cout << tx_data[7];
      tx_data[6]=bit[3];
      cout << tx_data[6];
      tx_data[5]=bit[2];
      cout << tx_data[5];
      tx_data[4]=r3o;
      cout <<"("<< tx_data[4]<<")";
      tx_data[3]=bit[1];
      cout << tx_data[3];
      tx_data[2]=r2o;
      cout <<"("<< tx_data[2]<<")";
      tx_data[1]=r1o;
      cout <<"("<< tx_data[1]<<")";
      cout<<endl;
      cout << "\n \n The TRANSMITTED DATA is: ";
      for(k=15;k>0;k--)
        cout << tx_data[k];
      cout << endl;
      err_bito=((8*r4o)+(4*r3o)+(2*r2o)+(1*r1o)); // ERROR BIT GENERATED
      for(k=15;k>0;k--)
        rx_data[k]=tx_data[k];
      if(rx_data[err_bito]==1)
        rx_data[err_bito]=0;
      else
        rx_data[err_bito]=1;
      cout << "\n \n The Received DATA WITH ERROR is:";
      for(k=15;k>0;k--)
        cout << rx_data[k];
      cout << endl;
      if(rx_data[3]==1){
        r1++;
        r2++;
      }
      if(rx_data[5]==1){
        r1++;
        r3++;
      }
      if(rx_data[6]==1){
        r2++;
        r3++;
      }
      if(rx_data[7]==1){
        r1++;
        r2++;
        r3++;
      }
      if(rx_data[9]==1){
        r1++;
        r4++;
      }
      if(rx_data[10]==1){
        r2++;
        r4++;
      }
      if(rx_data[11]==1){
        r1++;
        r2++;
        r4++;
      }
      if(rx_data[12]==1){
        r3++;
        r4++;
      }
      if(rx_data[13]==1){
        r1++;
        r3++;
        r4++;
      }
      if(rx_data[14]==1){
        r2++;
        r3++;
        r4++;
      }if(rx_data[15]==1){
        r1++;
        r2++;
        r3++;
        r4++;
      }
      r1n=r1%2;
      r2n=r2%2;
      r3n=r3%2;
      r4n=r4%2;
      r1=r2=r3=r4=0;
      if(r1o==r1n){
        if(r2o==r2n){
          if(r3o==r3n){
            if(r4o==r4n)
              cout << "\n \n No Error" << endl;
            else
              cout << "\n \n Not Possible" << endl;
          }else{
            if(r4o==r4n)
              cout << "\n \n Not Possible" << endl;
            else{
              err_bitn=12;
              cout << "\n \n Error in bit 12" << endl;
            }
          }
        }else{
          if(r3o==r3n){
            if(r4o==r4n)
              cout << "\n \n Not Possible" << endl;
            else{
              err_bitn=10;
              cout << "\n \n Error in bit 10" << endl;
            }
          }else{
            if(r4o==r4n){
              err_bitn=6;
              cout << "\n \n Error in bit 6" << endl;
            }else{
              err_bitn=14;
              cout << "\n \n Error in bit 14" << endl;
            }
          }
        }
      }else{
        if(r2o==r2n){
          if(r3o==r3n){
            if(r4o==r4n)
              cout << "\n \n Not Possible" << endl;
            else{
              err_bitn=9;
              cout << "\n \n Error in bit 9" << endl;
            }
          }else{
            if(r4o==r4n){
              err_bitn=5;
              cout << "\n \n Error in bit 5" << endl;
            }else{
              err_bitn=13;
              cout << "\n \n Error in bit 13" << endl;
            }
          }
        }else{
          if(r3o==r3n){
            if(r4o==r4n){
              err_bitn=3;
              cout << "\n \n Error in bit 3" << endl;
            }else{
              err_bitn=11;
              cout << "\n \n Error in bit 11" << endl;
            }
          }else{
            if(r4o==r4n){
              err_bitn=7;
              cout << "\n \n Error in bit 7" << endl;
            }else{
              err_bitn=15;
              cout << "\n \n Error in bit 15" << endl;
            }
          }
        }
      }
      if(rx_data[err_bitn]==1)
        rx_data[err_bitn]=0;
      else
        rx_data[err_bitn]=1;
      cout << "\n \n The CORRECTED DATA is: ";
      for(k=15;k>0;k--)
        cout << rx_data[k];
      cout << endl;
      bits[11]=rx_data[15];
      bits[10]=rx_data[14];
      bits[9]=rx_data[13];
      bits[8]=rx_data[12];
      bits[7]=rx_data[11];
      bits[6]=rx_data[10];
      bits[5]=rx_data[9];
      bits[4]=rx_data[7];
      bits[3]=rx_data[6];
      bits[2]=rx_data[5];
      bits[1]=rx_data[3];
      cout << "\n \n The Received data withot redundant bit is: ";
      for(k=11;k>0;k--)
        cout << bits[k];
      cout << endl;
      double x=2;
      double number=0;
      for(k=11;k>0;k--){
        double z=0;
        z=pow(x, k-1);
        double temp=0;
        temp=bit[k]*z;
        number=number+temp;
      }
      cout << "\n \n The ALPHABET entered was: " << one_char << endl;
    }else{
      if(alp==2){
        int num,numb,err_bito,err_bitn;
        int bit[]={0,0,0,0,0,0,0,0,0,0,0,0};
        int bits[]={0,0,0,0,0,0,0,0,0,0,0,0};
        cout << "\n \n THIS NUMERIC ERROR DETECTION AND CORRECTIONPROGRAM" << endl;
        cout<<"\n \n*******************************************************************************";
        cout<<"\n -:CAUTION:- ";
        cout<<"\n \n SINCE THE PROGRAM HAS BEEN DESIGNED FOR 11BITARRAY,THE PROGRAM WILL NOT ACCEPT ANY VALUES GREATER THAN OREQUAL 2048";
        cout<<"\n \n*******************************************************************************";
        cout << "\n \n \n Enter any decimal number: ";
        cin >> numb;
        if(numb<2048)
          num=numb;
        else{
          cout << "\n" << endl;
          cout << "If u carry on u will get erroneous results" <<endl;
        }
        bit[1]=num%2;
        int a;
        a=num-num%2;
        int b=a/2;
        bit[2]=b%2;
        a=b-b%2;
        b=a/2;
        bit[3]=b%2;
        a=b-b%2;
        b=a/2;
        bit[4]=b%2;
        a=b-b%2;
        b=a/2;
        bit[5]=b%2;
        a=b-b%2;
        b=a/2;
        bit[6]=b%2;
        a=b-b%2;
        b=a/2;
        bit[7]=b%2;
        a=b-b%2;
        b=a/2;
        bit[8]=b%2;
        a=b-b%2;
        b=a/2;
        bit[9]=b%2;
        a=b-b%2;
        b=a/2;
        bit[10]=b%2;
        a=b-b%2;
        b=a/2;
        bit[11]=b%2;
        a=b-b%2;
        b=a/2;
        cout << "\n \n The equivalent binary data is:";
        for( k=11;k>0;k--)
          cout << bit[k];
        cout << endl;
        int r1,r2,r3,r4,r1o,r2o,r3o,r4o,r1n,r2n,r3n,r4n;
        r1=r2=r3=r4=r1o=r2o=r3o=r4o=r1n=r2n=r3n=r4n=0;
        if(bit[1]==1){
          r1++;
          r2++;
        }
        if(bit[2]==1){
          r1++;
          r3++;
        }
        if(bit[3]==1){
          r2++;
          r3++;
        }
        if(bit[4]==1){
          r1++;
          r2++;
          r3++;
        }
        if(bit[5]==1){
          r1++;
          r4++;
        }
        if(bit[6]==1){
          r2++;
          r4++;
        }
        if(bit[7]==1){
          r1++;
          r2++;
          r4++;
        }
        if(bit[8]==1){
          r3++;
          r4++;
        }
        if(bit[9]==1){
          r1++;
          r3++;
          r4++;
        }
        if(bit[10]==1){
          r2++;
          r3++;
          r4++;
        }
        if(bit[11]==1){
          r1++;
          r2++;
          r3++;
          r4++;
        }
        r1o=r1%2;
        r2o=r2%2;
        r3o=r3%2;
        r4o=r4%2;
        cout<<"\n \n The REDUNDENT BITS are: ";
        cout << r1o << r2o << r3o << r4o << endl;
        r1=r2=r3=r4=0;
        int tx_data[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        int rx_data[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        cout<<"\n \n Data with REDUNDANTS BITS are:";
        tx_data[15]=bit[11];
        cout<< tx_data[15];
        tx_data[14]=bit[10];
        cout<< tx_data[14];
        tx_data[13]=bit[9];
        cout<< tx_data[12];
        tx_data[12]=bit[8];
        cout<< tx_data[12];
        tx_data[11]=bit[7];
        cout<< tx_data[11];
        tx_data[10]=bit[6];
        cout<< tx_data[10];
        tx_data[9]=bit[5];
        cout<< tx_data[9];
        tx_data[8]=r4o;
        cout<<"("<< tx_data[8]<<")";
        tx_data[7]=bit[4];
        cout<< tx_data[7];
        tx_data[6]=bit[3];
        cout<< tx_data[6];
        tx_data[5]=bit[2];
        cout<< tx_data[5];
        tx_data[4]=r3o;
        cout<<"("<< tx_data[4]<<")";
        tx_data[3]=bit[1];
        cout<< tx_data[3];
        tx_data[2]=r2o;
        cout<<"("<< tx_data[2]<<")";
        tx_data[1]=r1o;
        cout<<"("<< tx_data[1]<<")";
        cout<<endl;
        cout << "\n \n The TRANSMITTED DATA is: ";
        for(k=15;k>0;k--)
          cout << tx_data[k];
        cout << endl;
        err_bito=((8*r4o)+(4*r3o)+(2*r2o)+(1*r1o)); // ERROR BIT GENERATED
        for(k=15;k>0;k--)
          rx_data[k]=tx_data[k];
        if(rx_data[err_bito]==1)
          rx_data[err_bito]=0;
        else
          rx_data[err_bito]=1;
        cout << "\n \n The Received DATA WITH ERROR is:";
        for(k=15;k>0;k--)
          cout << rx_data[k];
        cout << endl;
        if(rx_data[3]==1){
          r1++;
          r2++;
        }
        if(rx_data[5]==1){
          r1++;
          r3++;
        }
        if(rx_data[6]==1){
          r2++;
          r3++;
        }
        if(rx_data[7]==1){
          r1++;
          r2++;
          r3++;
        }
        if(rx_data[9]==1){
          r1++;
          r4++;
        }
        if(rx_data[10]==1){
          r2++;
          r4++;
        }
        if(rx_data[11]==1){
          r1++;
          r2++;
          r4++;
        }
        if(rx_data[12]==1){
          r3++;
          r4++;
        }
        if(rx_data[13]==1){
          r1++;
          r3++;
          r4++;
        }
        if(rx_data[14]==1){
          r2++;
          r3++;
          r4++;
        }
        if(rx_data[15]==1){
          r1++;
          r2++;
          r3++;
          r4++;
        }
        r1n=r1%2;
        r2n=r2%2;
        r3n=r3%2;
        r4n=r4%2;
        r1=r2=r3=r4=0;
        if(r1o==r1n){
          if(r2o==r2n){
            if(r3o==r3n){
              if(r4o==r4n)
                cout << "\n \n No Error" << endl;
              else
                cout << "\n \n Not Possible" << endl;
            }else{
              if(r4o==r4n)
                cout << "\n \n Not Possible" << endl;
              else{
                err_bitn=12;
                cout << "\n \n Error in bit 12" << endl;
              }
            }
          }else{
            if(r3o==r3n){
              if(r4o==r4n)
                cout << "\n \n Not Possible" << endl;
              else{
                err_bitn=10;
                cout << "\n \n Error in bit 10" << endl;
              }
            }else{
              if(r4o==r4n){
                err_bitn=6;
                cout << "\n \n Error in bit 6" << endl;
              }else{
                err_bitn=14;
                cout << "\n \n Error in bit 14" << endl;
              }
            }
          }
        }else{
          if(r2o==r2n){
            if(r3o==r3n){
              if(r4o==r4n)
                cout << "\n \n Not Possible" << endl;
              else{
                err_bitn=9;
                cout << "\n \n Error in bit 9" << endl;
              }
            }else{
              if(r4o==r4n){
                err_bitn=5;
                cout << "\n \n Error in bit 5" << endl;
              }else{
                err_bitn=13;
                cout << "\n \n Error in bit 13" << endl;
              }
            }
          }else{
            if(r3o==r3n){
              if(r4o==r4n){
                err_bitn=3;
                cout << "\n \n Error in bit 3" << endl;
              }else{
                err_bitn=11;
                cout << "\n \n Error in bit 11" << endl;
              }
            }else{
              if(r4o==r4n){
                err_bitn=7;
                cout << "\n \n Error in bit 7" << endl;
              }else{
                err_bitn=15;cout << "\n \n Error in bit 15" << endl;
              }
            }
          }
        }
        if(rx_data[err_bitn]==1)
          rx_data[err_bitn]=0;
        else
          rx_data[err_bitn]=1;
        cout << "\n \n The CORRECTED DATA is: ";
        for(k=15;k>0;k--)
          cout << rx_data[k];
        cout << endl;
        bits[11]=rx_data[15];
        bits[10]=rx_data[14];
        bits[9]=rx_data[13];
        bits[8]=rx_data[12];
        bits[7]=rx_data[11];
        bits[6]=rx_data[10];
        bits[5]=rx_data[9];
        bits[4]=rx_data[7];
        bits[3]=rx_data[6];
        bits[2]=rx_data[5];
        bits[1]=rx_data[3];
        cout << "\n \n The Recieved data without redundant bit is: ";
        for(k=11;k>0;k--)
          cout << bits[k];
        cout << endl;
        double x=2;
        double number=0;
        for(k=11;k>0;k--){
          double z=0;
          z=pow(x, k-1);
          double temp=0;
          temp=bit[k]*z;
          number=number+temp;
        }
        cout << "\n \n The decimal number entered was:" << number << endl;
      }else{
        cout<<"You are quiting from the program";
      }
    }
  }while(alp!=3);
  return(0);
}

0 个答案:

没有答案