标签:div highlight pop efault ring oid 比较 class lse
public class PracticeDemo {
//十进制转二进制
public static void Binary(int x) {
int c = x;//为第二种方法先把x值预存起来
//方法一:
Stack stack = new Stack();
while(x > 0)
{
stack.push(x % 2);//余数进栈
x = x / 2; //每一次除以2
}
System.out.println("利用栈数据结构输出二进制数:");
while(!stack.isEmpty())
System.out.print(stack.pop());//利用栈的特性出栈(FILO)
//方法二:
int temp = 0;
int p = 0;
int[] a = new int[100]; //存放二进制权值数组
for(int i = 0;i < 100;i++)
{
a[i] = (int) Math.pow(2,i);//数组赋值。Math.pow(a,b)a的b次方
}
System.out.println();
System.out.println("利用比较法输出二进制数:");
//通过每一个位置上面的权值(从大到小)来寻找二进制输出时的位数p(只要找到数组下标 )
for(int j = a.length - 2;j >= 0;j--)
{
if((c > a[j]||c == a[j]) && (c < a[j+1]))
{
p = j ;
break;
}
}
for(int j = p;j >= 0;j--)
{
if(c >= a[j] && c < a[j+1])
{
temp = 1;
c = c - a[j];//当输出1时表示此位已用,减去此位的权值再比较。
}
else
temp = 0;
System.out.print(temp+"");
}
}
//十进制转八进制
public static void Octonary(int x) {
int c = x;
//方法一:
Stack stack = new Stack();
while(x > 0)
{
stack.push(x % 8);
x = x / 8;
}
System.out.println("利用栈数据结构输出八进制数:");
while(!stack.isEmpty())
System.out.print(stack.pop());
//方法二:
int p = 0;
int[] a = new int[100];
for(int i = 0;i < 100;i++)
{
a[i] = (int) Math.pow(8,i);
}
System.out.println();
System.out.println("利用比较法输出八进制数:");
for(int j = a.length - 2;j >= 0;j--)
{
if((c > a[j]||c == a[j]) && (c < a[j+1]))
{
p = j ;
break;
}
}
for(int j = p;j >= 0;j--)
{
for(int k = 7;k >= 0;k--)//八进制每一位上面的数是1~7所以需要权值乘以此位上面的数字
{
if((c > a[j]*k || c == a[j]*k)&&( c < a[j]*(k+1)))
{
System.out.print(k+"");
c = c - a[j]*k;
break;
}
}
}
}
//十进制转十六进制
public static void Hex(int x) {
int c = x;
//方法一:
int temp = 0;
Stack stack = new Stack();
while(x > 0)
{
temp = x % 16;
switch(temp)
{
case 10: stack.push(‘A‘);break;
case 11: stack.push(‘B‘);break;
case 12: stack.push(‘C‘);break;
case 13: stack.push(‘D‘);break;
case 14: stack.push(‘E‘);break;
case 15: stack.push(‘F‘);break;
default : stack.push(x % 16);
}
x = x / 16;
}
System.out.println("利用栈数据结构输出十六进制数:");
while(!stack.isEmpty())
System.out.print(stack.pop());
//方法二:
int p = 0;
int d = 0;
int[] a = new int[100];
for(int i = 0;i < 100;i++)
{
a[i] = (int) Math.pow(16,i);
}
System.out.println();
System.out.println("利用比较法输出十六进制数:");
for(int j = a.length - 2;j >= 0;j--)
{
if((c > a[j]||c == a[j]) && (c < a[j+1]))
{
p = j ;
break;
}
}
for(int j = p;j >= 0;j--)
{
for(int k = 15;k >= 0;k--)
{
if((c > a[j]*k || c == a[j]*k)&&( c < a[j]*(k+1)))
{
if(k > 9)
{
switch(k)
{
case 10: System.out.print(‘A‘);break;
case 11: System.out.print(‘B‘);break;
case 12: System.out.print(‘C‘);break;
case 13: System.out.print(‘D‘);break;
case 14: System.out.print(‘E‘);break;
case 15: System.out.print(‘F‘); break;
}
}
else
System.out.print(k+"");
c = c - a[j]*k;
break;
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("请输入数字:");
int x = in.nextInt();
Binary(x);
System.out.println();
Octonary(x);
System.out.println();
Hex(x);
}
}
标签:div highlight pop efault ring oid 比较 class lse
原文地址:https://www.cnblogs.com/YangK123/p/12296215.html