Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- char radix(unsigned long long int i)
- {
- if (i >= 10)
- {
- return i + 55;
- }
- else if (i >= 0 && i <= 9)
- {
- return i + 48;
- }
- }
- bool chk(unsigned long long int b, unsigned long long int a)
- {
- while (a != 0)
- {
- if (a % 10 >= b)
- {
- return 1;
- }
- a /= 10;
- }
- return 0;
- }
- unsigned long long int true_value(unsigned long long int b, unsigned long long int a)
- {
- unsigned long long int c;
- unsigned long long int i = 0;
- unsigned long long int sum = 0;
- while (a > 0)
- {
- sum += (a%10)*pow(b, i);
- a /= 10;
- i++;
- }
- return sum;
- }
- void convert(unsigned long long int a, unsigned long long int b)
- {
- char c[100] = {};
- unsigned long long int i = 0,d;
- while (a > 0)
- {
- d = a%b;
- c[i] = radix(d);
- a /= b;
- i++;
- }
- for (int i = 99; i >= 0; i--)
- {
- if(c[i] != 0)
- cout << c[i];
- }
- }
- int main()
- {
- unsigned long long int or_base, or_radix;
- unsigned long long int af_base;
- cout << "輸入待轉換進制" << endl;;
- cin >> or_base;
- cout << "輸入待轉換值" << endl;
- cin >> or_radix;
- if (chk(or_base, or_radix)==1)
- {
- cout << "輸入錯誤" << endl;
- system("PAUSE");
- return 0;
- }
- cout <<"輸入同等於的10進位值"<< true_value(or_base, or_radix) << endl;
- cout << "輸入轉換後的進制值" << endl;
- cin >> af_base;
- cout << "轉換後的數字為"<<endl;
- convert(true_value(or_base, or_radix), af_base);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement