SHARE
TWEET

Untitled

a guest Oct 21st, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include <string>
  6. #include <Windows.h>
  7. using namespace std;
  8.  
  9. const short int color1 = 10;
  10. const short int color0 = 6;
  11. const short int colordef = 15;
  12.  
  13. long double degree(int deg, int number) {
  14.     long double res = 1;
  15.     for (int i = 0; i < abs(deg); i++) {
  16.         res *= number;
  17.     }
  18.     if (deg < 0)
  19.         res = 1 / res;
  20.     return res;
  21. }
  22.  
  23. void numberEnter(string& str, int numberSystem, char& sign) {
  24.     char ch = 0;
  25.     while (ch != 13) {
  26.         ch = _getch();
  27.         if (ch == '-') {
  28.             sign *= -1;
  29.             cout << ch;
  30.         }
  31.         else {
  32.             if (numberSystem > 36) {
  33.                 if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch < numberSystem + '0' + 7 + 6)) {
  34.                     cout << ch;
  35.                     str += ch;
  36.                 }
  37.             }
  38.             else {
  39.                 if (numberSystem > 10) {
  40.                     if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch < numberSystem + '0' + 7)) {
  41.                         cout << ch;
  42.                         str += ch;
  43.                     }
  44.                 }
  45.                 else {
  46.                     if (ch >= '0' && ch < numberSystem + '0') {
  47.                         cout << ch;
  48.                         str += ch;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
  55.  
  56. void fractionalNumberEnter(string& str, int numberSystem, char& sign, int& dotPos) {
  57.     char ch = 0;
  58.     int k = 0;
  59.     while (ch != 13) {
  60.         ch = _getch();
  61.         if (ch == '.' && dotPos == -1) {
  62.             dotPos = k;
  63.             cout << ch;
  64.             str += ch;
  65.             k++;
  66.         }
  67.         else {
  68.             if (ch == '-') {
  69.                 sign *= -1;
  70.                 cout << ch;
  71.             }
  72.             else {
  73.                 k++;
  74.                 if (numberSystem > 36) {
  75.                     if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch < numberSystem + '0' + 7 + 6)) {
  76.                         cout << ch;
  77.                         str += ch;
  78.                     }
  79.                 }
  80.                 else {
  81.                     if (numberSystem > 10) {
  82.                         if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch < numberSystem + '0' + 7)) {
  83.                             cout << ch;
  84.                             str += ch;
  85.                         }
  86.                     }
  87.                     else {
  88.                         if (ch >= '0' && ch < numberSystem + '0') {
  89.                             cout << ch;
  90.                             str += ch;
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
  98.  
  99. void charEnter(string& charEnter) {
  100.     char ch = 0;
  101.     while (ch != 13/*return*/) {
  102.         ch = _getch();
  103.         if (ch >= '0' && ch <= '9') {
  104.             cout << ch;
  105.             charEnter += ch;
  106.         }
  107.         if ((charEnter[0] > '2' && charEnter.length() >= 2) || (charEnter.length() == 3))
  108.             break;
  109.     }
  110. }
  111.  
  112. void printAlphabet(int numberSystem) {
  113.     cout << "Admissible characters: ";
  114.     for (int i = 0; i < numberSystem; i++) {
  115.         if (i < 10)
  116.             cout << "\t" << char(i + '0') << "-" << i << " ";
  117.         if (i >= 10 && i < 36)
  118.             cout << "\t" << char(i + '0' + 7) << "-" << i << " ";
  119.         if (i >= 36)
  120.             cout << "\t" << char(i + '0' + 7 + 6) << "-" << i << " ";
  121.     }
  122.     cout << endl;
  123. }
  124.  
  125. void bitChange(int& number, HANDLE hConsole) {
  126.     cout << "Enter index of the 1st bit: ";
  127.     int index, temp;
  128.     cin >> index;
  129.     if (index >= sizeof(int) * 8)
  130.         return;
  131.     char ch = 0;
  132.     cout << "Enter bits: ";
  133.     for (int i = index; i < sizeof(int) * 8; i++) {
  134.         ch = _getch();
  135.         switch (ch) {
  136.         case '1':
  137.             SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color1));
  138.             cout << ch;
  139.             temp = 1;
  140.             temp <<= sizeof(int) * 8 - i - 1;
  141.             number = number | temp;
  142.             break;
  143.         case '0':
  144.             SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color0));
  145.             cout << ch;
  146.             temp = -1;
  147.             temp -= 1 << sizeof(int) * 8 - i - 1;
  148.             number = number & temp;
  149.             break;
  150.         default:
  151.             i--;
  152.         }
  153.     }
  154.     SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | colordef));
  155.     cout << endl;
  156. }
  157.  
  158. void bitChange(long long& number, HANDLE hConsole) {
  159.     cout << "Enter index of the 1st bit: ";
  160.     int index;
  161.     long long temp;
  162.     cin >> index;
  163.     if (index >= sizeof(long long) * 8)
  164.         return;
  165.     char ch = 0;
  166.     cout << "Enter bits: ";
  167.     for (int i = index; i < sizeof(long long) * 8; i++) {
  168.         ch = _getch();
  169.         switch (ch) {
  170.         case '1':
  171.             SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color1));
  172.             cout << ch;
  173.             temp = 1;
  174.             temp <<= sizeof(long long) * 8 - i - 1;
  175.             number = number | temp;
  176.             break;
  177.         case '0':
  178.             SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color0));
  179.             cout << ch;
  180.             temp = -1;
  181.             temp -= 1 << sizeof(long long) * 8 - i - 1;
  182.             number = number & temp;
  183.             break;
  184.         default:
  185.             i--;
  186.         }
  187.     }
  188.     SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | colordef));
  189.     cout << endl;
  190. }
  191.  
  192. void bit_print(char* arr, int size, HANDLE hConsole) {
  193.     for (int i = size - 1; i >= 0; i--) {
  194.         for (int j = 0; j < sizeof(char) * 8; j++) {
  195.             if (arr[i] & 128) {
  196.                 SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color1));
  197.                 cout << "1";
  198.             }
  199.             else {
  200.                 SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color0));
  201.                 cout << 0;
  202.             }
  203.             arr[i] <<= 1;
  204.         }
  205.     }
  206.     SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | colordef));
  207. }
  208.  
  209. void bit_print(char ch, HANDLE hConsole) {
  210.     for (int i = 0; i < sizeof(char) * 8; i++) {
  211.         if (ch & 128) {
  212.             SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color1));
  213.             cout << "1";
  214.         }
  215.         else {
  216.             SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | color0));
  217.             cout << 0;
  218.         }
  219.         ch <<= 1;
  220.     }
  221.     SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | colordef));
  222. }
  223. void int_processing(int numberSystem, HANDLE hConsole) {
  224.     union {
  225.         int number;
  226.         char bytes[sizeof(int)];
  227.     };
  228.     number = 0;
  229.     char sign = 1;
  230.     string intEnter = "";
  231.     system("cls");
  232.     cout << "INT " << numberSystem << " system\n";
  233.     printAlphabet(numberSystem);
  234.     cout << "Enter number: ";
  235.     numberEnter(intEnter, numberSystem, sign);
  236.     for (int i = 0; i < intEnter.length(); i++) {
  237.         if (intEnter[i] >= 'a') {
  238.             number += (intEnter[i] - '0' - 7 - 6) * degree(intEnter.length() - i - 1, numberSystem);
  239.         }
  240.         else {
  241.             if (intEnter[i] >= 'A') {
  242.                 number += (intEnter[i] - '0' - 7) * degree(intEnter.length() - i - 1, numberSystem);
  243.             }
  244.             else {
  245.                 number += (intEnter[i] - '0') * degree(intEnter.length() - i - 1, numberSystem);
  246.             }
  247.         }
  248.     }
  249.     number *= sign;
  250.     cout << "\ndecimal notation: " << number << endl;
  251.     int temp;
  252.     temp = number;
  253.     cout << "binary notation: ";
  254.     bit_print(bytes, sizeof(int), hConsole);
  255.     cout << endl;
  256.     cout << "Dou you want to change some bits?\n[y] - yes\n[n] - no";
  257.     char ch = 0;
  258.     do {
  259.         ch = _getch();
  260.     } while (ch != 'y' && ch != 'n');
  261.     cout << endl;
  262.     if (ch == 'y') {
  263.         bitChange(temp, hConsole);
  264.         number = temp;
  265.         cout << "changed demical notation: " << number << endl;
  266.         cout << "changed binary notation: ";
  267.         bit_print(bytes, sizeof(int), hConsole);
  268.         cout << endl;
  269.     }
  270.     cout << "Press ECS to exit...";
  271.     char exitcode;
  272.     do {
  273.         exitcode = _getch();
  274.     } while (exitcode != 27);
  275.     system("cls");
  276. }
  277.  
  278. void float_proccesing(int numberSystem, HANDLE hConsole) {
  279.     union {
  280.         float number;
  281.         int inumber;
  282.         char bytes[sizeof(float)];
  283.     };
  284.     number = 0;
  285.     char sign2 = 1;
  286.     int dotPos = -1;
  287.     string floatEnter = "";
  288.     system("cls");
  289.     cout << "FLOAT " << numberSystem << " system\n";
  290.     printAlphabet(numberSystem);
  291.     cout << "Enter number: ";
  292.     fractionalNumberEnter(floatEnter, numberSystem, sign2, dotPos);
  293.     if (dotPos != -1) {
  294.         for (int i = 0; i < dotPos; i++) {
  295.             if (floatEnter[i] - '0' < 10)
  296.                 number += (floatEnter[i] - '0') * degree(dotPos - i - 1, numberSystem);
  297.             else {
  298.                 if (floatEnter[i] <= 'Z') {
  299.                     number += (floatEnter[i] - '0' - 7 /*number that deletes the difference between the character A and number 10*/) * degree(dotPos - i - 1, numberSystem);
  300.                 }
  301.                 else {
  302.                     number += (floatEnter[i] - '0' - 7 - 6 /*number that deletes the difference between the character a and 10*/) * degree(dotPos - i - 1, numberSystem);
  303.                 }
  304.             }
  305.         }
  306.         for (int i = dotPos + 1; i < floatEnter.length(); i++) {
  307.             if (floatEnter[i] - '0' < 10)
  308.                 number += (floatEnter[i] - '0') * degree(dotPos - i, numberSystem);
  309.             else {
  310.                 if (floatEnter[i] <= 'Z') {
  311.                     number += (floatEnter[i] - '0' - 7 /*number that deletes the difference between the character A and number 10*/) * degree(dotPos - i, numberSystem);
  312.                 }
  313.                 else {
  314.                     number += (floatEnter[i] - '0' - 7 - 6 /*number that deletes the difference between the character a and number 10*/) * degree(dotPos - i, numberSystem);
  315.                 }
  316.             }
  317.         }
  318.     }
  319.     else {
  320.         for (int i = 0; i < floatEnter.length(); i++) {
  321.             if (floatEnter[i] - '0' < 10) {
  322.                 number += (floatEnter[i] - '0') * degree(floatEnter.length() - i - 1, numberSystem);
  323.             }
  324.             else {
  325.                 number += (floatEnter[i] - '0' - 7 /*number that deletes the difference between the character A and number 10*/) * degree(floatEnter.length() - i - 1, numberSystem);
  326.             }
  327.         }
  328.     }
  329.     number *= sign2;
  330.     cout << "\ndecimal notation: " << fixed << number << endl;
  331.     int temp;
  332.     temp = inumber;
  333.     cout << "binary notation: ";
  334.     bit_print(bytes, sizeof(float), hConsole);
  335.     cout << endl;
  336.     cout << "Dou you want to change some bits?\n[y] - yes\n[n] - no";
  337.     char chr;
  338.     chr = 0;
  339.     do {
  340.         chr = _getch();
  341.     } while (chr != 'y' && chr != 'n');
  342.     cout << endl;
  343.     if (chr == 'y') {
  344.         bitChange(temp, hConsole);
  345.         inumber = temp;
  346.         cout << "changed binary notation: ";
  347.         bit_print(bytes, sizeof(float), hConsole);
  348.         cout << endl;
  349.     }
  350.     cout << "Press ESC to exit...";
  351.     char exitcode;
  352.     do {
  353.         exitcode = _getch();
  354.     } while (exitcode != 27);
  355.     system("cls");
  356. }
  357.  
  358. void double_proccesing(int numberSystem, HANDLE hConsole) {
  359.     union {
  360.         double number;
  361.         long long inumber;
  362.         char bytes[sizeof(double)];
  363.     };
  364.     number = 0;
  365.     char sign3 = 1;
  366.     int dotPos2 = -1;
  367.     string doubleEnter = "";
  368.     system("cls");
  369.     cout << "DOUBLE " << numberSystem << " system\n";
  370.     printAlphabet(numberSystem);
  371.     cout << "Enter number: ";
  372.     fractionalNumberEnter(doubleEnter, numberSystem, sign3, dotPos2);
  373.     if (dotPos2 != -1) {
  374.         for (int i = 0; i < dotPos2; i++) {
  375.             if (doubleEnter[i] - '0' < 10)
  376.                 number += (doubleEnter[i] - '0') * degree(dotPos2 - i - 1, numberSystem);
  377.             else {
  378.                 if (doubleEnter[i] <= 'Z') {
  379.                     number += (doubleEnter[i] - '0' - 7 /*number that deletes the difference between the character A and number 10*/) * degree(dotPos2 - i - 1, numberSystem);
  380.                 }
  381.                 else {
  382.                     number += (doubleEnter[i] - '0' - 7 - 6 /*number that deletes the difference between the character a and 10*/) * degree(dotPos2 - i - 1, numberSystem);
  383.                 }
  384.             }
  385.         }
  386.         for (int i = dotPos2 + 1; i < doubleEnter.length(); i++) {
  387.             if (doubleEnter[i] - '0' < 10)
  388.                 number += (doubleEnter[i] - '0') * degree(dotPos2 - i, numberSystem);
  389.             else {
  390.                 if (doubleEnter[i] <= 'Z') {
  391.                     number += (doubleEnter[i] - '0' - 7 /*number that deletes the difference between the character A and number 10*/) * degree(dotPos2 - i, numberSystem);
  392.                 }
  393.                 else {
  394.                     number += (doubleEnter[i] - '0' - 7 - 6 /*number that deletes the difference between the character a and number 10*/) * degree(dotPos2 - i, numberSystem);
  395.                 }
  396.             }
  397.         }
  398.     }
  399.     else {
  400.         for (int i = 0; i < doubleEnter.length(); i++) {
  401.             if (doubleEnter[i] - '0' < 10) {
  402.                 number += (doubleEnter[i] - '0') * degree(doubleEnter.length() - i - 1, numberSystem);
  403.             }
  404.             else {
  405.                 if (doubleEnter[i] <= 'Z') {
  406.                     number += (doubleEnter[i] - '0' - 7 /*number that deletes the difference between the character A and number 10*/) * degree(doubleEnter.length() - i - 1, numberSystem);
  407.                 }
  408.                 else {
  409.                     number += (doubleEnter[i] - '0' - 7 - 6 /*number that deletes the difference between the character A and number 10*/) * degree(doubleEnter.length() - i - 1, numberSystem);
  410.                 }
  411.             }
  412.         }
  413.     }
  414.     number *= sign3;
  415.     cout << "\ndecimal notation: " << fixed << number << endl;
  416.     long long temperal;
  417.     temperal = inumber;
  418.     cout << "binary notation: ";
  419.     bit_print(bytes, sizeof(double), hConsole);
  420.     cout << endl;
  421.     cout << "Dou you want to change some bits?\n[y] - yes\n[n] - no";
  422.     char c;
  423.     c = 0;
  424.     do {
  425.         c = _getch();
  426.     } while (c != 'y' && c != 'n');
  427.     cout << endl;
  428.     if (c == 'y') {
  429.         bitChange(temperal, hConsole);
  430.         inumber = temperal;
  431.         cout << "changed binary notation: ";
  432.         bit_print(bytes, sizeof(double), hConsole);
  433.         cout << endl;
  434.     }
  435.     cout << "Press ESC to exit...";
  436.     char exitcode;
  437.     do {
  438.         exitcode = _getch();
  439.     } while (exitcode != 27);
  440.     system("cls");
  441. }
  442.  
  443. void char_proccesing(HANDLE hConsole) {
  444.     system("cls");
  445.     char task = 0, character = 0;
  446.     string charString = "";
  447.     do {
  448.         cout << "[1] - character itself\n" << "[2] - code of character";
  449.         task = _getch();
  450.         if (task != '1' && task != '2') {
  451.             system("cls");
  452.             cout << "ERROR: Wrong enter!\n";
  453.         }
  454.     } while (task != '1' && task != '2');
  455.     if (task == '1') {
  456.         cout << "\nEnter a character: ";
  457.         character = _getch();
  458.         cout << character << endl;
  459.         cout << "Code: " << int(character) << endl;
  460.     }
  461.     else {
  462.         cout << "\nEnter code of character: ";
  463.         charEnter(charString);
  464.         for (int i = 0; i < charString.length(); i++) {
  465.             character += (charString[i] - '0') * degree(charString.length() - i - 1, 10);
  466.         }
  467.         cout << "\nCharacter: " << character << endl;
  468.     }
  469.     cout << "binary notation: ";
  470.     bit_print(character, hConsole);
  471.     cout << endl;
  472.     cout << "Press ECS to exit...";
  473.     char exitcode;
  474.     do {
  475.         exitcode = _getch();
  476.     } while (exitcode != 27);
  477.     system("cls");
  478. }
  479.  
  480. void bool_proccesing(HANDLE hConsole) {
  481.     union {
  482.         bool flag;
  483.         char help;
  484.     };
  485.     system("cls");
  486.     cout << "Enter smt: ";
  487.     help = _getch();
  488.     cout << help;
  489.     if (help == '0')
  490.         flag = 0;
  491.     else
  492.         flag = 1;
  493.     cout << "\nYou entered: " << boolalpha << flag << endl;
  494.     cout << "binary notation: ";
  495.     bit_print(help, hConsole);
  496.     cout << endl;
  497.     cout << "Press ECS to exit...";
  498.     char exitcode;
  499.     do {
  500.         exitcode = _getch();
  501.     } while (exitcode != 27);
  502.     system("cls");
  503. }
  504.  
  505. void numberSystem_proccesing(int &numberSystem) {
  506.     char ch = 0;
  507.     string numberSystemEnter = "";
  508.     do {
  509.         cout << "Enter number system (2-62): ";
  510.         while (numberSystemEnter.length() < 2 && ch != 13/*return*/) {
  511.             ch = _getch();
  512.             if (ch >= '0' && ch <= '9') {
  513.                 if (!(ch == '0' && numberSystemEnter.length() == 0)) {
  514.                     cout << ch;
  515.                     numberSystemEnter += ch;
  516.                 }
  517.             }
  518.             if (numberSystemEnter.length() == 1 && numberSystemEnter[0] > '6') {
  519.                 break;
  520.             }
  521.         }
  522.         for (int i = 0; i < numberSystemEnter.length(); i++) {
  523.             numberSystem += (numberSystemEnter[i] - '0') * degree(numberSystemEnter.length() - i - 1, 10);
  524.         }
  525.         if (numberSystem > 62 || numberSystem < 2) {
  526.             system("cls");
  527.             cout << "ERROR: Wrong enter!" << endl;
  528.             ch = 0;
  529.             numberSystem = 0;
  530.             numberSystemEnter = "";
  531.         }
  532.     } while (numberSystem < 2 || numberSystem > 62);
  533. }
  534.  
  535. int main()
  536. {
  537.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  538.     char dataType;
  539.     do {
  540.         int numberSystem = 0;
  541.         cout << "Enter data type\n" << "[1] - int\n" << "[2] - float\n"
  542.             << "[3] - double\n" << "[4] - char\n" << "[5] - bool\n" << "[ESC] - exit\n";
  543.         dataType = _getch();
  544.         if (dataType == '1' || dataType == '2' || dataType == '3') {
  545.             system("cls");
  546.             numberSystem_proccesing(numberSystem);
  547.         }
  548.         switch (dataType) {
  549.         case '1' /*int*/:
  550.             int_processing(numberSystem, hConsole);
  551.             break;
  552.         case '2' /*float*/:
  553.             float_proccesing(numberSystem, hConsole);
  554.             break;
  555.         case '3' /*double*/:
  556.             double_proccesing(numberSystem, hConsole);
  557.             break;
  558.         case '4' /*char*/:
  559.             char_proccesing(hConsole);
  560.             break;
  561.         case '5'/*bool*/:
  562.             bool_proccesing(hConsole);
  563.             break;
  564.         default:
  565.             system("cls");
  566.             cout << "ERROR: Wrong enter!\n";
  567.         }
  568.     } while (dataType != 27 /*esc*/);
  569.     return 0;
  570. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top