Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- int convertTo10(string n, int notation) {
- int res=0;
- string symbols = "abcdef";
- for (int i = 0; i < (int)n.size(); ++i)
- {
- if (symbols.find(tolower(n[i]))!=string::npos)
- res += (tolower(n[i]) - 'a' + 10) * pow(notation, (int)n.size() - i - 1);
- else
- res += (n[i] - '0') * pow(notation, (int)n.size() - i - 1);
- }
- return res;
- }
- string convertToN(int res, int notation) {
- string symbols = "ABCDEF";
- string ans;
- while (res)
- {
- int add = res % notation;
- if (add >= 10) ans += symbols[add - 10];
- else ans += to_string(add);
- res /= notation;
- }
- reverse(ans.begin(), ans.end());
- return ans;
- }
- int main() {
- string num;
- int notation;
- cout << "enter number ans its notation\n";
- cin >> num >> notation;
- cout << "enter notation you need\n";
- int needNotation; cin >> needNotation;
- int tenNotation = convertTo10(num, notation);
- string result = convertToN(tenNotation, needNotation);
- cout << result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement