Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

лабы 1-й сем / лаба 1 / 1_lab_11_team

.cpp
Скачиваний:
4
Добавлен:
09.01.2023
Размер:
3.33 Кб
Скачать
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int checkLd(char *s){
    char *notLd;

    strtold(s, &notLd);

    if (strlen(notLd) > 0) return 0;
    else return 1;
}

void charToBytes(char n, bool *cBytes){
    for(int i = 8; i >= 0; i--) {
        cBytes[7 - i] = ((n >> i) & 1);
    }
}

int ldToBytes(long double n, bool *lBytes){
    int indexer = 0;

    for (int i = 0;i<64;i++){
        lBytes[i] = false;
    }

    union ld{
        char z[8];
        long double num;
    }rs{};

    rs.num = n;
    for(int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++)
        {
            lBytes[indexer++] = ((rs.z[8 - i - 1] >> (8 - j - 1)) & 1);
        }
    }

    return indexer;
}

char bytesToChar(bool *cBytes){
    char result;

    for (int i = 0; i < 8; i++){
        result = result << 1;
        result = result | cBytes[i];
    }

    return result;
}

long double bytesToLong(bool *lBytes){
    char charLBytes[64];
    long double result;

    for (int i = 0; i < 64; i++)
        charLBytes[i] = lBytes[i];
    memcpy(&result, charLBytes, sizeof(long double));

    return result;
}

void mirror(bool *bytes, int len, int n, int b){
    int temp = 0; // 2 - 1; 3 - 2; 4 - 2; 5 - 3; 6 - 3; 7 - 4;
    n--;
    b = len - b;

    for(int i = b, j = (b + n), k = 0; k < ((n / 2) + (n % 2)); i++, j--, k++){
        temp = bytes[i];
        bytes[i] = bytes[j];
        bytes[j] = temp;
    }

}

int main(){
    char *trash;
    int counter = 0, b = 0, n = 0, key = 0;
    char inp[64] = {};
    bool cBytes[8] = {};
    bool lBytes[64] = {};
    char A = 0;
    long double B = 0;

    cout << "char: ";
    cin >> A;

    cout << "long double B: ";
    cin >> inp;
    while (!checkLd(inp)){
        cout << "long double B: ";
        cin >> inp;
    }
    B = strtold(inp, &trash);

    charToBytes(A, cBytes);

    cout << "Char representation in binary: ";
    for (bool cByte : cBytes){
        if (counter && (counter % 4 == 0)) cout << " ";
        cout << cByte;
        counter++;
    }
    cout << endl;

    ldToBytes(B, lBytes);
    counter = 0;
    cout << "Long double representation in binary: ";
    for (bool lByte : lBytes)
    {
        cout << lByte;
        counter++;
        if (counter % 4 == 0) cout << ' ';
    }
    cout << endl;

    cout << "Use algorithm in..." << endl << "1 - char" << endl << "2 - long double" << endl;
    while((key < 1) || (key > 2)) {
        cout << "Your choice: ";
        cin >> key;
    }
    cout << "type begin: ";
    cin >> b;
    cout << "type num of symbols: ";
    cin >> n;
    if(key == 1) {
        mirror(cBytes, 8, n, b);
        counter = 0;
        cout << "Result: ";
        for (bool cByte : cBytes){
            if (counter && (counter % 4 == 0)) cout << " ";
            cout << cByte;
            counter++;
        }
        cout << endl;
    }
    else{
        mirror(lBytes, 64, n, b);
        counter = 0;
        cout << "Result: ";
        for (bool lByte : lBytes){
            if (counter && (counter % 4 == 0)) cout << " ";
            cout << lByte;
            counter++;
        }
        cout << endl;
    }

    cout << endl << "char: ";
    cout << bytesToChar(cBytes) << endl;
    cout << endl << "long double: ";
    cout << bytesToLong(lBytes) << endl;

    return 0;
}
Соседние файлы в папке лаба 1