二进制到格雷码,反之亦然

时间:2013-04-19 14:40:03

标签: c++ binary gray-code

所以我想编写一个程序,它将二进制值作为输入并将其转换为格雷码,反之亦然。

这是我最初写的:

#include <iostream>

using namespace std;

int main()
{

    int Choice;
    bool g0,g1,g2,g3,b0,b1,b2,b3;

    cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
    cin>>Choice;

    if(Choice==1)
    {

        cout<<"Enter the Binary Bits." << endl;

        cin>>b0>>b1>>b2>>b3;


        cout<<"Orginal Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;

        g3=b3;
        g2=b3^b2;
        g1=b2^b1;
        g0=b1^b0;

        cout<<"Converted Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
    }
    else if(Choice==2)
    {

        cout<<"Enter The Gray Code Bits." << endl;

        cin>>g0>>>g1>>g2>>g3;


        cout<<"Original Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;

        b3=g3;
        b2=b3^g2;
        b1=b2^g1;
        b0=b1^g0;

        cout<<"Converted Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
    }

    return 0;
}

现在这适用于4位但是

  1. 我希望用户在运行时确定输入的二进制/格雷码的大小。

    我做了一些研究并发现在这种情况下使用向量但是因为在大学里我们刚开始使用C ++我不熟悉向量甚至数组。还有什么我可以用来实现它吗?如果没有,有人可以告诉我如何使用载体吗?

  2. 其次我想把输入放在一行没有空格。

    示例:

    1011而不是1 0 1 1或在单独的一行中为每个位输入。

  3. 现在我也意识到我不会知道没有。预先进行比特,因此用于实现XOR运算的比特公式也将被改变。是否可以声明二进制和格雷码bool变量,并以某种方式对那些变量执行XOR运算而不是使用更简单的语句对各个位执行,没有什么复杂的?

2 个答案:

答案 0 :(得分:1)

你可以一次读取一个字符串并评估一个字符,让你开始这样的事情:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    std::string input
    int Choice;

    cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
    cin>>Choice;
    cout << "Enter input string: ";
    cin >> input;

    if(Choice==1)
        Gray2Binary(input.c_str(), input.length());
    else
        Binary2Gray(input.c_str(), input.length());

    return 0;
}

void Gray2Binary(char s[], int n)
{
    int state = s[0]=='1' ? 1 : 0;
    int i=0;

    do {
        if (i > 0)
            state ^= s[i++]=='1' ? 1 : 0;

        cout << state;
    }while (i<n);

    cout << endl;
}

答案 1 :(得分:0)

编写,测试和调试花了我30分钟。

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

void printReverse(bool* arr, int size){
    for(int i = size - 1; i >= 0 ; i--)
        cout << arr[i];
    cout << endl;
}
int main(){
    int choice;
    string str;

    cout << "For Binary To Gray Code Enter 1." << endl
        << "For Gray Code to Binary Enter 2." << endl;
    cin >> choice;

    if( choice != 1 && choice != 2)
        return 1;

    if(choice == 1) cout << "Enter the Binary Bits." << endl;
    else cout << "Enter the Gray Code Bits." << endl;

    cin >> str;

    int size = str.size();
    bool arr[size];

    for(int i = 0; i < size; i++)
        if(str[i] == '0')
            arr[i] = false;
        else
            arr[i] = true;

    if(choice == 1) cout << "Orginal Binary Form: ";
    else cout << "Orignal Gray Code Form: ";

    printReverse(arr, size);

    if(choice == 1){
        for(int i = 0; i < size - 1 ; i++) // explain 1
            arr[i] = arr[i] ^ arr[i + 1];
        cout << "Converted Gray Code Form: ";
        printReverse(arr, size);
    } else {
        for(int i = str.size() - 2; i >= 0 ; i--) // explain 2
             arr[i] = arr[i] ^ arr[i + 1];
        cout << "Converted Binary Form: ";
        printReverse(arr, size);
    }
    system("pause");
    return 0;
}

printReverse :(以4位为例)

cout << arr[3]; // cout << b3; or cout << g3;
cout << arr[2]; // cout << b2; or cout << g2;
cout << arr[1]; // cout << b1; or cout << g1;
cout << arr[0]; // cout << b0; or cout << g0;

解释1 :(以4位为例)

// at the beggining, arr[0], arr[1], arr[2], arr[3] are b0, b1, b2, b3
arr[0] = arr[0] ^ arr[1]; // g0 = b0 ^ b1; // arr[0] becomes g0 after assignment
arr[1] = arr[1] ^ arr[2]; // g1 = b1 ^ b2; // arr[1] becomes g1 after assignment
arr[2] = arr[2] ^ arr[3]; // g2 = b2 ^ b3; // arr[2] becomes g2 after assignment
arr[3] = arr[3];          // g3 = b3;      // arr[3] becomes g3 after assignment

解释2 :(以4位为例)

// at the beggining, arr[0], arr[1], arr[2], arr[3] are g0, g1, g2, g3
arr[3] = arr[3];          // b3 = g3;      // arr[3] becomes b3 after assignment
arr[2] = arr[2] ^ arr[3]; // b2 = g2 ^ b3; // arr[2] becomes b2 after assignment
arr[1] = arr[1] ^ arr[2]; // b1 = g1 ^ b2; // arr[1] becomes b1 after assignment
arr[0] = arr[0] ^ arr[1]; // b0 = g0 ^ b1; // arr[0] becomes b0 after assignment