Advertisement
evage

Untitled

Nov 30th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6.  
  7. int convertTo10(string n, int notation) {
  8.     int res=0;
  9.     string symbols = "abcdef";
  10.     for (int i = 0; i < (int)n.size(); ++i)
  11.     {
  12.         if (symbols.find(tolower(n[i]))!=string::npos)
  13.             res += (tolower(n[i]) - 'a' + 10) * pow(notation, (int)n.size() - i - 1);
  14.         else
  15.             res += (n[i] - '0') * pow(notation, (int)n.size() - i - 1);
  16.     }
  17.     return res;
  18. }
  19. string convertToN(int res, int notation) {
  20.     string symbols = "ABCDEF";
  21.     string ans;
  22.     while (res)
  23.     {
  24.         int add = res % notation;
  25.         if (add >= 10) ans += symbols[add - 10];
  26.         else ans += to_string(add);
  27.         res /= notation;
  28.     }
  29.    
  30.     reverse(ans.begin(), ans.end());
  31.     return ans;
  32. }
  33. int main() {  
  34.     string num;
  35.     int notation;
  36.     cout << "enter number ans its notation\n";
  37.     cin >> num >> notation;
  38.     cout << "enter notation you need\n";
  39.     int needNotation; cin >> needNotation;
  40.  
  41.     int tenNotation = convertTo10(num, notation);
  42.     string result = convertToN(tenNotation, needNotation);
  43.     cout << result;
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement