Имя пользователя:
Пароль:
 

Показать сообщение отдельно

Новый участник


Сообщения: 2
Благодарности: 0

Профиль | Отправить PM | Цитировать


lxa85, программу уже помогли написать, она ниже. теперь бы ее как нибудь по проще сделать нужно бы. посмотри пожалуйста может что сможешь сделать.

читать дальше »
Код: Выделить весь код
#include <iostream>
#include <conio.h>
#include <vector>
using namespace std;

void ShowHex(unsigned int dec) {
    vector<char> hex;
    char hex_num[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
                        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
                       };

    while (dec/16 != 0) {
       hex.push_back(hex_num[dec%16]);
       dec = dec/16;
    }
    hex.push_back(hex_num[dec%16]);
    for (int i = hex.size()-1; i >= 0; i--) {
       cout << hex.at(i);
    }
}

unsigned char* GenerateBytes(unsigned int dec) {
   unsigned char *x = new unsigned char[4];
   x[0] = dec >> 24;
   x[1] = dec >> 16;
   x[2] = dec >> 8;
   x[3] = dec;
   return x;
}

bool CheckByte(unsigned char byte) {
    //195 = 11000011
    //66 = 01000010
    //129 = 10000001
    if (  (byte & 195 == 0) || (byte & 195 == 66) ||
          (byte & 195 == 129) || (byte & 195 == 195) ) {
        return true;
    }
    else
        return false;
}

int Castling(unsigned int dec) {
    unsigned char *bytes = GenerateBytes(dec);
    int number, number_byte[4];
    int count_symmetric = 4;
    for (int i = 0; i < 4; i++) {
       if (CheckByte(bytes[i]) == false) {
          number_byte[i] = int(bytes[i]) << 8*(3-i);
          count_symmetric--;
       }

    }

    //перестановка симметричных байтов
    if (count_symmetric == 1) {
       for (int i = 0; i < 4; i++) {
          if (CheckByte(bytes[i]) == true) {
             number_byte[i] = int(bytes[i]) << 8*(3-i);
          }
          break;
       }
    }
    else if (count_symmetric == 2 || count_symmetric == 3) {
       for (int i = 0; i < 4; i++) {
          if (CheckByte(bytes[i]) == true) {
             for (int j = 3; j > 0; j--) {
                if (CheckByte(bytes[j]) == true) {
                   number_byte[i] = int(bytes[j]) << 8*(3-i);
                   number_byte[j] = int(bytes[i]) << 8*(3-j);
                   break;
                }
             }
             break;
          }
       }
    }
    else if (count_symmetric == 4) {
       for (int i = 0; i < 4; i++) {
          number_byte[i] = int(bytes[3-i]) << 8*(3-i);
       }
    }

    number = number_byte[0] | number_byte[1] | number_byte[2] | number_byte[3];
    return number;
}
//---------------------------------------------------------------------------

int main()
{
unsigned int x, x_end;
cin >> x;

ShowHex(x);
cout << endl;
x_end = Castling(x);
ShowHex(x_end);
getch();
    return 0;
}

Отправлено: 15:37, 03-12-2011 | #3