#include #include #include #include using namespace std; void Exit() { cout << "\n\nКонец\n\n"; exit(0); } int powerup(int number, int degree) { int result = 1; for(int i = 1; i <= degree; i++) result *= number; return result; } float powerdown(int number, int degree) { float result = 1; for(int i = 1; i <= degree; i++) result *= number; return 1/result; } void DecToOct() { bool done; do { string num,integer_temp,fraction_temp; int value,integer; double fraction,result; system("cls"); cout << "Перевод числа из десятичной системы счисления в восьмеричную\n\n"; cout << "Введите число, дробная часть указывается через \" . \"\n\n"; cin >> num; cout << "\nИсходное число : " << num; value = num.find('.'); integer_temp = num.substr(0, value); if(num.find('.') == string::npos) fraction_temp = "0"; else fraction_temp = num.substr(value+1, num.size()); fraction_temp.insert(0,"0"); integer = stoi(integer_temp); fraction = stod(fraction_temp)/powerup(10,fraction_temp.size()-1); int int_res = 0, temp = 1; // temp - разряды while (integer) { int_res += integer % 8 * temp; integer /= 8; temp *= 10; } double frac_res = 0, temp1; for (int i = 0; i < 8; i++) { fraction *= 8; temp1 = (int)fraction; frac_res += temp1 * powerdown(10,i+1); fraction -= temp1; } result = int_res + frac_res; cout << "\nРезультат перевода : " <> c; done = (c != 'y'); } while (!done); system("cls"); cout << "\n0 - для выхода\n\n1 - Перевод из десятичной системы счисления в восьмеричную\n\n2 - Перевод из восьмеричной системы счисления в десятичную\n\n"; } void OctToDec() { bool done; do { string num,integer_temp,fraction_temp; int value,integer; double fraction,result; system("cls"); cout << "Перевод числа из восьмеричной системы счисления в десятичную\n\n"; cout << "Введите число, дробная часть указывается через \" . \"\n\n"; cin >> num; cout << "\nИсходное число : " << num; value = num.find('.'); integer_temp = num.substr(0, value); if(num.find('.') == string::npos) fraction_temp = "0"; else fraction_temp = num.substr(value+1, num.size()); fraction_temp.insert(0,"0"); integer = stoi(integer_temp); fraction = stod(fraction_temp)/powerup(10,fraction_temp.size()-1); int int_res = 0,temp = 8; // temp - разряды for (int i = 0; integer > 0; i++) { int_res += (integer%10)*powerup(8.0,i); integer /= 10; } double frac_res = 0, temp1; for (int i = 0; i < 10; i++) { fraction *= 10; temp1 = (int)fraction; frac_res += temp1 * powerdown(8,i+1); fraction -= temp1; } result = int_res + frac_res; cout << "\nРезультат перевода : " <> c; done = (c != 'y'); } while (!done); system("cls"); cout << "\n0 - для выхода\n\n1 - Перевод из десятичной системы счисления в восьмеричную\n\n2 - Перевод из восьмеричной системы счисления в десятичную\n\n"; } int main() { void(*pact[])()={Exit,DecToOct,OctToDec}; setlocale(LC_ALL,""); int num; //void(*pact[])(int n)={f0,f1,f2}; cout << "0 - для выхода\n\n1 - Перевод из десятичной системы счисления в восьмеричную\n\n2 - Перевод из восьмеричной системы счисления в десятичную\n\n"; while (1) { cin >> num; pact[num](); } return 0; }