From 8313e24a46eb39708edba20495ce543be227d905 Mon Sep 17 00:00:00 2001 From: rogozinay Date: Mon, 15 Jan 2024 21:07:56 +0300 Subject: [PATCH] Add absolutely new main function --- main.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index ae14a56..30a4847 100644 --- a/main.cpp +++ b/main.cpp @@ -1,29 +1,65 @@ #include -#include -#include "lab04.h" - +#include using namespace std; -int main() { - uint8_t singleByte = 0xAB; - uint8_t byteArray[] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 }; +// Функция для представления nibble (4 бит) в шестнадцатеричной системе +char nibble_to_hex(uint8_t i) { + // Массив цифр в шестнадцатеричной системе + char digits[] = "0123456789abcdef"; - cout << "Print in Hex:" << endl; - print_in_hex(singleByte); + // Проверка на корректность аргумента + assert(0x0 <= i && i <= 0xf); - cout << endl; - print_in_hex(byteArray, sizeof(byteArray)); + // Возвращаем символ для nibble из массива digits + return digits[i]; +} + +// Функция для печати одного байта в шестнадцатеричной и двоичной форме +void print_in_hex(uint8_t byte) { + // Печать в шестнадцатеричной форме + cout << "Hex: " << nibble_to_hex(byte >> 4) << nibble_to_hex(byte & 0xf) << endl; - cout << "\nPrint in Binary:" << endl; - print_in_binary(singleByte); + // Печать в двоичной форме + cout << "Binary: "; + for (int i = 7; i >= 0; --i) { + cout << ((byte >> i) & 1); + } cout << endl; - print_in_binary(byteArray, sizeof(byteArray)); +} + +int main() { + // Тестирование функции nibble_to_hex + //for (int i = 0; i <= 0xf; ++i) { + //assert(nibble_to_hex(i) == nibble_to_hex(i)); + //} + + assert(nibble_to_hex(0x0) == '0'); + assert(nibble_to_hex(0x1) == '1'); + assert(nibble_to_hex(0x2) == '2'); + assert(nibble_to_hex(0x3) == '3'); + assert(nibble_to_hex(0x4) == '4'); + assert(nibble_to_hex(0x5) == '5'); + assert(nibble_to_hex(0x6) == '6'); + assert(nibble_to_hex(0x7) == '7'); + assert(nibble_to_hex(0x8) == '8'); + assert(nibble_to_hex(0x9) == '9'); + assert(nibble_to_hex(0xa) == 'a'); + assert(nibble_to_hex(0xb) == 'b'); + assert(nibble_to_hex(0xc) == 'c'); + assert(nibble_to_hex(0xd) == 'd'); + assert(nibble_to_hex(0xe) == 'e'); + assert(nibble_to_hex(0xf) == 'f'); + + // Печать байта в шестнадцатеричной и двоичной форме + uint8_t test_byte = 0x2f; + print_in_hex(test_byte); return 0; } -int main_calc(){ + +int main_calc() { uint16_t operand1, operand2; char operation; @@ -44,7 +80,7 @@ int main_calc(){ return 0; } - int main_students() { +int main_students() { // Student Student students[3] = { Student("Alice", 2019, 4.5, 0, 3, nullptr),