Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // full.cpp
- #include <iostream>
- int main() {
- int base;
- std::cout << "Base: ";
- if (!(std::cin >> base) || base > 9 || base < 2) {
- std::cerr << "Integer base in range [2, 9] required\n";
- return 1;
- }
- char input;
- long double result = 0;
- long double current_power = 1.0l / (long double)base;
- std::cout << "Number: ";
- if (!std::cin >> input) {
- std::cerr << "Incorrect input\n";
- return 2;
- }
- if (!(input >= int('0') && input <= int('9')) && !(input == '-' || input == '+')) {
- std::cerr << "Unknown character: " << input << '\n';
- return 3;
- }
- if (input > base - 1) {
- std::cerr << input << " is invalid number in base number system with base " << base << '\n';
- return 4;
- }
- bool sign = true;
- switch (input) {
- case '-':
- sign = false;
- break;
- case '+'
- break;
- default:
- result += input - int('0');
- }
- bool fraction = false;
- while (std::cin >> input) {
- if (!(input >= int('0') && input <= int('9')) && !(input == '.')) {
- std::cerr << "Unknown character: " << input << '\n';
- return 3;
- }
- if (input == '.' && fraction) {
- std::cerr << "Unexpected symbol '.'\n;
- return 5;
- }
- if (input > base - 1) {
- std::cerr << input << " is invalid number in base number system with base " << base << '\n';
- return 4;
- }
- if (input == '.') {
- fraction = true;
- continue;
- }
- if (fraction) {
- result += (input - int('0')) * current_power;
- current_power /= base;
- } else {
- result *= base;
- result += input - int('0');
- }
- }
- if (!sign) {
- std::cout << '-';
- }
- std::cout << result << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement