Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. // full.cpp
  2.  
  3. #include <iostream>
  4.  
  5.  
  6. int main() {
  7. int base;
  8. std::cout << "Base: ";
  9. if (!(std::cin >> base) || base > 9 || base < 2) {
  10. std::cerr << "Integer base in range [2, 9] required\n";
  11. return 1;
  12. }
  13. char input;
  14. long double result = 0;
  15. long double current_power = 1.0l / (long double)base;
  16. std::cout << "Number: ";
  17. if (!std::cin >> input) {
  18. std::cerr << "Incorrect input\n";
  19. return 2;
  20. }
  21. if (!(input >= int('0') && input <= int('9')) && !(input == '-' || input == '+')) {
  22. std::cerr << "Unknown character: " << input << '\n';
  23. return 3;
  24. }
  25. if (input > base - 1) {
  26. std::cerr << input << " is invalid number in base number system with base " << base << '\n';
  27. return 4;
  28. }
  29. bool sign = true;
  30. switch (input) {
  31. case '-':
  32. sign = false;
  33. break;
  34. case '+'
  35. break;
  36. default:
  37. result += input - int('0');
  38. }
  39. bool fraction = false;
  40. while (std::cin >> input) {
  41. if (!(input >= int('0') && input <= int('9')) && !(input == '.')) {
  42. std::cerr << "Unknown character: " << input << '\n';
  43. return 3;
  44. }
  45. if (input == '.' && fraction) {
  46. std::cerr << "Unexpected symbol '.'\n;
  47. return 5;
  48. }
  49. if (input > base - 1) {
  50. std::cerr << input << " is invalid number in base number system with base " << base << '\n';
  51. return 4;
  52. }
  53. if (input == '.') {
  54. fraction = true;
  55. continue;
  56. }
  57. if (fraction) {
  58. result += (input - int('0')) * current_power;
  59. current_power /= base;
  60. } else {
  61. result *= base;
  62. result += input - int('0');
  63. }
  64. }
  65. if (!sign) {
  66. std::cout << '-';
  67. }
  68. std::cout << result << '\n';
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement