/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab2/main.cpp
462 строки
16 KB
danillyapunov
unsigned shiftDigits
15 апр 2025, 17:26
15 апр 2025, 17:26
fd8d803
Код
Авторство
О чём код?
/* * ������ୠ� ࠡ�� �2 * �������� ��� � ��祥 * * Developer: Danil Lyapunov * OS: Windows 11 * locale (������): IBM866 / CP866 / csIBM866 */ #include<iostream> #include<cmath> #include <limits> #include <bitset> #define BITS_BYTE 8 auto decToBin(unsigned n) -> unsigned { unsigned ans = 0; unsigned remainder, i = 1; while(n!=0){ remainder = n % 2; ans += remainder*i; i = i * 10; n = n / 2; } return ans; } int main() { #if 0 std::cout << "������� 1. �������� ��� ���� �����쭮�� ���" << std::endl; /* ����� ?�������� ��� ���� �����쭮�� ���?. � �ᯮ�짮������ ����祭��� ������ � �뢮��, � ࠡ��� � ⨯��� ������, ᮧ���� �ணࠬ��, ����� �뢮��� ���ଠ�� � ��襬 �����쭮� ���: ������� ���� ���짮��⥫�: ������ ������: ������ ���: ������ ���: � ��稭� ������ 1, ���騭� 0: ������ �����쭮�� ��� a - �� ��㫥 �ப� b - �� ������� ����� ⥫� � - �� ������� ����� ⥫� � ��⮬ ������ ���짮������ �ணࠬ��� ������ ���� ����⭮, ���짮��⥫� ����⭮ ������ �� ������� , �� ��������, �.� ����ᠭ� �।��ᠭ�� �� �������, ࠧ��୮��� �����, ����⨥ Enter �� ����� � �. � ⠪�� � �뢮���. */ std::cout << "������ ������: "; unsigned int age; while(true) { std::cin >> age; if ((age < 1u) || (age > 120u)) { std::cout << "���ࠢ���� ����. ������ ������ � ��������� �� 1 ���� �� 120 ���" << '\n'; continue; } else { break; } } std::cout << "������ ��� � ��: "; unsigned int weight; while(true) { std::cin >> weight; if ((weight < 20u) || (weight > 300u)) { std::cout << "���ࠢ���� ����. ������ ��� � ��������� �� 20 �� 300 ��" << '\n'; continue; } else { break; } } std::cout << "������ ��� � �: "; unsigned int height; while(true) { std::cin >> height; if ((height < 100u) || (height > 300u)) { std::cout << "���ࠢ���� ����. ������ ��� � ��������� �� 100 �� 300 �" << '\n'; continue; } else { break; } } std::cout << "� �� ��稭� ������ 1, � �� ���騭� - 0: "; unsigned int gender; while(true) { std::cin >> gender; if ((gender < 0u) || (gender > 1u)) { std::cout << "���ࠢ���� ����. ������ 1, � �� ��稭� ��� 0, � �� ���騭�" << '\n'; continue; } else { break; } } char calcOption; std::cout << "������ ���� ���� �����쭮�� ���:\n"; std::cout << "a - �� ��㫥 �ப�\n"; std::cout << "b - �� ������� ����� ⥫�\n"; std::cout << "� - �� ������� ����� ⥫� � ��⮬ ������\n"; while(true) { std::cin >> calcOption; if (!((calcOption == 'a') || (calcOption == 'b') || (calcOption == 'c'))) { std::cout << "���ࠢ���� ����. ������ ���� �� ��権 - �, b ��� �\n"; continue; } else { break; } } unsigned int idealWeight; if (calcOption == 'a') { if (gender == 1u) { idealWeight = (height - 100u) * 0.9f; } else { idealWeight = (height - 100u) * 0.85f; } std::cout << "�����᭮ ��㫥 �ப�, ��� ������� ��� ��⠢���: " << idealWeight << " ��" << '\n'; } float bodyMassIndexMin = 18.5, bodyMassIndexMax = 25; int idealWeightMin, idealWeightMax; if (calcOption == 'b') { idealWeightMin = round(bodyMassIndexMin * height * height / 10000); idealWeightMax = round(bodyMassIndexMax * height * height / 10000); std::cout << "��室� �� ������ ����� ⥫�, ��� ������� ����� � ���ࢠ�� �� " << idealWeightMin << " �� " << idealWeightMax << " ��\n"; } float bodyMassIndex; if (calcOption == 'c') { if (gender == 1u) { if ((age >= 19u) && (age <= 24u)) { bodyMassIndex = 21.4f; } else if ((age > 24u) && (age <= 34u)) { bodyMassIndex = 21.6f; } else if ((age > 34u) && (age <= 44u)) { bodyMassIndex = 22.9f; } else if ((age > 44u) && (age <= 54u)) { bodyMassIndex = 25.8f; } else if (age > 54u) { bodyMassIndex = 26.6f; } } if (gender == 0u) { if ((age >= 19u) && (age <= 24u)) { bodyMassIndex = 19.5f; } else if ((age > 24u) && (age <= 34u)) { bodyMassIndex = 23.2f; } else if ((age > 34u) && (age <= 44u)) { bodyMassIndex = 23.4f; } else if ((age > 44u) && (age <= 54u)) { bodyMassIndex = 25.2f; } else if (age > 54u) { bodyMassIndex = 27.3f; } } idealWeight = round(bodyMassIndex * height * height / 10000u); std::cout << "��室� �� ������ ����� ⥫� � ��⮬ ������, ��� ������� ��� ��⠢���: " << idealWeight << " ��" << '\n'; } #endif #if 0 std::cout << "������� 2" << std::endl; /* ������� 2. �࣠����� �뢮� ࠧ�� �᭮���� ⨯�� ������ C++ (�����᪨�, 楫�����, ᨬ����� � �.�. ? ��� �� ���� ⨯��) � �����. �ਬ�� �뢮��: int: ___ bytes; �࣠����� ⠪�� �뢮� ࠧ�� ⨯�� ������ � �ᯮ�짮������ ����䨪�� ����� � ࠧ��� (unsigned/signed; short/long/long long ? ��� �� ��ᥬ� �뢮��� �� ࠧ�� ����䨪���). �ਬ�� �뢮��: unsigned int: ___ bytes; �࣠����� �뢮� ����� ��������� ⨯�� ������ � hex, dec, bin (��� �� ���� ⨯��): ����� �������� int: �� ____________ �� _____________;*/ std::cout << '\n'; std::cout << "������� �᭮���� ⨯�� ������" << std::endl; std::cout << '\n'; std::cout << "bool:\t\t" << sizeof(bool) << " bytes" << std::endl; std::cout << "char:\t\t" << sizeof(char) << " bytes" << std::endl; std::cout << "int:\t\t" << sizeof(int) << " bytes" << std::endl; std::cout << "float:\t\t" << sizeof(float) << " bytes" << std::endl; std::cout << "double:\t\t" << sizeof(double) << " bytes" << std::endl; std::cout << '\n'; std::cout << '\n'; std::cout << "���� ��������� �᭮���� ⨯�� ������" << std::endl; std::cout << '\n'; std:: cout << "����� �������� bool: �� " << std::numeric_limits<bool>::min() << " �� " << std::numeric_limits<bool>::max() << std::endl; std:: cout << "����� �������� char: �� " << SCHAR_MIN << " �� " << SCHAR_MAX << std::endl; std:: cout << "����� �������� int: �� " << INT_MIN << " �� " << INT_MAX << std::endl; std:: cout << "����� �������� float: �� " << std::numeric_limits<float>::min() << " �� " << std::numeric_limits<float>::max() << std::endl; std:: cout << "����� �������� double: �� " << std::numeric_limits<double>::min() << " �� " << std::numeric_limits<double>::max() << std::endl; return 0; #endif #if 0 std::cout << "������� 3" << std::endl; /*������� 3. ������� ������ �।������ ���짮��⥫� ����� 10 �ᥫ � ������饩 �窮�, � ��⮬ �뢥�� �� ��࠭ �� �।��� ��䬥���, �ᯮ���� � � ������饩 �窮�. �।��� ��䬥��� ? �� �㬬� ���쪨� ������, ࠧ���� �� ������⢮ ��� ������. */ float nums[10]; float sum=0; int counter = 0; for (int i=0; i<10; ++i){ std::cout<<"������ " << i+1 << "-� �: "; std::cin >> nums[i]; sum += nums[i]; ++counter; } std::cout << "�।��� ��䬥��� ��������� �ᥫ ࠢ�� " << sum / counter << '.'; #endif #if 0 std::cout << "������� 4" << std::endl; /* ������� 4. ������� ������ ��ॢ����� ���騥 � �� �������饩 ����� � ��ᯮ���樠���� ������ � �⨫� �몠 C++ (�ᯮ���� �㪢� �, ��� ᨬ��� ��ᯮ����). ��।����, �쪮 ������ ��� ����� ������ �� ����� �ᥫ, � �뢥��� �� �� ��࠭: 34.50 0.004000 123.005 146000 */ // ���塞 ���ᨢ �ᥫ � ������饩 �窮� double arr[] = {34.50, 0.004000, 123.005, 146000}; std::cout << '\n'; std::cout << "�� � ��ᯮ���樠�쭮� �ଥ:\n"; // �뢮��� �� ������⥫쭮 � ��ᯮ���樠�쭮� �ଥ 㪠������ ������⢠ ������ ��� std::cout << "�� " << std::scientific << arr[0] << " ����� " << 4 << " ����騥 ����" << std::endl; std::cout << "�� " << std::scientific << arr[1] << " ����� " << 4 << " ����騥 ����" << std::endl; std::cout << "�� " << std::scientific << arr[2] << " ����� " << 6 << " ������ ���" << std::endl; std::cout << "�� " << std::scientific << arr[3] << " ����� " << 3 << " ����騥 ����" << std::endl; std::cin.get(); /* TODO: � ���쭥�襬 ����室��� ॠ�������� ������ ������ ���. * �।������⥫쭮, ����室��� �८�ࠧ����� ����祭��� ��ᯮ���樠���� ������ � * � ��ப� � �� ����� 横�� ���� ���� ��ᯮ����� � �ᯮ������� �㫥� */ #endif #if 1 std::cout << "������� 5" << std::endl; /* 1) �।������� १���� ����� ��ࠦ���� ⥮���᪨: (true && true) || false (false && true) || true (false && true) || false || true (5 > 6 || 4 > 3) && (7 > 8) !(7 > 6 || 3 > 4) �������� �뢮� १���⮢ ��� ��ࠦ���� � �ணࠬ��, �⮡� ����� ��- ���쭮��� �� �।���������. 2) ������ 6 �����᪨� ����⠭� (� ������� const, constexpr, ���� ����ᮢ) ��� ���- ��� ���� ��ࠦ����. ��� ������� ��ࠦ���� ⠪�� ������ 6 楫������ ����⠭� � ᮡ�⢥���� 楫������ ⨯� (� ������� enum). �������� �뢮� १���⮢ ����� ��ࠦ����, ����⠢�� � ��� ��� ����⠭�� ����� �ய�� (������): (( _ && _ ) || ( !_) && (_||_)) ((_ &&_ ) || (_&&_) || (!_)) ((_ || _) && (_ || _) && (_||_)) ((_ >_) && (_<_) && (_!=_)) 3) ����� �ࠢ��쭮��� ������� �� ��࣠�� �� �ࠪ⨪�. ������ �� ��࣠�� � ⥪�⮢�� ����� ����� ���騩 ���: �� (a � b) = (�� a) ��� (�� b) �� (a ��� b) = (�� a) � (�� b) ������ ��� �����᪨� ��६���� a � b � ॠ����� �ணࠬ��, ����� �����뢠�� ࠢ���⢮ ����� � �ࠢ�� ��⥩ � ��ࢮ� � �� �� ������ �� ��࣠��. 4) �������� ���� 楫������ ��६����� x, y, z, v �१ ���᮫�. ����� ����⮢�� �������� ��६����� logicValue. ��� �।�⮨� ॠ�������� ���騥 ��ࠦ����. 1. x = 3 + 4 + 5; 2. x = y = z; 3. z *= ++y + 5; 4. logicValue = x || y && z || v. �� ॠ����樨 � ���������� � ������� ��ࠦ���� ��⪮ ���᭨�, �� ������ ������ ������. �� �믮������ ��ࠦ���� 1, 2, 3 ����室��� �뢥�� � ���᮫� ���祭�� ���� ��६�����: x, y, z (�.�. 3 ࠧ�). �� �믮������ ��ࠦ���� 4 ����室��� �뢥�� � ���᮫� ���祭�� ��६����� logicValue. ����� �� ��ࠦ���� 4: ����� ���祭�� ������ ����� ��६���� x, y, z, v, �⮡� ��६����� logicValue �ਭ ���祭�� false? �⢥� �� ��� ����� ����� ��⠢��� � �������ਨ � ����, ���� ᪠���� �� �६� �����. 5) ������ �ணࠬ��, ����� ���� ���짮��⥫� ����� � � ��������� �� 0 �� 2^n, ��� n=4, ��⥬ ���� ���짮��⥫� ����� � �� ��� ����室��� ᤢ����� � �����. ������� �뢥��: � �����筮� , � ����୮� ����, 16-� ����, 8-� ����. 6) �������� �뢮� �ਮ��� ��ࠦ���� � ���樠⨢����: int res = a + b * 1 ? 128/5; int res = a | b >> 1; int res = a /b* k; */ std::cout << '\n'; std::cout << "1) �뢮� १���⮢ �����᪨� ��ࠦ����:" << std::endl; std::cout << "(true && true) || false = " << std::boolalpha << ((true && true) || false) << std::endl; std::cout << "(false && true) || true = " << std::boolalpha << ((false && true) || true) << std::endl; std::cout << "(false && true) || false || true = " << std::boolalpha << ((false && true) || false || true) << std::endl; std::cout << "(5 > 6 || 4 > 3) && (7 > 8) = " << std::boolalpha << ((5 > 6 || 4 > 3) && (7 > 8)) << std::endl; std::cout << "!(7 > 6 || 3 > 4) = " << std::boolalpha << (!(7 > 6 || 3 > 4)) << std::endl; std::cout << '\n'; std::cout << "2) �뢮� १���⮢ ��ࠦ���� � ����⠢������ ����⠭� ����� �ய��:" << std::endl; const bool m = 0, n = 1, p = 0, q = 1, r = 0, s = 1; std::cout << "m = " << std::boolalpha << m << ", " << "n = " << std::boolalpha << n << ", " << "p = " << std::boolalpha << p << ", " << "q = " << std::boolalpha << q << ", " << "r = " << std::boolalpha << r << ", " << "s = " << std::boolalpha << s << std::endl; std::cout << "((m&&n)||(!p)&&(q||r)) = " << std::boolalpha << ((m&&n)||((!p)&&(q||r))) << std::endl; std::cout << "((m&&n)||(p&&q)||(!r)) = " << std::boolalpha << ((m&&n)||(p&&q)||(!r)) << std::endl; std::cout << "((m||n)&&(p||q)&&(r||s)) = " << std::boolalpha << ((m||n)&&(p||q)&&(r||s)) << std::endl; enum: bool { g = 0, h = 1, i = 0, j = 1, k = 0, l = 1, }; std::cout << "((g>h) && (i<j) && (k!=l)) = " << std::boolalpha << ((g >h) && (i<j) && (k!=l)) << std::endl; std::cout << '\n'; std::cout << "3) ��ઠ �ࠢ��쭮�� ������� �� ��࣠�� �� �ࠪ⨪�. �ணࠬ�� �뢥��� true, � ����� �믮������" << std::endl; const bool a = 1; const bool b = 0; std::cout << "����� �� ��࣠��: �� (a � b) = (�� a) ��� (�� b) - " << (!(a&&b) == (!a || !b)) << std::endl; std::cout << "����� �� ��࣠��: �� (a ��� b) = (�� a) � (�� b) - " << (!(a||b) == (!a && !b)) << std::endl; std::cout << '\n'; std::cout << "4) ��������� ����� 楫������ ��६����� x, y, z, v �१ ���᮫�" << std::endl; int x, y, z, v; bool logicValue; std::cout << "������ x: " << '\n'; std::cin >> x; // ������ � 12 = 3 + 4 + 5, ��⠫쭮� �������� ��⮬���᪨ �� ᮮ⢥����騬 ��ࠦ����, �� v std::cout << "������ y: " << '\n'; std::cin >> y; std::cout << "������ z: " << '\n'; std::cin >> z; std::cout << "������ v: " << '\n'; std::cin >> v; x = 3 + 4 + 5; std::cout << "x = " << x << ", y = " << y << ", z = " << z << '\n'; x = y = z; std::cout << "x = " << x << ", y = " << y << ", z = " << z << '\n'; z *= ++y + 5; std::cout << "x = " << x << ", y = " << y << ", z = " << z << '\n'; std::cout << "x = " << x << std::endl; std::cout << "y = " << y << std::endl; std::cout << "z = " << z << std::endl; logicValue = (x || (y && z) || v); std::cout << "logicValue = " << logicValue << std::endl; //�⮡� १���� �� false, ����室��� �⮡� � = 0, y ��� z - ��� �� ���� �� ��� �� ࠢ�� 0, � v = 0 unsigned number; unsigned shiftDigits; std::cout << '\n'; std::cout << "5) ���� � � ��������� �� 0 �� 2^n, ��� n=4 � �뢮� ��� � ࠧ����� ��⥬�� �����" << std::endl; std::cout << "������ � �� 0 �� 16: "; std::cin >> number; std::cout << '\n'; std::cout << "������ �� �쪮 ࠧ�冷� ����室��� ᤢ����� �� � �����: "; std::cin >> shiftDigits; number = (number << shiftDigits); unsigned bnumber = decToBin(number); unsigned tmp; tmp=number; std::cout << '\n'; std::cout << "����祭��� � � �����筮� ���� "<< std::dec <<number<<std::endl; std::cout << "����祭��� � � ����୮� ����, ��������� �१ �㭪�� "<< bnumber << std::endl; std::bitset<sizeof(tmp) * BITS_BYTE> morebits_32(tmp); std::cout << "����祭��� � � ����୮� ����, ��������� � �ᯮ�짮������ macroobject "<<morebits_32<<std::endl; std::cout << "����祭��� � � ��⭠����筮� ���� "<< std::hex <<number<<std::endl; std::cout << "����祭��� � � ���쬥�筮� ���� "<< std::oct <<number<<std::endl; std::cout << '\n'; std::cout << "5) ��������� �뢮�� �ਮ��� ��ࠦ���� � ���樠⨢����: "<< std::endl; std::cout << "��ࠦ���� res = a + b * 1 ? 128/5 �믮������ � ���饬 ���浪�: res = a + (b * 1) ? (128/5)"<< std::endl; std::cout << "��ࠦ���� res = a | b >> 1 �믮������ � ���饬 ���浪�: res = a | (b >> 1)"<< std::endl; std::cout << "��ࠦ���� res = a/b* k; �믮������ � ���饬 ���浪�: res = (a/b)* k;"<< std::endl; #endif return 0; }