⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mton.java

📁 利用JAVA实现M与N进制的转换
💻 JAVA
字号:
import java.io.*; 
public class mton 
{ 
public static void main(String args[]) throws IOException 
{ 
BufferedReader buf1; 
String str1,str2=""; 
buf1=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("输入的数的进制:");
int zhi=Integer.parseInt(buf1.readLine());
System.out.println("请输入转换成的进制:"); 
int end=Integer.parseInt(buf1.readLine());
System.out.println("请输入一个数:"); 
str1=buf1.readLine();

char ch; 
int flag=1; 
for(int i=0;i<str1.length();i++) 
{ 
ch=str1.charAt(i); 
if(!(ch>='0'&&ch<='9'||ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch=='.')) 
{ 
System.out.println("输入不合法!"); 
flag=0; 
break;

} 
}

double ten=0;
int x;

for(x=0,ch=str1.charAt(0);x<str1.length();x++)
{
	ch=str1.charAt(x);
	if(ch=='.')
	break;
}

for(int i=0;i<x;i++)
{
    ch=str1.charAt(i);
    if(ch>='0'&&ch<='9')
	ten+=(int)(ch-48)*(Math.pow(zhi,x-1-i));
	else if(ch>='A'&&ch<='Z')
	ten+=(int)(ch-55)*(Math.pow(zhi,x-1-i));
	else 
	ten+=(int)(ch-87)*(Math.pow(zhi,x-1-i));
}


for(int i=x+1;i<str1.length();i++)
{
		ch=str1.charAt(i);
		if(ch!='\0')
		{
			if(ch>='0'&&ch<='9')
		    ten+=(int)(ch-48)*(Math.pow(zhi,x-i));
		    else if(ch>='A'&&ch<='Z')
	        ten+=(int)(ch-55)*(Math.pow(zhi,x-i));
	        else 
	        ten+=(int)(ch-87)*(Math.pow(zhi,x-i));
	   }
	
}
    System.out.println("10进制形式为:"+ten);
int a=(int)ten;
double b=ten-a;
if(b==0)
{
	exchange(a,end);
} 
    
}
public static void exchange(int n,int end) 
{ 
if(n>0&&end<11) 
{ 

exchange(n/end,end); 
 System.out.print(n%end);
} 
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -