您的位置:首页 > 其它

挑战CPU运算能力2-输出十进制、二进制、八进制及十六进制对照表(Table of Binary, Octal and Hexadecimal Numbers)

2016-07-06 16:28 756 查看
花了1个多小时写的,有一定的实用性和健壮性,挺有成就感


理由:

1. 生成对照表,省去十进制转码时间,输出结果导入数据库后,可供快速查询

2.经过在Windows 8自带计算器的programmer mode中的对比测试,转换结果是正确无误的

3.十进制转其他进制,使用了一套逻辑,一个方法,简单、可靠

4.增加了时间戳,程序运行结束时,会显示用时。调试程序时输入较大的数字时,输出对照表需要较长时间,可用此程序对比测试不同计算机的运算速度。

PS:刚好买了新的游戏本,回家测测看。


代码如下:

package example;
//JHTP Exercise 6.34: Table of Binary, Octal and Hexadecimal Numbers
//by pandenghuang@163.com
/**(Table of Binary, Octal and Hexadecimal Numbers) Write an application that displays a
table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1
through 256. If you aren’t familiar with these number systems, read online Appendix J first.*/
import java.util.Scanner;
import java.util.Date;

public class NumberConversion
{
public static String convert(int number,int system){
//10->10/2=5,10%2=0->5/2=2,5%2=1->2/2=1,2%2=0->1010
//2->2/2=1,2%2=0
String result="";
boolean flag=true;
int left=0;
do{
switch (number%system){
case 0: case 1: case 2: case 3: case 4:
case 5: case 6: case 7: case 8: case 9:
result+=number%system;
break;
case 10:
result+='A';
break;
case 11:
result+='B';
break;
case 12:
result+='C';
break;
case 13:
result+='D';
break;
case 14:
result+='E';
break;
case 15:
result+='F';
break;
}

left=number/system;
if (left>1)
number=left;
else if(left==1){
result+=1;
flag=false;
break;
}
else if(left==0){
break;
}

}
while(flag);
return result;

}

public static void toHex(int number){

}

public static void main(String[] args)
{
int size=0;
String converted="";
long beginTime=0;
long endTime=0;
double duration=0.0; //分钟

Scanner input=new Scanner(System.in);

do {
System.out.print("请输入数据上限(十进制整数,输入-1退出):");
size=input.nextInt();
if(size==-1)
System.out.print("已退出程序");
else
{
beginTime=new Date().getTime();
System.out.print("十进制\t二进制\t八进制\t十六进制\t\n");
for(int i=1;i<=size;i++){
//输出十进制数字
System.out.print(i+"\t");

//输出二进制数字
converted=convert(i,2);
for (int j=converted.length()-1;j>=0;j--)
System.out.print(converted.charAt(j));
System.out.print("\t");

//输出八进制数字
converted=convert(i,8);
for (int j=converted.length()-1;j>=0;j--)
System.out.print(converted.charAt(j));
System.out.print("\t");

//输出十六进制数字
converted=convert(i,16);
for (int j=converted.length()-1;j>=0;j--)
System.out.print(converted.charAt(j));
System.out.print("\t");
System.out.println();

}

System.out.println();
endTime=new Date().getTime();
duration=(double)(endTime-beginTime)/1000/60;
System.out.printf("共用时%.2f分钟\n",duration);

}
}
while (size!=-1);
}
}


运行结果:(此处格式有点乱,Eclispse控制台中输出结果显示的很整齐)

请输入数据上限(十进制整数,输入-1退出):17

十进制 二进制 八进制 十六进制 

1 1 1 1 

2 10 2 2 

3 11 3 3 

4 100 4 4 

5 101 5 5 

6 110 6 6 

7 111 7 7 

8 1000 10 8 

9 1001 11 9 

10 1010 12 A 

11 1011 13 B 

12 1100 14 C 

13 1101 15 D 

14 1110 16 E 

15 1111 17 F 

16 10000 20 10 

17 10001 21 11 

99996 11000011010011100 303234 1869C 

99997 11000011010011101 303235 1869D 

99998 11000011010011110 303236 1869E 

99999 11000011010011111 303237 1869F 

100000 11000011010100000 303240 186A0 

共用时0.14分钟


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: