Edit main_calc.cpp
Этот коммит содержится в:
34
calc_bits.cpp
Обычный файл
34
calc_bits.cpp
Обычный файл
@@ -0,0 +1,34 @@
|
|||||||
|
#include "calc_bits.h"
|
||||||
|
#include "lab04.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
uint16_t perform_bitwise_operation(uint16_t operand1, char operation, uint16_t operand2) {
|
||||||
|
uint16_t result;
|
||||||
|
|
||||||
|
switch (operation) {
|
||||||
|
case '&':
|
||||||
|
result = operand1 & operand2;
|
||||||
|
break;
|
||||||
|
case '|':
|
||||||
|
result = operand1 | operand2;
|
||||||
|
break;
|
||||||
|
case '^':
|
||||||
|
result = operand1 ^ operand2;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cerr << "Invalid operator. Please use '&', '|', or '^'." << std::endl;
|
||||||
|
result = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_calculation(uint16_t result) {
|
||||||
|
std::cout << "Result in Hex: ";
|
||||||
|
print_in_hex(result);
|
||||||
|
std::cout << "\nResult in Binary: ";
|
||||||
|
print_in_binary(result);
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
7
calc_bits.h
Обычный файл
7
calc_bits.h
Обычный файл
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef CALC_BITS_H_INCLUDED
|
||||||
|
#define CALC_BITS_H_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif // CALC_BITS_H_INCLUDED
|
||||||
@@ -32,6 +32,7 @@
|
|||||||
<Add option="-Wall" />
|
<Add option="-Wall" />
|
||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Unit filename="calc_bits.cpp" />
|
||||||
<Unit filename="lab04.h" />
|
<Unit filename="lab04.h" />
|
||||||
<Unit filename="main.cpp" />
|
<Unit filename="main.cpp" />
|
||||||
<Unit filename="source.cpp" />
|
<Unit filename="source.cpp" />
|
||||||
|
|||||||
4
lab04.h
4
lab04.h
@@ -2,12 +2,14 @@
|
|||||||
#define LAB04_H_INCLUDED
|
#define LAB04_H_INCLUDED
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <cstdint>
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
void print_in_hex(uint8_t byte);
|
void print_in_hex(uint8_t byte);
|
||||||
void print_in_hex(const void* data, size_t size);
|
void print_in_hex(const void* data, size_t size);
|
||||||
void print_in_binary(uint8_t byte);
|
void print_in_binary(uint8_t byte);
|
||||||
void print_in_binary(const void* data, size_t size);
|
void print_in_binary(const void* data, size_t size);
|
||||||
|
uint16_t perform_bitwise_operation(uint16_t operand1, char operation, uint16_t operand2);
|
||||||
|
void print_calculation(uint16_t result);
|
||||||
|
|
||||||
#endif // LAB04_H_INCLUDED
|
#endif // LAB04_H_INCLUDED
|
||||||
|
|||||||
21
main.cpp
21
main.cpp
@@ -22,3 +22,24 @@ int main() {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main_calc(){
|
||||||
|
uint16_t operand1, operand2;
|
||||||
|
char operation;
|
||||||
|
|
||||||
|
cout << "Enter the first operand (in hexadecimal): ";
|
||||||
|
cin >> std::hex >> operand1;
|
||||||
|
|
||||||
|
cout << "Enter the bitwise operation (&, |, or ^): ";
|
||||||
|
cin >> operation;
|
||||||
|
|
||||||
|
cout << "Enter the second operand (in hexadecimal): ";
|
||||||
|
cin >> std::hex >> operand2;
|
||||||
|
|
||||||
|
uint16_t result = perform_bitwise_operation(operand1, operation, operand2);
|
||||||
|
|
||||||
|
cout << "\nCalculation Result:" << endl;
|
||||||
|
print_calculation(result);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "lab04.h"
|
#include "lab04.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "lab04.h"
|
#include "calc_bits.h"
|
||||||
|
|
||||||
void print_in_hex(uint8_t byte) {
|
void print_in_hex(uint8_t byte) {
|
||||||
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << " ";
|
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << " ";
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user