写的很粗糙,输入未进行验证,但是功能实现了,大家多多来检验下:
- #include<iostream>
- using namespace std;
- //m进制数转换为逆向字符串
- void ItoC(char*A ,int z,int &counter)
- {
- int temp1,temp2;
- counter=1;
- temp1 = z%10;
- temp2 = z/10;
- A[0]=temp1+48;
- while(temp2 != 0)
- {
- z=temp2;
- temp1 = z%10;
- temp2 = z/10;
- A[counter++]=temp1+48;
- }
- }
- //十进制数转换为n进制数
- void DtoN(char *A,int z,int n,int &counter)
- {
- counter = 1;
- A[0] = z%n+48;
- z/=n;
- while(z != 0)
- {
- A[counter++]=z%n+48;
- z/=n;
- }
- }
- int main()
- {
- int i,m,n,z=0,x=1,counter1,counter2;
- char A[10];
- cout<<"This program will transform a m system number to a n system number!"<<endl;
- cout<<"Please cin the M:";
- cin>>m;
- cout<<"Please cin the N:";
- cin>>n;
- cout<<"Then please cin the M system number:";
- cin>>z;
- ItoC(A,z,counter1);
- //将M进制数转化为十进制数
- //for(i=0;i<counter1;i++) cout<<A[i];
- //cout<<endl;
- z=0;
- for(i=0;i<counter1;i++)
- {
- z+=x*(A[i]-48);
- x*=m;
- }
- DtoN(A,z,n,counter2);
- for(i=counter2-1;i>=0;i--)
- {
- cout<<A[i];
- }
- cout<<endl;
- return 0;
- }