Advertisement
mercMatvey4

Untitled

Nov 28th, 2018
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <windows.h>
  4. using namespace std;
  5.  
  6. int powerup(int number, int degree)
  7. {
  8.     int result = 1;
  9.     for(int i = 1; i <= degree; i++)
  10.         result *= number;
  11.     return result;
  12. }
  13.  
  14. float powerdown(int number, int degree)
  15. {
  16.     float result = 1;
  17.     for(int i = 1; i <= degree; i++)
  18.         result *= number;
  19.     return 1/result;
  20. }
  21.  
  22. int main()
  23. {
  24.     setlocale(LC_ALL,"");
  25.     string num,integer_temp,fraction_temp,null = "0.";
  26.     int value,choice,integer;
  27.     double fraction;
  28.     cout << "Выберите перевод: \n1 - из десятичной системы счисления в восьмеричную \n2 - из восьмеричной системы счисления в десятичную\n\n";
  29.     cin >> choice;
  30.     system("cls");
  31.     cout << "Введите число, дробная часть указывается через \" . \"\n\n";
  32.     cin >> num;
  33.     value = num.find('.');
  34.     integer_temp = num.substr(0, value);
  35.     fraction_temp = num.substr(value+1, num.size());
  36.     fraction_temp.insert(0,"0");
  37.     integer = stoi(integer_temp);
  38.     fraction = stod(fraction_temp)/powerup(10,fraction_temp.size()-1);
  39.     if (choice == 1)
  40.     {
  41.         int int_res = 0,temp = 1; // temp - разряды
  42.         while (integer)
  43.         {
  44.             int_res += integer % 8 * temp;
  45.             integer /= 8;
  46.             temp *= 10;
  47.         }
  48.         double frac_res = 0, temp1;
  49.         for (int i = 0; i < 8; i++)
  50.         {
  51.             fraction *= 8;
  52.             temp1 = (int)fraction;
  53.             frac_res += temp1;
  54.             fraction -= int(temp1);
  55.         }
  56.         cout << frac_res;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement