#include<stdio.h> #include<string.h> #include<stdlib.h> int TenNum(char a[],int B); //將輸入的數字轉換成10進制數 void Numchange(int m, int B); //將轉換好了的10進制數轉換爲所需進制數 int TenNum(char a[], int B) { int len, i, num; int sum = 0; len = strlen(a); //求得字符串長度 for (i = 0; i < len; i++) { if (a[i] >= '0' && a[i] <= '9') num = a[i] - '0'; else if (a[i] >= 'A' && a[i] <= 'F') num = a[i] - 'A' + 10; sum = sum * B + num; } return sum; } void Numchange(int m, int B) { int n; if (m) { Numchange(m / B, B); n = m % B; if (n < 10) printf("%d", n); //小於10直接輸出 else printf("%c", n + 55); //大於10轉換成字符輸出 } } int main() { int B, b; char a[20]; printf("請輸入待轉換數的進制(2-16):"); do { scanf("%d", &B); } while (B < 2 && B > 16); printf("請輸入待轉換數:"); getchar(); gets(a); //將輸入的n進制數存放在數組a中 int m = TenNum(a, B); //將輸入的數字轉換成十進制數 printf("請輸入須要轉成幾進制數(2-16):"); do { scanf("%d", &b); } while (B < 2 && B > 16); printf("%d進制數%s轉換爲%d進制數的結果爲:",B,a,b); Numchange(m, b); //將十進制數轉換爲所需進制數 printf("\n"); system("pause"); return 0; }
二,八,十,十二,十六進制:ios
二進制:0,1 (用於計算機存儲信息)
八進制:0,1,2,3,4,5,6,7
十進制:0,1,2,3,4,5,6,7,8,9 (咱們最熟悉的)數組
十二進制:0,1,2,3,4,5,6,7,8,9,A,B
十六進制:0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,Fide
這些進制數字很容易轉換爲十進制spa
舉個例子:code
1101:2^3+2^2+2^0=13(2—>10)blog
173:8^2+8^1*7+8^0*3=123(8—>10)ci
3A0:12^2*3+12^1*10+12^0*0=552字符串
...get
總結:任何進制數字轉換成爲十進制數字的規律:string
每一位進制數字乘自己進制單位乘自身值再相加即爲十進制數字
int Tenchange(char a[],int B)//char a[100];任意進制數據串 int B;//它所表明的進制數 Tenchange(a,B); { int len=strlen(a); int sum=0,num; for(int i=0;i<len;i++) { if (a[i] >= '0' && a[i] <= '9') num = a[i] - '0'; else if (a[i] >= 'A' && a[i] <= 'F') num = a[i] - 'A' + 10; sum=sum*B+num; } return sum;//返回十進制數字 }
十進制轉任意進制的通用方法:除x取餘倒排法(x表明進制數)
例如:將十進制數76轉換爲任意進制 二進制: 76/2 0 38/2 0 19/2 1 9 1 4 0 2 0 1 1 因此:1001100(2) 八進制: 76/8 4 9 1 1 1 因此:114(8) 十六進制: 76/16 12(C) 4/16 4 因此:4C(16)
char * mychange(int n,int B) //十進制轉換爲其餘進制 (注意:獲得的結果是反的) { int t=0; static char b[100]; while(n!=0) { if(n%B>9) b[t]='A'+n%B-10; else b[t]='0'+n%B; n=n/B; t++; } return b; } int main() { char *c=mychange(76,2); for (int i =strlen(c)-1 ; i >=0; i--){
cout << c[i] << " "; } }
char * mychange(int n,int B) //十進制轉換爲其餘進制 (注意:獲得的結果是反的) {int t=0;static char b[100]; while(n!=0){if(n%B>9)b[t]='A'+n%B-10;elseb[t]='0'+n%B;n=n/B;t++;}return b;}
int main(){ char a[100]; // gets(a);//獲得須要轉換的字符串 char *c=mychange(76,2); for (int i = 0 ; i < strlen(c); i++){ cout << c[i] << " "; } cout<<endl; char *d=mychange(76,8); for (int i = 0 ; i < strlen(d); i++){ cout << d[i] << " "; } cout<<endl; char *e=mychange(76,16); for (int i = 0 ; i < strlen(e); i++){ cout << e[i] << " "; } cout<<endl; }
#include<iostream> #include<cstdio> #include<string.h> using namespace std; char answer[1000]; int n; int len=0; int Numchange(int m, int B) { if (m) { Numchange(m / B, B); n = m % B; if (n < 10) answer[len]='0'+n; //小於10直接輸出 else answer[len]='A'+n-10; //大於10轉換成字符輸出 len++; } return len; } int mychange(int len) { int sum=0; for(int i=0;i<len;i++) { if(answer[i]>='0'&&answer[i]<='9') sum+=(answer[i]-'0'); else sum+=(answer[i]-'A'+10); } return sum; } int main() { while(cin>>n&&n!=0) { //10,16,12 len=0; int n1=n,n2=n,n3=n; int a=0,b=0,c=0; Numchange(n, 10); // cout<<len<<"///"; // for(int i=0;i<len;i++) // cout<<answer[i]; a=mychange(len); // cout<<endl<<a<<endl; len=0; Numchange(n1, 16); // cout<<len<<"///"; // for(int i=0;i<len;i++) // cout<<answer[i]; b=mychange(len); // cout<<endl<<b<<endl; len=0; Numchange(n2, 12); // cout<<len<<"///"; // for(int i=0;i<len;i++) // cout<<answer[i]; c=mychange(len); // cout<<endl<<c<<endl; if(a==b&&b==c) cout<<n3<<" is a Sky Number."<<endl; else cout<<n3<<" is not a Sky Number."<<endl; } return 0; }
#include<iostream> #include<string.h> using namespace std; int Tenchange(char a[],int B)//char a[100];任意進制數據串 int B;//它所表明的進制數 Tenchange(a,B); { int len=strlen(a); int sum=0,num; for(int i=0;i<len;i++) { if (a[i] >= '0' && a[i] <= '9') num = a[i] - '0'; else if (a[i] >= 'A' && a[i] <= 'F') num = a[i] - 'A' + 10; sum=sum*B+num; } return sum; } char * mychange(int n,int B) //十進制轉換爲其餘進制 (注意:獲得的結果是反的) { int t=0; static char b[100]; while(n!=0) { if(n%B>9) b[t]='A'+n%B-10; else b[t]='0'+n%B; n=n/B; t++; } return b; } int main() { char *c=mychange(76,2); for (int i = 0 ; i < strlen(c); i++){ cout << c[i] << " "; } // delete[] c; cout<<endl; char *d=mychange(76,8); for (int i = 0 ; i < strlen(d); i++){ cout << d[i] << " "; } // delete[] d; cout<<endl; char *e=mychange(76,16); for (int i = 0 ; i < strlen(e); i++){ cout << e[i] << " "; } cout<<endl; }
#include<iostream> #include<string.h> using namespace std; char b[100]; void mychange(int n,int B) //十進制轉換爲其餘進制 (注意:獲得的結果是反的) { int t=0; while(n!=0) { if(n%B>9) b[t]='A'+n%B-10; else b[t]='0'+n%B; n=n/B; t++; } } int main() { mychange(76,2); for (int i = 0 ; i < strlen(b); i++){ cout << c[i] << " "; } cout<<endl; char *d=mychange(76,8); for (int i = 0 ; i < strlen(d); i++){ cout << d[i] << " "; } cout<<endl; char *e=mychange(76,16); for (int i = 0 ; i < strlen(e); i++){ cout << e[i] << " "; } cout<<endl; }