转换二进制数字字符串的十进制数...使用递归 [英] Converting String of binary digits to decimal number... using recursion

查看:978
本文介绍了转换二进制数字字符串的十进制数...使用递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的计算机科学教授给我们在做作业这个问题......我不知道如何着手和code我写的好像是惨遭失败。这里是提示:


(二进制到十进制)编写解析二进制数作为一个字符串转换为十进制整数递归方法。该方法标头是:

公共静态字符串BIN2DEC(字符串binaryString)

编写提示用户输入一个二进制字符串,并显示相应的十进制数的测试程序。


任何帮助非常AP preciated。这里是我的code如下:

 进口java.util.Scanner中;公共类HW04_P5 {
    静态INT索引= 0;
    静态INT功率= 0;
    静态INT数= 0;
    静态布尔退出= FALSE;    @燮pressWarnings(资源)
    公共静态无效的主要(字串[] args){
        扫描程序扫描=新的扫描仪(System.in);
        System.out.print(输入一个二进制数转换为十进制:);
        字符串= scan.nextLine();
        指数= in.length() - 1;
        System.out.print(二进制数转换为十进制:+ BIN2DEC(中));
    }    公共静态字符串BIN2DEC(字符串中)
    {
        如果((in.substring(索引,索引+1).equals(1))及及(索引> 0))
        {
            数+ = Math.pow(2,功率);
            System.out.print(数);
            功率++;
            指数 -​​ ;
            BIN2DEC(在);
        }
        否则如果((in.substring(索引,索引+1).equals(0))及及(索引> 0))
        {
            功率++;
            指数 -​​ ;
            BIN2DEC(在);
        }
        System.out.print(数);
        返回;
    }
}


解决方案

这是清洁不要有多余的变量指标,电力和页。只是处理由右至左的字符串。你也不想递归函数外跟踪的全局变量数...它的混乱和怪异。你想要的递归函数内进行,在我看来,所有的状态。即使有这些限制,你还是可以做到这一点基本上两行:

 公共静态INT BIN2DEC(String s)将{
  如果(S == NULL || s.isEmpty())返回0;
  否则返回s.charAt(s.length() - 1)-48 + 2 * BIN2DEC(s.substring(0,s.length() - 1));
}

这可能不是最清晰的解决方案,但它是最优雅的,我想。清晰度可以通过打破else子句成几行得到改善。 48是0的统一code字符数,这也许不是转换的最佳方式各自的号码字符'0'和'1'。

Comp sci professor gave us this problem in our homework... I'm not sure how to proceed and the code I have written seems to be failing miserably. Here is the prompt:


(binary to decimal) Write a recursive method that parses a binary number as a string into a decimal integer. The method header is:

public static String bin2Dec(String binaryString)

write a test program that prompts the user to enter a binary string and displays its decimal equivalent.


Any help greatly appreciated. Here is my code as follows:

import java.util.Scanner;

public class HW04_P5 {
    static int index = 0;
    static int power = 0;
    static int number = 0;
    static boolean exit = false;

    @SuppressWarnings("resource")
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.print("  Enter a binary number to convert to decimal: ");
        String in = scan.nextLine();
        index = in.length()-1;
        System.out.print("  Binary number converted to decimal:          "+bin2Dec(in));
    }

    public static String bin2Dec(String in)
    {
        if((in.substring(index,index+1).equals("1"))&&(index>0))
        {
            number += Math.pow(2,power);
            System.out.print(number);
            power++;
            index--;
            bin2Dec(in);
        }
        else if((in.substring(index,index+1).equals("0"))&&(index>0))
        {
            power++;
            index--;
            bin2Dec(in);
        }
        System.out.print(number);
        return "";
    }
}

解决方案

It's cleaner not to have the extra variables index, power, and p. Just process the string from right to left. You also don't want the "global" variable number tracked outside the recursive function ... it's confusing and weird. You want all the state carried inside the recursive functions, in my opinion. Even with those restrictions, you can still do it in essentially two lines:

public static int bin2Dec(String s) {
  if (s == null || s.isEmpty()) return 0;
  else return s.charAt(s.length()-1)-48+2*bin2Dec(s.substring(0,s.length()-1));
}

That may not be the clearest solution, but it is the most elegant, I think. Clarity could be improved by breaking the else clause into several lines. 48 is the Unicode character number for 0, which is maybe not the best way to convert the characters '0' and '1' to their respective numbers.

这篇关于转换二进制数字字符串的十进制数...使用递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆